25
\$\begingroup\$

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 , so fewest bytes wins.

\$\endgroup\$
  • 2
    \$\begingroup\$ Related. \$\endgroup\$ – AdmBorkBork Dec 12 '18 at 16:23
  • \$\begingroup\$ Can we have spaces before/after each line? \$\endgroup\$ – Olivier Grégoire Dec 12 '18 at 16:54
  • \$\begingroup\$ @OlivierGrégoire Yes, trailing whitespace is ok. \$\endgroup\$ – Pavel Dec 12 '18 at 16:55
  • \$\begingroup\$ And heading whitespace? \$\endgroup\$ – Olivier Grégoire Dec 12 '18 at 16:58
  • \$\begingroup\$ @OlivierGrégoire Yeah, that's fine. \$\endgroup\$ – Pavel Dec 12 '18 at 17:00

46 Answers 46

2
\$\begingroup\$

C (gcc), 127 95 + 26 = 121 bytes

i,j;f(x){for(i=0;i<x;putchar(10),i+=2){for(j=0;E;printf(">\n%c",x-i-1?60:9);for(j=0;x-i-1&&E;}}

Try it online!

Compile with -DE=j<i/2;++j)putchar(45)

-6 bytes from Logern

Yay mismatched parentheses!

Ungolfed:

void f(int x) {
	for (int i = 0; i < x; i += 2) {
		for (int j = 0; j < i/2; ++j) {
			printf("-");
		}
		printf(">\n");
		if (x - i - 1) { // Test for last loop: only print <-- if x is even
			printf("<");
			for (int j = 0; j < i/2; ++j) {
				printf("-");
			}
		}
		else {
			printf("\t"); // This is the 9 in the x-i-1?60:9
		}
		printf("\n");
	}
}
			

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 18 bytes

F…> <N'-×.ø#`})s£»

Try it online!


F            }     # Loop from 0 to input...
 …> <              # Push '> <'...
     N'-×          # Push "-" multiplied by N.
         .ø        # Surround with this.
           #`      # Split on spaces, flatten both directions to stack.
              )    # Group all entries together after loop.
               s£  # Take first N.
                 » # Print with newlines.

Non-iterative, same byte-count:

05AB1E, 18 bytes

'-ש'>«.s®'<ìη.ιs£

Try it online!

Other, other method:

05AB1E, 18 bytes

'-×'>«.sð«.º€#˜s£

Try it online!

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 18 15 bytes

F'-N;∍'>«º2äNè,

Suggested as a golf for @nedla2004's 05AB1E answer, but he said I should post it myself since it's using a slightly different approach.

Try it online.

Explanation:

F                # Loop `N` in the range [0, (implicit) input):
   N;            #  Halve the current index `N`
 '-  ∍          '#  Have that many "-" (the halved index decimal .5 is ignored)
      '>«       '#  And append a ">"
         º       #  Now mirror it: i.e. "--->" becomes "---><---"
          2ä     #  Split it into two equal-sized parts: i.e. ["--->","<---"]
            Nè   #  Index the `N` into this array of two strings (with automatic wraparound)
              ,  #  And output it with a trailing newline
\$\endgroup\$
  • \$\begingroup\$ You could probably make something work with NÈ„><è \$\endgroup\$ – Magic Octopus Urn Jan 24 at 18:37
  • 1
    \$\begingroup\$ @MagicOctopusUrn Not sure I could use that, since one had to be prepended, and the other appended (hence the use of an if-else). But, I have been able to golf 3 bytes using a different approach now with a mirror º and splitting into two equal parts . :) \$\endgroup\$ – Kevin Cruijssen Jan 24 at 19:31
  • \$\begingroup\$ Your TIO link isn't right now :P \$\endgroup\$ – Magic Octopus Urn Jan 24 at 19:43
  • \$\begingroup\$ @MagicOctopusUrn Oops.. fixed. Thanks \$\endgroup\$ – Kevin Cruijssen Jan 24 at 19:52
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 69 bytes

i=>{for(var s="";i>0;s+=--i%2<1?"-":"")WriteLine(i%2<1?"<"+s:s+">");}

Try it online!

Uses a technique from Meerkat's answer, but has a lot of improvements.

Less golfed code:

// anonymous function with
// input i and no return value
i=>{
  for(
    // s is a string of hyphens
    var s="";
    // loop from i down to 0
    i>0;
    // decrement i and conditionally
    // tack on an additional hyphen
    // depending on whether i
    // is even or odd
    s+=--i%2<1?"-":""
  )
    // print the hyphens with an arrow
    // at the start or end, depending
    // on whether i is even or even
    WriteLine(i%2<1?"<"+s:s+">");
}
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 44 bytes

map{$_/=2;say'<'x/\./,'-'x$_,'>'x!$&}0..<>-1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ruby, 47 bytes

