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 \\\\\\

43 Answers 43


Python 3, 43 Bytes

if n%2:a='/'*n

In python the \ symbol is also used as an operator when inside a string (e.g: \n means newline) so \\ has to be used to get the backslash symbol.

This code works by assuming the input (n) is odd, and then checks if it is even. If so, then it redefines the variable a (the output) before displaying it.

This is shorter than using an if-else statement by 5 bytes as it does the checking after defining the variable, meaning that the else command is not needed.

  • \$\begingroup\$ You could save bytes by changing to Python2, since int(input()) could be changed to input() and print wouldn't need parentheses. Also, there's an unnecessary whitespace in n%2: . \$\endgroup\$ – Yytsi Aug 31 '16 at 12:12
  • \$\begingroup\$ Also, you may hook the character from a string (selecting n%2-th) character, but that would end up being the same answer as @xnor has. It's a nice trick for the future! \$\endgroup\$ – Yytsi Aug 31 '16 at 12:16
  • \$\begingroup\$ Indeed, '\/'[n%2]*n is pretty much as short as it can be. \$\endgroup\$ – SylvainD Sep 1 '16 at 9:58
  • \$\begingroup\$ Thanks for the tips, I'll bear them in mind for next time \$\endgroup\$ – sonrad10 Sep 1 '16 at 11:25

PHP, 63 52 bytes

Version 1 - 63 bytes

$i=$argv[1];$x=($i%2?"/":"\\");echo sprintf("%'".$x.$i."s",$x);

Version 2 - 52 bytes

Thanks to manatwork for saving 11 bytes


Run with php -f squiggle.php <number>

https://repl.it/DFdi/2 (remember to change the input on repl)

  • 1
    \$\begingroup\$ No need for parenthesis around the ternary expression. Why using sprintf() to return a value just to echo it and not directly printf() it? And is shorter to use variable expansion than concatenating the format string: "%'$x{$i}s". \$\endgroup\$ – manatwork Sep 2 '16 at 13:28
  • \$\begingroup\$ @manatwork thanks for the great tips! I've modified my answer. \$\endgroup\$ – ʰᵈˑ Sep 2 '16 at 13:38

Bash + coreutils, 29 28 bytes

yes $1_45r2%*92+P|sed $1q|dc

Previous answer, easier to understand:

yes $[92-$1%2*45]P|sed $1q|dc

In the new script I just transferred the calculation of the ASCII code from bash to dc entirely.

  • \$\begingroup\$ @LeakyNun I was used with the Bash and pure Bash terms until I started here. I updated my answer. \$\endgroup\$ – seshoumara Aug 31 '16 at 14:39

S.I.L.O.S, 55 bytes

printChar c
if i a

Try it online!

Port of this answer in C.


Haskell, 32 bytes

f n=replicate n$"\\/"!!(mod n 2)

RETURN, 21 bytes


Try it here.

Anonymous lambda. Usage:



[                    ]  [lambda]%
 $2÷%                   [copy input and mod 2]%
     '/'\2øø            [choose either / or \ depending on input mod 2]%
            4ø          [get input again]%
              2␂{      [copy the /\ char and the input into a new stack]%
                  ␋},   [repeat the stack by the input and output the stack]%

Bash, 63 bytes

q(){ seq -s0 0 $1|cut -c-$1|tr 0-9 $(($1%2))|sed 'y/10/\/\\/';}
  • \$\begingroup\$ Weird. If you know the tr command, then why used sed for the 2nd transliteration? \$\endgroup\$ – manatwork Aug 31 '16 at 7:05
  • \$\begingroup\$ Move your code outside of the function and drop the q(){} part. Also, you can drop both tr and sed conversions by doing something like this: c=(\\ /);seq -s${c[$1%2]} 0 $1|tr -d [0-9]. For an even shorter way see my answer \$\endgroup\$ – seshoumara Aug 31 '16 at 14:29

Rust, 210 190 Bytes

fn main(){let x:usize=std::env::args().nth(1).unwrap().parse::<usize>().unwrap();let l:&str=match x%2{0=>"\\",1=>"/",_=>"",};println!("{}",std::iter::repeat(l).take(x).collect::<String>());}


fn main() {
    let x : usize = std::env::args().nth(1)
    let l : &str = match x % 2 {
        0 => "\\",
        1 => "/",
        _ => "",
    println!("{}", std::iter::repeat(l).take(x)

I don't usually golf (so I bet this isn't a great score, at least for Rust), but I figured this would be good Rust practice. This compiles into an executable run as squiggly.exe x where x is the number of times to repeat the character.

  • \$\begingroup\$ Cut off 20 bytes by realizing that direct referencing (std::env::args(), std::iter::repeat(l)) is more effective than use clauses \$\endgroup\$ – MutantOctopus Sep 1 '16 at 15:36

><>, 15 + 3 = 18 bytes


+3 bytes for the -v flag to initialise the stack with the input. Try it online!

><> is a toroidal 2D language, so the following runs in a loop:

\/          Mirrors that reflect the instruction pointer. Since the code is
            one row high, the reflection wraps and the IP ends up moving in the
            same direction anyway, so these are no-ops.

:l2-)       Push (input > length of stack - 1) - note that the actual subtraction
            is by 2 to account for the fact that we duplicated an element with :

,           Divide the input by the above. If (input == length of stack - 1),
            then the program errors out due to division by 0. Otherwise, the
            input is unchanged.

:2%0g       Push the char at (input % 2, 0), which will be one of the two slashes
            at the beginning of the code

o           Output this char

:           Duplicate the input, increasing the length of the stack by 1

awk, 26 bytes


$1 is the input and then part of the output

NF is the number of fields

OFS is the output field separator

It prints if the expression before the comma is truthy.


Forth, 58 54 bytes

Leaves n%2 as junk on the stack. Output is sent to stdout. 47 is /, and 92 is \. Computes y=92-45*x, where x is n%2 and emits (outputs) the character with that codepoint.

: f dup 2 mod swap 0 DO dup 92 swap 45 * - emit LOOP ;

Try it online


Lua (33 44 Bytes)

Never tried golfing with Lua before.

Shortest program I could come up with:


  • \$\begingroup\$ Your code should handle either input or parameter. \$\endgroup\$ – manatwork Sep 2 '16 at 13:31

PL-SQL (Oracle), 530 characters

-- Enter your input here, here example is taken as 3 ---
  n number := 3 ;
  k varchar2(20);
     -- Checking the input number for odd --
     if mod(n,2)=1 then      
     for j in 1..n loop 
      k := k||'/';           
      end loop; 
      DBMS_OUTPUT.PUT_line (n||' '||k); 
     end if;

     -- Checking the input number for even --
         if mod(n,2)=0 then
         for j in 1..n loop
         k := k||'\';         
          end loop;
          DBMS_OUTPUT.PUT_line (n||' '||k); 
         end if; 
  • \$\begingroup\$ Language name?. \$\endgroup\$ – acrolith Sep 2 '16 at 21:43
  • \$\begingroup\$ Welcome to PPCG! Please put what language this is and the length in bytes at the top of your post. Also, remove as many extraneous characters as possible, including whitespace. Posters must make a serious effort to golf (shorten) their code for [code-golf] questions. \$\endgroup\$ – mbomb007 Sep 2 '16 at 21:43

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.