25
\$\begingroup\$

Your task is to take in one integer input and print a zigzag pattern using slashes and backslashes.

  • The integer input determines the length of each zig and zag, as well as the number of zigs and zags
  • The pattern always starts from right to left

Test Cases

4->
   /
  /
 /
/
\
 \
  \
   \
   /
  /
 /
/
\
 \
  \
   \
2->
 /
/
\
 \
0->
1->
/
8->
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
| improve this question | | | | |
\$\endgroup\$
  • 3
    \$\begingroup\$ Can we output an array/list of strings for each line? Are training or leading newlines or spaces allowed? \$\endgroup\$ – Shaggy Sep 21 '17 at 23:50
  • 2
    \$\begingroup\$ Is leading whitespace okay as long as the pattern is unaffected? \$\endgroup\$ – Emigna Sep 22 '17 at 8:10

26 Answers 26

10
\$\begingroup\$

C (gcc), 108 102 101 98 80 76 72 bytes

  • Saved six bytes thanks to Kevin Cruijssen; removing parentheses and golfing N-n-1 to N+~n
  • Saved a byte by moving Z's incrementation into the loop condition
  • Saved three bytes by using printf("%c\n",...) instead of putchar(...) and ,puts("")
  • Saved eighteen (!) bytes thanks to HatsuPointerKun; using printf("%*s",n,""); to print n spaces instead of using a loop j;for(j=n;j--;)putchar(32); and combining both printf(...); calls
  • Saved four bytes by using printf("%*c",-~n,...); instead of printf("%*s%c",n,"",...);
  • Saved four bytes thanks to nwellnhof; moving everything inside one loop instead of two
j;f(k){for(j=0;j<k*k;j++)printf("%*c\n",j/k%2?j%k+1:k-j%k,j/k%2?92:47);}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Z,n,j;f(N){for(Z=0;Z<N;Z++)for(n=N;n--;putchar(Z%2?92:47),puts(""))for(j=Z%2?N+~n:n;j--;)putchar(32);} 102 bytes. Removed the curly brackets by putting everything inside the loops; and changed N-n-1 to N+~n. \$\endgroup\$ – Kevin Cruijssen Sep 22 '17 at 8:27
  • 1
    \$\begingroup\$ @KevinCruijssen Thanks. Saved another byte by swapping both Z%2?...:... and replacing Z<N;Z++ with Z++<N;. \$\endgroup\$ – Jonathan Frech Sep 22 '17 at 8:37
  • 1
    \$\begingroup\$ You can save several bytes by using the printf magic like i did in my answer. That way you will get rid of the for loop used to print spaces. For more details, see this stack overflow answer about left padding spaces with printf \$\endgroup\$ – HatsuPointerKun Sep 22 '17 at 9:46
  • \$\begingroup\$ @HatsuPointerKun Thanks; that is a really short way to repeat spaces in C. \$\endgroup\$ – Jonathan Frech Sep 22 '17 at 10:01
  • \$\begingroup\$ 4 bytes shorter: i;f(N){for(i=0;i<N*N;i++)printf("%*c\n",i/N%2?i%N+1:N-i%N,i/N%2?92:47);}. Try it online! \$\endgroup\$ – nwellnhof Sep 22 '17 at 13:25
10
\$\begingroup\$

Charcoal, 16 10 9 bytes

FN«↖Iθ→‖T

Try it online! Link is to verbose version of code.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ this works (Was InputNumber broken in golfy mode too?) \$\endgroup\$ – ASCII-only Oct 4 '17 at 11:29
  • \$\begingroup\$ @ASCII-only No, thus the separate links to the given succinct version and the approximate verbose version. \$\endgroup\$ – Neil Oct 4 '17 at 12:05
  • \$\begingroup\$ Oh >_> didn't look to see which link I was opening \$\endgroup\$ – ASCII-only Oct 4 '17 at 12:06
  • \$\begingroup\$ @ASCII-only Well there's only one link now ;-) \$\endgroup\$ – Neil Oct 4 '17 at 12:49
4
\$\begingroup\$

MATL, 17 bytes

:"GXy@o?P47}92]*c

Try it online!

Explanation

:         % Implicit input, n. Push range [1 2 ... n]
"         % For each k in that range
  G       %   Push n again
  Xy      %   Identity matrix of that size
  @       %   Push k
  o?      %   If it's odd
    P     %     Flip the matrix upside down
    47    %     Push 47 (ASCII for '/')
  }       %   Else
    92    %     Push 92 (ASCII for '\')
  ]       %   End
  *       %   Multiply each entry of the matrix by that number
  c       %   Convert to char. Char 0 is shown as space
          % Implicit end. Implicit display