->n{n.times.map{|x|"<#{?-*n}>"[x%2*~x/=2,x+1]}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Tcl, 99 bytes

proc A i {time {puts [expr [incr j]%2?"":"<"][string repe - [expr ($j-1)/2]][expr $j%2?">":""]} $i}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 113 bytes

<?php $x=fgets(STDIN);for($i=0;$i<$x;){echo $i%2<1?str_repeat("-",$i++/2).">\n":"<".str_repeat("-",$i++/2)."\n";}

Try it online!

A pretty straightforward solution.

\$\endgroup\$
1
\$\begingroup\$

C#, 99 bytes

n=>new string[n].Select((x,i)=>(i%2>0?"<":"")+new String('-',(int)Math.Floor(i/2d))+(i%2<1?">":""))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Java 8, 123 120 119 bytes

n->{String l="",r="";for(int c=0;c<n;c++)System.out.println(c%2<1?r=c<1?">":c<2?"<":"-"+r:(l+=c<1?">":c<2?"<":"-"));}
\$\endgroup\$
  • \$\begingroup\$ In r=c<1?">":c==1? -- I think if c<1, equivalent to c==0, is excluded, c==1 is equivalent to c<2. \$\endgroup\$ – Jonathan Frech Dec 14 '18 at 18:05
  • \$\begingroup\$ @JonathanFrech - true, I updated it. thanx. \$\endgroup\$ – isaace Dec 17 '18 at 14:27
1
\$\begingroup\$

Lua, 69 68 bytes

for i=0,...-1 do a=("-"):rep(i//2)print(i%2<1 and a..">"or"<"..a)end

Explanation

for i=0,...-1 do  -- for 'i' going from 0 to the number on the first argument
  dashes = ("-"):rep(i//2)  -- dashes is a dash repeated by the iteration we are divided by 2 and rounded down
  print(i%2<1 and dashes..">" or "<"..dashes)  -- if 'i' is even, then print the dashes and then the right-facing arrow
                                               -- else, print the left-facing arrow and then the dashes
end

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Changed my solution! Thanks :) \$\endgroup\$ – Visckmart Jan 12 at 6:22
1
\$\begingroup\$

PHP, 102 bytes

command line options added -n -d

<?for(;$i++<$argv[1];){$b=str_repeat("-",abs($u=ceil($i/2)*pow(-1,$i+1))-1);echo($u>0)?"<$b
":"$b>
";}

Try it online!

Explanation

It is not a much different solution, but is using one of the ways to get the n-th term of the series A001057 wich is another way to look at the challenge.

According to A001057 the one way to obtain the n-th term is with the formula a($n)=ceil($n/2)*pow(-1,$n+1).

And you can look at the challenge like this.

>     +1
<     -1
->    +2
<-    -2
-->   +3
<--   -3
--->  +4
<---  -4
----> +5  
# And so on
\$\endgroup\$
1
\$\begingroup\$

Rust, 152 142 bytes

|n|{for i in 0..n{println!("");if i%2==1{print!("<");for _ in 1..i{print!("-");}}else{for _ in 1..i{print!("-");}print!(">");}}println!("");};

Down from 152 bytes to 142 bytes thanks to Pavel.

\$\endgroup\$
  • 3
    \$\begingroup\$ I'm not familiar with Rust, but it looks like you have a lot of extra spaces: Around %, around { and } \$\endgroup\$ – Pavel Jan 15 at 21:19
1
\$\begingroup\$

PHP, 76 73 72 67 bytes

for(;$x<$i;)echo$x&1?'<':'',str_repeat('-',$x/2),$x++&1?'':'>',"
";

Try it online!

Output:

>
<
->
<-
-->
<--
--->
<---
---->
<----
----->
<-----
------>
<------
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 36 bytes

say$|--?v17^($@.='-'):"$@>"for 1..<>

Try it online!

Gets < from the bitwise-xor of - and v17 (ASCII 17).

\$\endgroup\$
1
\$\begingroup\$

CJam, 31 bytes

qi{A2%{'<'-A2/*}{'-A2/*'>}?N}fA

Explanation:

qi{A2%{'<'-A2/*}{'-A2/*'>}?N}fA e# Whole code
qi                              e# Get input as an integer
  {                         }fA e# A for loop, from 0 - (input - 1), stored in var A
   A2%                          e# Is A a multiple of 2?
      {        }{        }?     e# Ternary operator: Left is true, right is false
      {
       '<                       e# Push "<" onto the stack
         '-                     e# Push "-" onto the stack
           A2/                  e# Get A/2
              *                 e# Repeat "-" A/2 times
               }{
                 '-             e# Push "-" onto the stack
                   A2/          e# Get A/2
                      *         e# Repeat "-" A/2 times
                       '>       e# Push ">" onto the stack
                         }
                           N    e# Push a new line after each arrow
                                e# Implicit output

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.