All credits to Adnan for coming up with this challenge.

My last challenge, before I go on break.


Given positive integer n, if n is odd, repeat / that many times; if n is even, repeat \ that many times.

(Seriously, the testcases would be much clearer than this description, so just look at the testcases.)



n output
1 /
2 \\
3 ///
4 \\\\
5 /////
6 \\\\\\

63 Answers 63

1 2

Thunno 2, 5 bytes



`\/i×  # Implicit input
`\/    # Two character string "\\/"
   i   # Modularly index in
    ×  # Repeat input times
       # Implicit output




Regenerate, 22 bytes


Attempt This Online!


 /                      Match a forward slash
  {         }           repeated this many times:
   1/(     )             Invert:
      $~1                 Command-line argument
         %2               mod 2
                         (For odd arguments, this gives one forward slash; for even
                         arguments, it fails due to division by zero)
             |          If the previous alternative failed,
              \\        match a backslash instead
(               ){$~1}  Repeat the above (command-line argument) times

Ly, 17 bytes


Try it online!

I tried a couple variations and this one was the shortest. It's uses the eveness/oddness of the number to pick the character from a list on the stack, then stashes it away before looping "N" times and printing the saved character. The stack ends up with junk on it so it explicitly exits to avoid printing it. That proved shorter than cleaning the stack.

'\'/               - push "\/" onto the stack
    n:             - get number "N" from STDIN and duplicate
      2%           - get 0|1 depending on whether it's even or odd
        I          - copy top "\" or one-down "/" entry from stack
         sp        - stash the char and delete
           [,  ]   - loop "N" times
             lo    - load char to print, print it
                ;  - exit to avoid printing junk on the stack
1 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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