| improve this answer | | | | |
\$\endgroup\$
4
\$\begingroup\$

C# (.NET Core), 117 103 101 bytes

a=>{for(int z=a+1,e=0;e<a*a;)System.Console.WriteLine(e++/a%2<1?"/".PadLeft(--z):@"\".PadLeft(z++));}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
3
\$\begingroup\$

SOGL V0.12, 13 12 9 bytes

╝F{±↔}P}ø

Try it Here!

could be 8 bytes ╝F{±↔}P} if the 0 test-case wasn't required

Explanation:

       }   implicitly started loop repeated input times
╝            create a down-right diagonal of the input
 F           get the current looping index, 1-indexed
  {  }       that many times
   ±↔          reverse the diagonal horizontally
      P      print that
        ø  push an empty string - something to implicitly print if the loop wasn't executed
| improve this answer | | | | |
\$\endgroup\$
3
\$\begingroup\$

Python 2, 69 68 62 bytes

-1 byte thanks to Jonathan Frech

lambda n:[[~i,i][i/n%2]%n*' '+'/\\'[i/n%2]for i in range(n*n)]

Try it online!

| improve this answer | | | | |
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 84 90 bytes

(n=#;Grid@Array[If[Abs[n-(s=Mod[#-1,2n])-.5]==#2-.5,If[s<n,"‌​/","\\"],""]&,{n^2,n‌​}])&
  • Thank Jenny_mathy for -6 bytes.

I have no idea why \ is obviously darker than /.

enter image description here

| improve this answer | | | | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 84 bytes (n=#;Grid@Array[If[Abs[n-(s=Mod[#-1,2n])-.5]==#2-.5,If[s<n,"/","\\"],""]&,{n^2,n}])& \$\endgroup\$ – J42161217 Sep 22 '17 at 10:52
3
\$\begingroup\$

Jq 1.5, 94 89 bytes

["/","\\"][range($n)%2]as$s|range($n)|[(range(if$s=="/"then$n-.-1 else. end)|" "),$s]|add

Explanation

  ["/","\\"][range($n)%2] as $s                         # for $s= / \ / \ $n times 
| range($n)                                             # for .=0 to $n-1
| [(range(if $s=="/" then $n-.-1 else . end)|" "), $s]  # form list of spaces ending with $s
| add                                                   # concatenate

Sample Run

$ jq -Mnr --argjson n 5 '["/","\\"][range($n)%2]as$s|range($n)|[(range(if$s=="/"then$n-.-1 else. end)|" "),$s]|add'
    /
   /
  /
 /
/
\
 \
  \
   \
    \
    /
   /
  /
 /
/
\
 \
  \
   \
    \
    /
   /
  /
 /
/

Try it online!

| improve this answer | | | | |
\$\endgroup\$
3
\$\begingroup\$

Java 8, 140 134 116 bytes

n->{String r="";for(int a=0,b,c;a++<n;)for(b=n;b-->0;r+=a%2>0?"/\n":"\\\n")for(c=b-n+b|-a%2;++c<b;r+=" ");return r;}

-24 bytes thanks to @Nevay.

Explanation:

Try it here.

n->{                // Method with integer parameter and String return-type
  String r="";      //  Result-String
  for(int a=0,b,c;  //  Index integers
      a++<n;)       //  Loop (1) from 0 to the input (exclusive)
    for(b=n;        //   Reset `b` to the input
        b-->0;      //   Inner loop (2) from the input to 0 (exclusive)
                    //     After every iteration: 
        r+=a%2>0?"/\n":"\\\n") 
                    //      Append either of the slashes + a new-line
      for(c=b-n+b|-a%2;++c<b;r+=" ");
                    //    Append the correct amount of spaces
                    //   End of inner loop (2) (implicit / single-line body)
                    //  End of loop (1) (implicit / single-line body)
  return r;         //  Return the result-String
}                   // End of method
| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ The condition of the innermost loop can be written as c-->f*(b-n-~b) (-6 bytes). \$\endgroup\$ – Nevay Sep 22 '17 at 14:31
  • 1
    \$\begingroup\$ 116 bytes: n->{String r="";for(int a=0,b,c;a++<n;)for(b=n;b-->0;r+=a%2>0?"/\n":"\\\n")for(c=b-n+b|-a%2;++c<b;r+=" ");return r;} \$\endgroup\$ – Nevay Sep 24 '17 at 14:09
3
\$\begingroup\$

Javascript ES8, 83 79 78 76 75 74 71 bytes

*reduced 1 byte with ES8 thanks to Shaggy

A=(m,i=0)=>i<m*m?`/\\`[x=i/m&1].padStart(x?i%m+1:m-i%m)+`
`+A(m,++i):""

Test here

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ whoever downvoted my solution, could you explain why? Am i missing something? \$\endgroup\$ – DanielIndie Sep 22 '17 at 12:19
  • 2
    \$\begingroup\$ I'm not the one who downvoted, but I assume that it is because functions need to be repeatable to be valid. Yours could be fixed pretty easily by making i a default parameter. The byte count seem off as well. \$\endgroup\$ – Emigna Sep 22 '17 at 12:21
  • 1
    \$\begingroup\$ Adding a TIO link is always appreciated as well, so that people can easily test your solution. \$\endgroup\$ – Emigna Sep 22 '17 at 12:23
  • 1
    \$\begingroup\$ @Emigna fixed it (char wise and Link wise) :) \$\endgroup\$ – DanielIndie Sep 22 '17 at 13:19
  • 1
    \$\begingroup\$ 74 bytes with some ES8. Also, for JS you can just use a Stack Snippet rather than TIO. \$\endgroup\$ – Shaggy Sep 22 '17 at 15:20
2
\$\begingroup\$

Pyth, 20 bytes

Lm+*;dbQjs<*,_y\/y\\

Try it online!

| improve this answer | | | | |
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 81 bytes

param($a)if($a){1..$a|%{((1..$a|%{" "*--$_+'\'}),($a..1|%{" "*--$_+'/'}))[$_%2]}}

Try it online!

Ugh, this is ugly. So much repeated code, plus 7 bytes required to account for 0 special case. Golfing suggestions welcome.

| improve this answer | | | | |
\$\endgroup\$
2
\$\begingroup\$

Pyth, 17 bytes

js<*_+RV"\/"_B*L;

Try it online: Demonstration

Explanation:

js<*_+RV"\/"_B*L;QQQ   implicit Qs at the end
              *L;Q     list with ["", " ", "  ", ..., " "*(input-1)]
            _B         bifurcate with reverse: [["" to "   "], ["   " to ""]]
     +RV"\/"           append to each one either "\" or "/": 
                       [["\", to "   \"], ["   /" to "/"]]
    _                  reverse
   *              Q    repeat input times
  <                Q   but only take the first input many
 s                     flatten the list of lists
j                      print on each line
| improve this answer | | | | |
\$\endgroup\$
2
\$\begingroup\$

Python 3: 90 Bytes 82 Bytes

lambda n:"\n".join(" "*(abs(i%(n*2)-n+i//n%2)-1)+"/\\"[i//n%2]for i in range(n*n))

Thanks to @Jonathan Frech for pointing out that print wasn't needed and that the first zig was the wrong way

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ ] for -> ]for. \$\endgroup\$ – Jonathan Frech Sep 22 '17 at 9:04
  • \$\begingroup\$ You do not need the print(...), a function returning a string would be valid. Also, I think your initial zig is oriented the wrong way (\ rather than /). \$\endgroup\$ – Jonathan Frech Sep 22 '17 at 9:08
  • \$\begingroup\$ @JonathanFrech Thanks! I changed it \$\endgroup\$ – Bassintag Sep 22 '17 at 9:23
  • 1
    \$\begingroup\$ (abs(...)-1) -> ~-abs(...). \$\endgroup\$ – Jonathan Frech Sep 22 '17 at 9:26
2
\$\begingroup\$

05AB1E, 17 16 bytes

F<„/\Nèú.sNƒR}»,

Try it online!

Explanation

F                  # for N in [0 ... input-1] do
  „/\              # push the string "/\"
     Nè            # cyclically index into this string with N
 <     ú           # prepend input-1 spaces to this string
        .s         # get suffixes
          NƒR}     # reverse the list of suffixes input+1 times
              »,   # join on newline and print

Current best attempt using canvas:

F„/\Nè©53NèΛ2®ð«4Λ
| improve this answer | | | | |
\$\endgroup\$
2
\$\begingroup\$

C++, 92 91 bytes

-1 bytes thanks to Kevin Cruijssen

void m(int n){for(int i=0,j;i<n;++i)for(j=0;j<n;++j)printf("%*c\n",i%2?j+1:n-j,i%2?92:47);}

Thanks to the power of the magic printf

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ You can put the int i=0,j in the for-loop for(int i=0,j;i<n;++i) to save a byte. \$\endgroup\$ – Kevin Cruijssen Sep 22 '17 at 11:10
  • \$\begingroup\$ 88 bytes \$\endgroup\$ – ceilingcat Jan 10 '19 at 21:33
2
\$\begingroup\$

Java (OpenJDK 8), 131 106 98 96 94 91 bytes

i->{for(int j=0;j<i*i;System.out.printf("%"+(j/i%2<1?i-j%i:j%i+1)+"c\n",47+45*(j++/i%2)));}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove quite a few parentheses: i->{for(int j=0;j<i*i;System.out.printf("%"+(j/i%2<1?i-j%i+1:j%i+2)+"s",j++/i%2<1?"/\n":"\\\n"));} (98 bytes). \$\endgroup\$ – Nevay Sep 22 '17 at 14:39
2
\$\begingroup\$

Dyalog APL, 39 36 35 34 bytes

{↑((,⍵ ⍵⍴(⌽,⊢)⍳⍵)/¨' '),¨⍵/⍵⍴'/\'}

Try it online!

1 byte saved thanks to Zacharý

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ Dang it, beat me by one byte. You can make ⎕IO be 0, and then remove ¯1+. \$\endgroup\$ – Zacharý Sep 24 '17 at 13:41
  • \$\begingroup\$ @Zacharý I was just about to do that :p \$\endgroup\$ – dzaima Sep 24 '17 at 13:42
  • \$\begingroup\$ Oh, one more thing: (⌽,⊢)⍳⍵ instead of (⌽⍳⍵),⍳⍵ \$\endgroup\$ – Zacharý Sep 24 '17 at 13:42
  • \$\begingroup\$ @Zacharý Yeah, I'm yet to understand the tacks, tacitness and stuff that comes with that :/ \$\endgroup\$ – dzaima Sep 24 '17 at 13:46
  • \$\begingroup\$ Don't worry, I don't fully understand how trains/forks/whatever-they're-called work either. \$\endgroup\$ – Zacharý Sep 24 '17 at 13:48
1
\$\begingroup\$

Perl 5, 70 + 1 (-n) = 71 bytes

$n=$_**2;$_=$"x--$_.'/';say&&s% /%/ %||s%\\ % \\%||y%\\/%/\\%while$n--

Try it online!

| improve this answer | | | | |
\$\endgroup\$
1
\$\begingroup\$

Kotlin, 102 bytes

(0..q-1).map{r->if(r%2<1)q-1 downTo 0 else{0..q-1}.map{(1..it).map{print(' ')};println('/'+45*(r%2))}}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 84 83 Bytes

Anonymous VBE immediate window function that takes input from range [A1] and outputs to the VBE immediate window

For i=1To[A1]:For j=1To[A1]:?IIf(i mod 2,Space([A1]-j)&"/",Space(j-1)&"\"):Next j,i
| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Jelly, 15 bytes

ḶṚ⁶ẋm0ż⁾/\x$ṁ²Y

Try it online!

Full program.

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Haskell, 86 85 bytes

f n=take(n*n)$cycle$[(' '<$[x..n-1])++"/"|x<-[1..n]]++[(' '<$[2..x])++"\\"|x<-[1..n]]

Try it online!

Saved one byte thanks to Laikoni

Repeat a zig ++ a zag, and take the first n*n lines.

| improve this answer | | | | |
\$\endgroup\$
  • \$\begingroup\$ cycle$ ... instead of cycle( ... ) saves a byte. \$\endgroup\$ – Laikoni Sep 22 '17 at 19:15
  • \$\begingroup\$ @Laikoni thanks! \$\endgroup\$ – jferard Sep 23 '17 at 7:31
0
\$\begingroup\$

J, 39 35 33 32 25 bytes

' /\'{~[,/@$(|.,:+:)@=@i.

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

Dyalog APL, 41 40 bytes

⎕IO must be 0.

{⍪/((⌽⍵ ⍵⍴S↑'/')(⍵ ⍵⍴'\'↑⍨S←⍵+1))[2|⍳⍵]}

Try it online!

| improve this answer | | | | |
\$\endgroup\$
0
\$\begingroup\$

D, 105 bytes

import std.stdio;void m(T)(T n){for(T i,j;i<n;++i)for(j=0;j<n;++j)printf("%*c\n",i%2?j+1:n-j,i%2?92:47);}

Try it online!

Lifted from HatsuPointerKun's C++ answer.

| improve this answer | | | | |
\$\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.