Draw some expanding arrows
up vote
9
down vote
favorite
This challenge is about printing a series of growing ASCII-art arrows. I'll describe the pattern in words, but it might be easier to look at what the start of this series looks like:
>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...
An arrow with length n contains an arrowhead (<
or >
) and n-1
dashes (-
). A right-facing arrow has the dashes first, then a >
. A left-facing arrow starts with <
, and is followed by the dashes. The series consists of a length n
right-facing arrow followed by a length n left-facing arrow, with n from 1 to infinity.
To complete the challenge, write a program or function that takes one input, an integer i >= 1
, and outputs the first i
arrows. Arrows are individual, not in right-left pairs, so for i=3
you should output:
>
<
->
You can return a list of strings, or print them one after the other. If printing, the arrows must be delimited by some consistent delimiter, which doesn't have to be a newline as in the example.
This is code-golf, so fewest bytes wins.
code-golf ascii-art sequence
add a comment |
up vote
9
down vote
favorite
This challenge is about printing a series of growing ASCII-art arrows. I'll describe the pattern in words, but it might be easier to look at what the start of this series looks like:
>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...
An arrow with length n contains an arrowhead (<
or >
) and n-1
dashes (-
). A right-facing arrow has the dashes first, then a >
. A left-facing arrow starts with <
, and is followed by the dashes. The series consists of a length n
right-facing arrow followed by a length n left-facing arrow, with n from 1 to infinity.
To complete the challenge, write a program or function that takes one input, an integer i >= 1
, and outputs the first i
arrows. Arrows are individual, not in right-left pairs, so for i=3
you should output:
>
<
->
You can return a list of strings, or print them one after the other. If printing, the arrows must be delimited by some consistent delimiter, which doesn't have to be a newline as in the example.
This is code-golf, so fewest bytes wins.
code-golf ascii-art sequence
1
Related.
– AdmBorkBork
3 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago
add a comment |
up vote
9
down vote
favorite
up vote
9
down vote
favorite
This challenge is about printing a series of growing ASCII-art arrows. I'll describe the pattern in words, but it might be easier to look at what the start of this series looks like:
>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...
An arrow with length n contains an arrowhead (<
or >
) and n-1
dashes (-
). A right-facing arrow has the dashes first, then a >
. A left-facing arrow starts with <
, and is followed by the dashes. The series consists of a length n
right-facing arrow followed by a length n left-facing arrow, with n from 1 to infinity.
To complete the challenge, write a program or function that takes one input, an integer i >= 1
, and outputs the first i
arrows. Arrows are individual, not in right-left pairs, so for i=3
you should output:
>
<
->
You can return a list of strings, or print them one after the other. If printing, the arrows must be delimited by some consistent delimiter, which doesn't have to be a newline as in the example.
This is code-golf, so fewest bytes wins.
code-golf ascii-art sequence
This challenge is about printing a series of growing ASCII-art arrows. I'll describe the pattern in words, but it might be easier to look at what the start of this series looks like:
>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
...
An arrow with length n contains an arrowhead (<
or >
) and n-1
dashes (-
). A right-facing arrow has the dashes first, then a >
. A left-facing arrow starts with <
, and is followed by the dashes. The series consists of a length n
right-facing arrow followed by a length n left-facing arrow, with n from 1 to infinity.
To complete the challenge, write a program or function that takes one input, an integer i >= 1
, and outputs the first i
arrows. Arrows are individual, not in right-left pairs, so for i=3
you should output:
>
<
->
You can return a list of strings, or print them one after the other. If printing, the arrows must be delimited by some consistent delimiter, which doesn't have to be a newline as in the example.
This is code-golf, so fewest bytes wins.
code-golf ascii-art sequence
code-golf ascii-art sequence
asked 3 hours ago
Pavel
4,73813287
4,73813287
1
Related.
– AdmBorkBork
3 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago
add a comment |
1
Related.
– AdmBorkBork
3 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago
1
1
Related.
– AdmBorkBork
3 hours ago
Related.
– AdmBorkBork
3 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago
add a comment |
15 Answers
15
active
oldest
votes
up vote
6
down vote
R, 69 bytes
for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')
Try it online!
- -5 bytes thanks to @Giuseppe
- -3 bytes thanks to @Robert S.
strrep
coerces its second argument tointeger
so you can use/
in place of%/%
– Giuseppe
2 hours ago
you can also get rid ofa
entirely by indexing over0...(n-1)
instead: Try it online!
– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
add a comment |
up vote
3
down vote
Canvas, 10 bytes
⇵-×<n¹[↔}]
Try it here!
I don't know any Canvas, but is that an arrow-drawing builtin I see?↔
kinda looks like it!
– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping>
&<
), sadly no arrow built-ins :p
– dzaima
3 hours ago
add a comment |
up vote
3
down vote
Python 2, 53 bytes
k=-1
exec"print'<'[k%2:]+'-'*k+k%2*'>';k+=1;"*input()
Try it online!
add a comment |
up vote
3
down vote
Commodore BASIC V2 (C64), 94 bytes
0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE
Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.
Online Demo
Slightly "ungolfed":
0 inputn:fori=1ton:oniand1goto1:print"<"; :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">"; :rem if even skip ">"
3 print:next :rem newline and next loop iteration
add a comment |
up vote
3
down vote
PowerShell, 62 56 50 bytes
param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]
Try it online!
Loops from 0
up to input $n
, each iteration creating two arrow strings. Those are then indexed with 0..--$n
to pull out the correct number of elements.
Saved 6 bytes thanks to KGlasier.
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ieparam($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write$x
twice.
– KGlasier
2 hours ago
Also you can save two more bytes by not using++
in($j='-'*$_++)
as you don't use$_
anywhere else.
– KGlasier
2 hours ago
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
add a comment |
up vote
2
down vote
Pyth, 17 bytes
m_W%d2+*-/d2@"><
Output is a list of strings. Try it online here.
m_W%d2+*-/d2@"><"dQ Implicit: Q=eval(input())
Trailing "dQ inferred
m Q Map [0-Q), as d, using:
/d2 Floored division of d by 2
*- Repeat "-" the above number of times
+ Append to the above...
@"><"d Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W Reverse the above if...
%d2 ... (d % 2) != 0
Implicit print result of the map
add a comment |
up vote
2
down vote
Java (JDK), 81 bytes
n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}
Try it online!
Explanations
n->{ // int-accepting consumer
for(int i=0;i<n;) // for each i from 0 to n-1 included
System.out.printf( // output on stdout with a pattern
i%2<1 // if i is even:
?"<%s%n" // use the left-arrow pattern
:"%s>%n", // else: use the right-arrow pattern
"-".repeat(i++/2) // fill the pattern with i/2 dashes, and increment i
); //
} //
add a comment |
up vote
2
down vote
SNOBOL4 (CSNOBOL4), 123 122 118 bytes
N =INPUT - 1
P H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>' :S(I)
OUTPUT ='<' Y
I X =LT(X,N) X + 1 :S(P)
END
Try it online!
add a comment |
up vote
1
down vote
JavaScript (ES6), 58 bytes
Returns a space-separated string.
n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')
Try it online!
add a comment |
up vote
1
down vote
Jelly, 15 bytes
ị⁾><;’H”-ẋƲṚ⁸¡)
Try it online!
add a comment |
up vote
1
down vote
Haskell, 51 bytes
(`take`do b<-['-'<$[1..n]|n<-[0..]];[b++">",'<':b])
Try it online!
Explanation / Ungolfed
Using do
-notation saves us a concat
, using '-'<$[1..n]
is shorter than replicate
, and finally using infix-notation allows a pointfree function with take
, undoing these would give:
f n = take n $ concat [ [b++">", '<':b] | n<-[0..], let b = replicate n '-' ]
add a comment |
up vote
1
down vote
Haskell, 41 bytes
(`take`g"")
g p=(p++">"):('<':p):g('-':p)
Try it online!
Plain old recursion: start with an emtpy string p
, collect p
with a right arrow, a left arrow with p
and a recursive call with p
one -
longer. Take the first n
items of this list.
add a comment |
up vote
1
down vote
Powershell, 51 bytes
param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}
add a comment |
up vote
1
down vote
V, 22 bytes
i>
<Àñäkjjé-já-ñÀGjdG
Try it online!
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
add a comment |
up vote
1
down vote
Japt -m
, 22 bytes
u ç'< +Uz ç'- +°Uu ç'>
Try it online!
Explanation:
#Implicitly map over the range [0..input) as U
u #U modulo 2
ç'< #Print "<" that number of times (i.e. once, only if it's odd)
+ #Concat
Uz #U integer divide by 2
ç'- #Print "-" that number of times
+ #Concat
°Uu #U+1 modulo 2
ç'> #Print ">" that number of times (i.e. once, only if it's even)
add a comment |
15 Answers
15
active
oldest
votes
15 Answers
15
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
R, 69 bytes
for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')
Try it online!
- -5 bytes thanks to @Giuseppe
- -3 bytes thanks to @Robert S.
strrep
coerces its second argument tointeger
so you can use/
in place of%/%
– Giuseppe
2 hours ago
you can also get rid ofa
entirely by indexing over0...(n-1)
instead: Try it online!
– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
add a comment |
up vote
6
down vote
R, 69 bytes
for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')
Try it online!
- -5 bytes thanks to @Giuseppe
- -3 bytes thanks to @Robert S.
strrep
coerces its second argument tointeger
so you can use/
in place of%/%
– Giuseppe
2 hours ago
you can also get rid ofa
entirely by indexing over0...(n-1)
instead: Try it online!
– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
add a comment |
up vote
6
down vote
up vote
6
down vote
R, 69 bytes
for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')
Try it online!
- -5 bytes thanks to @Giuseppe
- -3 bytes thanks to @Robert S.
R, 69 bytes
for(i in 1:scan()-1)cat('<'[i%%2],rep('-',i/2),'>'[!i%%2],'
',sep='')
Try it online!
- -5 bytes thanks to @Giuseppe
- -3 bytes thanks to @Robert S.
edited 2 hours ago
answered 2 hours ago
digEmAll
2,36148
2,36148
strrep
coerces its second argument tointeger
so you can use/
in place of%/%
– Giuseppe
2 hours ago
you can also get rid ofa
entirely by indexing over0...(n-1)
instead: Try it online!
– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
add a comment |
strrep
coerces its second argument tointeger
so you can use/
in place of%/%
– Giuseppe
2 hours ago
you can also get rid ofa
entirely by indexing over0...(n-1)
instead: Try it online!
– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
strrep
coerces its second argument to integer
so you can use /
in place of %/%
– Giuseppe
2 hours ago
strrep
coerces its second argument to integer
so you can use /
in place of %/%
– Giuseppe
2 hours ago
you can also get rid of
a
entirely by indexing over 0...(n-1)
instead: Try it online!– Giuseppe
2 hours ago
you can also get rid of
a
entirely by indexing over 0...(n-1)
instead: Try it online!– Giuseppe
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
I'm an idiot... thanks ! :D
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
@Giuseppe :also I just noticed the deleted question of Robert S. I can use rep instead of strrep and save 3 bytes...(facepalm)
– digEmAll
2 hours ago
add a comment |
up vote
3
down vote
Canvas, 10 bytes
⇵-×<n¹[↔}]
Try it here!
I don't know any Canvas, but is that an arrow-drawing builtin I see?↔
kinda looks like it!
– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping>
&<
), sadly no arrow built-ins :p
– dzaima
3 hours ago
add a comment |
up vote
3
down vote
Canvas, 10 bytes
⇵-×<n¹[↔}]
Try it here!
I don't know any Canvas, but is that an arrow-drawing builtin I see?↔
kinda looks like it!
– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping>
&<
), sadly no arrow built-ins :p
– dzaima
3 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
Canvas, 10 bytes
⇵-×<n¹[↔}]
Try it here!
Canvas, 10 bytes
⇵-×<n¹[↔}]
Try it here!
answered 3 hours ago
dzaima
14.3k21754
14.3k21754
I don't know any Canvas, but is that an arrow-drawing builtin I see?↔
kinda looks like it!
– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping>
&<
), sadly no arrow built-ins :p
– dzaima
3 hours ago
add a comment |
I don't know any Canvas, but is that an arrow-drawing builtin I see?↔
kinda looks like it!
– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping>
&<
), sadly no arrow built-ins :p
– dzaima
3 hours ago
I don't know any Canvas, but is that an arrow-drawing builtin I see?
↔
kinda looks like it!– Pavel
3 hours ago
I don't know any Canvas, but is that an arrow-drawing builtin I see?
↔
kinda looks like it!– Pavel
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping >
& <
), sadly no arrow built-ins :p– dzaima
3 hours ago
↔
is the "reverse horizontally" built-in (also swapping >
& <
), sadly no arrow built-ins :p– dzaima
3 hours ago
add a comment |
up vote
3
down vote
Python 2, 53 bytes
k=-1
exec"print'<'[k%2:]+'-'*k+k%2*'>';k+=1;"*input()
Try it online!
add a comment |
up vote
3
down vote
Python 2, 53 bytes
k=-1
exec"print'<'[k%2:]+'-'*k+k%2*'>';k+=1;"*input()
Try it online!
add a comment |
up vote
3
down vote
up vote
3
down vote
Python 2, 53 bytes
k=-1
exec"print'<'[k%2:]+'-'*k+k%2*'>';k+=1;"*input()
Try it online!
Python 2, 53 bytes
k=-1
exec"print'<'[k%2:]+'-'*k+k%2*'>';k+=1;"*input()
Try it online!
answered 3 hours ago
ovs
18.6k21059
18.6k21059
add a comment |
add a comment |
up vote
3
down vote
Commodore BASIC V2 (C64), 94 bytes
0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE
Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.
Online Demo
Slightly "ungolfed":
0 inputn:fori=1ton:oniand1goto1:print"<"; :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">"; :rem if even skip ">"
3 print:next :rem newline and next loop iteration
add a comment |
up vote
3
down vote
Commodore BASIC V2 (C64), 94 bytes
0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE
Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.
Online Demo
Slightly "ungolfed":
0 inputn:fori=1ton:oniand1goto1:print"<"; :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">"; :rem if even skip ">"
3 print:next :rem newline and next loop iteration
add a comment |
up vote
3
down vote
up vote
3
down vote
Commodore BASIC V2 (C64), 94 bytes
0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE
Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.
Online Demo
Slightly "ungolfed":
0 inputn:fori=1ton:oniand1goto1:print"<"; :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">"; :rem if even skip ">"
3 print:next :rem newline and next loop iteration
Commodore BASIC V2 (C64), 94 bytes
0inputn:fOi=1ton:oniaN1gO1:?"<";
1on-(i<3)gO2:fOj=1.5toi/2:?"-";:nE
2on-nOiaN1gO3:?">";
3?:nE
Not entirely sure about the byte count, this is based on the text representation for typing the valid program. It's a bit shorter on disk (91 bytes) because BASIC V2 uses a "tokenized" representation of programs.
Online Demo
Slightly "ungolfed":
0 inputn:fori=1ton:oniand1goto1:print"<"; :rem read n from user, loop to n, if odd skip "<"
1 on-(i<3)goto2:forj=1.5toi/2:print"-";:next :rem skip for i<3, print (i-1)/2 times "-"
2 on-notiand1goto3:print">"; :rem if even skip ">"
3 print:next :rem newline and next loop iteration
answered 2 hours ago
Felix Palmen
3,261525
3,261525
add a comment |
add a comment |
up vote
3
down vote
PowerShell, 62 56 50 bytes
param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]
Try it online!
Loops from 0
up to input $n
, each iteration creating two arrow strings. Those are then indexed with 0..--$n
to pull out the correct number of elements.
Saved 6 bytes thanks to KGlasier.
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ieparam($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write$x
twice.
– KGlasier
2 hours ago
Also you can save two more bytes by not using++
in($j='-'*$_++)
as you don't use$_
anywhere else.
– KGlasier
2 hours ago
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
add a comment |
up vote
3
down vote
PowerShell, 62 56 50 bytes
param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]
Try it online!
Loops from 0
up to input $n
, each iteration creating two arrow strings. Those are then indexed with 0..--$n
to pull out the correct number of elements.
Saved 6 bytes thanks to KGlasier.
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ieparam($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write$x
twice.
– KGlasier
2 hours ago
Also you can save two more bytes by not using++
in($j='-'*$_++)
as you don't use$_
anywhere else.
– KGlasier
2 hours ago
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
add a comment |
up vote
3
down vote
up vote
3
down vote
PowerShell, 62 56 50 bytes
param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]
Try it online!
Loops from 0
up to input $n
, each iteration creating two arrow strings. Those are then indexed with 0..--$n
to pull out the correct number of elements.
Saved 6 bytes thanks to KGlasier.
PowerShell, 62 56 50 bytes
param($n)(0..$n|%{($j='-'*$_)+'>';"<$j"})[0..--$n]
Try it online!
Loops from 0
up to input $n
, each iteration creating two arrow strings. Those are then indexed with 0..--$n
to pull out the correct number of elements.
Saved 6 bytes thanks to KGlasier.
edited 2 hours ago
answered 2 hours ago
AdmBorkBork
26k364226
26k364226
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ieparam($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write$x
twice.
– KGlasier
2 hours ago
Also you can save two more bytes by not using++
in($j='-'*$_++)
as you don't use$_
anywhere else.
– KGlasier
2 hours ago
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
add a comment |
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ieparam($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write$x
twice.
– KGlasier
2 hours ago
Also you can save two more bytes by not using++
in($j='-'*$_++)
as you don't use$_
anywhere else.
– KGlasier
2 hours ago
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ie
param($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write $x
twice.– KGlasier
2 hours ago
Messing around with my own solution I found a way to cut a few bytes on yours: Can save 4 bytes by wrapping the loop in brackets and indexing directly. ie
param($n)(0..$n|%{($j='-'*$_++)+'>';"<$j"})[0..--$n]
. So now you don't have to write $x
twice.– KGlasier
2 hours ago
Also you can save two more bytes by not using
++
in ($j='-'*$_++)
as you don't use $_
anywhere else.– KGlasier
2 hours ago
Also you can save two more bytes by not using
++
in ($j='-'*$_++)
as you don't use $_
anywhere else.– KGlasier
2 hours ago
1
1
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
@KGlasier Awesome - thanks for the obvious golfs! :)
– AdmBorkBork
2 hours ago
add a comment |
up vote
2
down vote
Pyth, 17 bytes
m_W%d2+*-/d2@"><
Output is a list of strings. Try it online here.
m_W%d2+*-/d2@"><"dQ Implicit: Q=eval(input())
Trailing "dQ inferred
m Q Map [0-Q), as d, using:
/d2 Floored division of d by 2
*- Repeat "-" the above number of times
+ Append to the above...
@"><"d Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W Reverse the above if...
%d2 ... (d % 2) != 0
Implicit print result of the map
add a comment |
up vote
2
down vote
Pyth, 17 bytes
m_W%d2+*-/d2@"><
Output is a list of strings. Try it online here.
m_W%d2+*-/d2@"><"dQ Implicit: Q=eval(input())
Trailing "dQ inferred
m Q Map [0-Q), as d, using:
/d2 Floored division of d by 2
*- Repeat "-" the above number of times
+ Append to the above...
@"><"d Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W Reverse the above if...
%d2 ... (d % 2) != 0
Implicit print result of the map
add a comment |
up vote
2
down vote
up vote
2
down vote
Pyth, 17 bytes
m_W%d2+*-/d2@"><
Output is a list of strings. Try it online here.
m_W%d2+*-/d2@"><"dQ Implicit: Q=eval(input())
Trailing "dQ inferred
m Q Map [0-Q), as d, using:
/d2 Floored division of d by 2
*- Repeat "-" the above number of times
+ Append to the above...
@"><"d Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W Reverse the above if...
%d2 ... (d % 2) != 0
Implicit print result of the map
Pyth, 17 bytes
m_W%d2+*-/d2@"><
Output is a list of strings. Try it online here.
m_W%d2+*-/d2@"><"dQ Implicit: Q=eval(input())
Trailing "dQ inferred
m Q Map [0-Q), as d, using:
/d2 Floored division of d by 2
*- Repeat "-" the above number of times
+ Append to the above...
@"><"d Modular index d into "><" - yields ">" for even d, "<" for odd
- examples: d=4 gives "-->", d=7 gives "---<"
_W Reverse the above if...
%d2 ... (d % 2) != 0
Implicit print result of the map
answered 3 hours ago
Sok
3,459722
3,459722
add a comment |
add a comment |
up vote
2
down vote
Java (JDK), 81 bytes
n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}
Try it online!
Explanations
n->{ // int-accepting consumer
for(int i=0;i<n;) // for each i from 0 to n-1 included
System.out.printf( // output on stdout with a pattern
i%2<1 // if i is even:
?"<%s%n" // use the left-arrow pattern
:"%s>%n", // else: use the right-arrow pattern
"-".repeat(i++/2) // fill the pattern with i/2 dashes, and increment i
); //
} //
add a comment |
up vote
2
down vote
Java (JDK), 81 bytes
n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}
Try it online!
Explanations
n->{ // int-accepting consumer
for(int i=0;i<n;) // for each i from 0 to n-1 included
System.out.printf( // output on stdout with a pattern
i%2<1 // if i is even:
?"<%s%n" // use the left-arrow pattern
:"%s>%n", // else: use the right-arrow pattern
"-".repeat(i++/2) // fill the pattern with i/2 dashes, and increment i
); //
} //
add a comment |
up vote
2
down vote
up vote
2
down vote
Java (JDK), 81 bytes
n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}
Try it online!
Explanations
n->{ // int-accepting consumer
for(int i=0;i<n;) // for each i from 0 to n-1 included
System.out.printf( // output on stdout with a pattern
i%2<1 // if i is even:
?"<%s%n" // use the left-arrow pattern
:"%s>%n", // else: use the right-arrow pattern
"-".repeat(i++/2) // fill the pattern with i/2 dashes, and increment i
); //
} //
Java (JDK), 81 bytes
n->{for(int i=0;i<n;)System.out.printf(i%2<1?"<%s%n":"%s>%n","-".repeat(i++/2));}
Try it online!
Explanations
n->{ // int-accepting consumer
for(int i=0;i<n;) // for each i from 0 to n-1 included
System.out.printf( // output on stdout with a pattern
i%2<1 // if i is even:
?"<%s%n" // use the left-arrow pattern
:"%s>%n", // else: use the right-arrow pattern
"-".repeat(i++/2) // fill the pattern with i/2 dashes, and increment i
); //
} //
edited 2 hours ago
answered 2 hours ago
Olivier Grégoire
8,60711843
8,60711843
add a comment |
add a comment |
up vote
2
down vote
SNOBOL4 (CSNOBOL4), 123 122 118 bytes
N =INPUT - 1
P H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>' :S(I)
OUTPUT ='<' Y
I X =LT(X,N) X + 1 :S(P)
END
Try it online!
add a comment |
up vote
2
down vote
SNOBOL4 (CSNOBOL4), 123 122 118 bytes
N =INPUT - 1
P H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>' :S(I)
OUTPUT ='<' Y
I X =LT(X,N) X + 1 :S(P)
END
Try it online!
add a comment |
up vote
2
down vote
up vote
2
down vote
SNOBOL4 (CSNOBOL4), 123 122 118 bytes
N =INPUT - 1
P H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>' :S(I)
OUTPUT ='<' Y
I X =LT(X,N) X + 1 :S(P)
END
Try it online!
SNOBOL4 (CSNOBOL4), 123 122 118 bytes
N =INPUT - 1
P H =X / 2
Y =DUPL('-',H)
OUTPUT =EQ(H,X - H) Y '>' :S(I)
OUTPUT ='<' Y
I X =LT(X,N) X + 1 :S(P)
END
Try it online!
edited 2 hours ago
answered 2 hours ago
Giuseppe
16.4k31052
16.4k31052
add a comment |
add a comment |
up vote
1
down vote
JavaScript (ES6), 58 bytes
Returns a space-separated string.
n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')
Try it online!
add a comment |
up vote
1
down vote
JavaScript (ES6), 58 bytes
Returns a space-separated string.
n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 58 bytes
Returns a space-separated string.
n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')
Try it online!
JavaScript (ES6), 58 bytes
Returns a space-separated string.
n=>(g=p=>n--?k++&1?`<${p} `+g(p+'-'):p+'> '+g(p):'')(k='')
Try it online!
answered 2 hours ago
Arnauld
71.3k688298
71.3k688298
add a comment |
add a comment |
up vote
1
down vote
Jelly, 15 bytes
ị⁾><;’H”-ẋƲṚ⁸¡)
Try it online!
add a comment |
up vote
1
down vote
Jelly, 15 bytes
ị⁾><;’H”-ẋƲṚ⁸¡)
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Jelly, 15 bytes
ị⁾><;’H”-ẋƲṚ⁸¡)
Try it online!
Jelly, 15 bytes
ị⁾><;’H”-ẋƲṚ⁸¡)
Try it online!
answered 2 hours ago
Erik the Outgolfer
31k429102
31k429102
add a comment |
add a comment |
up vote
1
down vote
Haskell, 51 bytes
(`take`do b<-['-'<$[1..n]|n<-[0..]];[b++">",'<':b])
Try it online!
Explanation / Ungolfed
Using do
-notation saves us a concat
, using '-'<$[1..n]
is shorter than replicate
, and finally using infix-notation allows a pointfree function with take
, undoing these would give:
f n = take n $ concat [ [b++">", '<':b] | n<-[0..], let b = replicate n '-' ]
add a comment |
up vote
1
down vote
Haskell, 51 bytes
(`take`do b<-['-'<$[1..n]|n<-[0..]];[b++">",'<':b])
Try it online!
Explanation / Ungolfed
Using do
-notation saves us a concat
, using '-'<$[1..n]
is shorter than replicate
, and finally using infix-notation allows a pointfree function with take
, undoing these would give:
f n = take n $ concat [ [b++">", '<':b] | n<-[0..], let b = replicate n '-' ]
add a comment |
up vote
1
down vote
up vote
1
down vote
Haskell, 51 bytes
(`take`do b<-['-'<$[1..n]|n<-[0..]];[b++">",'<':b])
Try it online!
Explanation / Ungolfed
Using do
-notation saves us a concat
, using '-'<$[1..n]
is shorter than replicate
, and finally using infix-notation allows a pointfree function with take
, undoing these would give:
f n = take n $ concat [ [b++">", '<':b] | n<-[0..], let b = replicate n '-' ]
Haskell, 51 bytes
(`take`do b<-['-'<$[1..n]|n<-[0..]];[b++">",'<':b])
Try it online!
Explanation / Ungolfed
Using do
-notation saves us a concat
, using '-'<$[1..n]
is shorter than replicate
, and finally using infix-notation allows a pointfree function with take
, undoing these would give:
f n = take n $ concat [ [b++">", '<':b] | n<-[0..], let b = replicate n '-' ]
edited 1 hour ago
answered 2 hours ago
BMO
11k21881
11k21881
add a comment |
add a comment |
up vote
1
down vote
Haskell, 41 bytes
(`take`g"")
g p=(p++">"):('<':p):g('-':p)
Try it online!
Plain old recursion: start with an emtpy string p
, collect p
with a right arrow, a left arrow with p
and a recursive call with p
one -
longer. Take the first n
items of this list.
add a comment |
up vote
1
down vote
Haskell, 41 bytes
(`take`g"")
g p=(p++">"):('<':p):g('-':p)
Try it online!
Plain old recursion: start with an emtpy string p
, collect p
with a right arrow, a left arrow with p
and a recursive call with p
one -
longer. Take the first n
items of this list.
add a comment |
up vote
1
down vote
up vote
1
down vote
Haskell, 41 bytes
(`take`g"")
g p=(p++">"):('<':p):g('-':p)
Try it online!
Plain old recursion: start with an emtpy string p
, collect p
with a right arrow, a left arrow with p
and a recursive call with p
one -
longer. Take the first n
items of this list.
Haskell, 41 bytes
(`take`g"")
g p=(p++">"):('<':p):g('-':p)
Try it online!
Plain old recursion: start with an emtpy string p
, collect p
with a right arrow, a left arrow with p
and a recursive call with p
one -
longer. Take the first n
items of this list.
edited 1 hour ago
answered 1 hour ago
nimi
31.1k31985
31.1k31985
add a comment |
add a comment |
up vote
1
down vote
Powershell, 51 bytes
param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}
add a comment |
up vote
1
down vote
Powershell, 51 bytes
param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}
add a comment |
up vote
1
down vote
up vote
1
down vote
Powershell, 51 bytes
param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}
Powershell, 51 bytes
param($n)0..$n|%{'-'*$_+'>';'<'+'-'*$_}|?{$n---gt0}
answered 1 hour ago
mazzy
1,985314
1,985314
add a comment |
add a comment |
up vote
1
down vote
V, 22 bytes
i>
<Àñäkjjé-já-ñÀGjdG
Try it online!
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
add a comment |
up vote
1
down vote
V, 22 bytes
i>
<Àñäkjjé-já-ñÀGjdG
Try it online!
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
add a comment |
up vote
1
down vote
up vote
1
down vote
V, 22 bytes
i>
<Àñäkjjé-já-ñÀGjdG
Try it online!
V, 22 bytes
i>
<Àñäkjjé-já-ñÀGjdG
Try it online!
answered 1 hour ago
DJMcMayhem♦
40.7k11145308
40.7k11145308
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
add a comment |
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
1
1
This looks like some weird scandinavian language
– Pavel
1 hour ago
This looks like some weird scandinavian language
– Pavel
1 hour ago
add a comment |
up vote
1
down vote
Japt -m
, 22 bytes
u ç'< +Uz ç'- +°Uu ç'>
Try it online!
Explanation:
#Implicitly map over the range [0..input) as U
u #U modulo 2
ç'< #Print "<" that number of times (i.e. once, only if it's odd)
+ #Concat
Uz #U integer divide by 2
ç'- #Print "-" that number of times
+ #Concat
°Uu #U+1 modulo 2
ç'> #Print ">" that number of times (i.e. once, only if it's even)
add a comment |
up vote
1
down vote
Japt -m
, 22 bytes
u ç'< +Uz ç'- +°Uu ç'>
Try it online!
Explanation:
#Implicitly map over the range [0..input) as U
u #U modulo 2
ç'< #Print "<" that number of times (i.e. once, only if it's odd)
+ #Concat
Uz #U integer divide by 2
ç'- #Print "-" that number of times
+ #Concat
°Uu #U+1 modulo 2
ç'> #Print ">" that number of times (i.e. once, only if it's even)
add a comment |
up vote
1
down vote
up vote
1
down vote
Japt -m
, 22 bytes
u ç'< +Uz ç'- +°Uu ç'>
Try it online!
Explanation:
#Implicitly map over the range [0..input) as U
u #U modulo 2
ç'< #Print "<" that number of times (i.e. once, only if it's odd)
+ #Concat
Uz #U integer divide by 2
ç'- #Print "-" that number of times
+ #Concat
°Uu #U+1 modulo 2
ç'> #Print ">" that number of times (i.e. once, only if it's even)
Japt -m
, 22 bytes
u ç'< +Uz ç'- +°Uu ç'>
Try it online!
Explanation:
#Implicitly map over the range [0..input) as U
u #U modulo 2
ç'< #Print "<" that number of times (i.e. once, only if it's odd)
+ #Concat
Uz #U integer divide by 2
ç'- #Print "-" that number of times
+ #Concat
°Uu #U+1 modulo 2
ç'> #Print ">" that number of times (i.e. once, only if it's even)
answered 1 hour ago
Kamil Drakari
2,851416
2,851416
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177454%2fdraw-some-expanding-arrows%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Related.
– AdmBorkBork
3 hours ago
Can we have spaces before/after each line?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yes, trailing whitespace is ok.
– Pavel
2 hours ago
And heading whitespace?
– Olivier Grégoire
2 hours ago
@OlivierGrégoire Yeah, that's fine.
– Pavel
2 hours ago