# 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.)

# Testcases

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


# Javascript, 22 bytes

n=>"\\/"[n%2].repeat(n)


Defines an anonymous function.

If only * repeated strings in Javascript. sighs

• Finally you have found the right operator – Leaky Nun Aug 30 '16 at 23:21
• @LeakyNun What are you talking about? – DanTheMan Aug 30 '16 at 23:22
• You were using the ternary operator to choose the character, no? – Leaky Nun Aug 30 '16 at 23:22
• @LeakyNun Initially, yes, but if you look at chat, I also posted this about a minute later. – DanTheMan Aug 30 '16 at 23:23
• @Jordumus You could either assign the function to a variable: f=n=>..., you could directly call it: (n=>...)(5). (Or if you are using the Node.js REPL, then you could use this._, which stands for the last thing entered) – DanTheMan Sep 1 '16 at 14:22

## Python, 20 bytes

lambda n:'\/'[n%2]*n

• Great, simple and elegant ^_^ – ABcDexter Sep 2 '16 at 13:19

# 05AB1E/2sable, 15119 8 bytes

DÈ„/\sè×


-2 bytes thanks to Leaky Nun

-1 byte thanks to Emigna

• ftfy – Leaky Nun Aug 31 '16 at 0:21
• It says sè× 😮 – DanTheMan Aug 31 '16 at 4:56
• "/\" can be „/\ – Emigna Aug 31 '16 at 7:06

# Perl, 20 bytes

Includes +1 for -p

Run with input on STDIN:

squigly.pl <<< 6


squigly.pl

#!/usr/bin/perl -p
T/\\^(..)+$ Try it online (First line added to allow multiple testcases to be run). # C#, 42 bytes string f(int n)=>new string("\\/"[n%2],n);  Selects the correct character, then creates a new string consisting of that character repeated n times. # PHP, 38 bytes for(;$i++<$a=$argv[1];)echo'\/'[$a%2];  (variant 38 bytes) while($i++<$a=$argv[1])echo'\/'[$a%2];  (variant 38 bytes) <?=str_pad('',$a=$argv[1],'\/'[$a%2]);


(variant 40 bytes)

<?=str_repeat('\/'[($a=$argv[1])%2],$a);  # C, 40 bytes i;f(n){for(i=n;i--;)putchar(n%2?47:92);}  Try it on Ideone • putchar(92-n%2*45) is the same length. – mbomb007 Sep 2 '16 at 13:46 # J, 10 bytes #'\/'{~2|]  This is a six-train verb, consisting of: # ('\/' {~ 2 | ])  This is a hook between # and ('\/' {~ 2 | ]); a hook (f g) y expands to y f (g y), so this expands to y # (... y), which, for single-characters, yields a list of y characters. The second part is a 5-train, consisting of: '\/' {~ 2 | ]  This evaluates to two forks: '\/' {~ (2 | ])  The inner fork, 2 | ], is modulus two. The outer fork, therefore, is: '\/' {~ mod2  Which takes ({~) the mod2 index (mod2) from the string /. Then, using the hook from above: y # (apt char)  This yields what we want, and we are done. ## Haskell, 25 bytes f n=cycle"\\/"!!n<$[1..n]


-1 byte thanks to Damien with cycle.

• f n=cycle"\\/"!!n<$[1..n] – Damien Aug 31 '16 at 6:20 • @Damien Wow, how did I forget cycle. – xnor Aug 31 '16 at 6:22 • I don't know. But I'm glad to have had the opportunity to "beat" you once :) – Damien Aug 31 '16 at 6:31 # Mathematica, 3432 28 bytes If[OddQ@#,"/","\\"]~Table~#&  Anonymous function. Takes an integer as input and returns a list of characters as output. • You can use ~Table~#. – Martin Ender Aug 31 '16 at 6:37 • I think it was added in 10.2. – Martin Ender Aug 31 '16 at 10:09 • Also, I think you can ditch the <>"" and return a list of characters. – Martin Ender Aug 31 '16 at 10:11 • @MartinEnder They must have also added ~Do~Infinity and such in 10.2+ as well... – LegionMammal978 Aug 31 '16 at 11:35 # Jelly, 5 bytes ị⁾/\x  ị⁾/\x ị⁾/\ modular-indexing into the string "/\" x repeat  # Powershell, 30 27 bytes Update: param($n)('\','/')[$n%2]*$n


Switching to param, thanks to timmyd.

"$("\/"[$args[0]%2])"*$args[0]  or slightly more readable ("\","/")[$args[0]%2]*$args[0]  Test: > 1..10 | % { ./run.ps1$_ }
/
\\
///
\\\\
/////
\\\\\\
///////
\\\\\\\\
/////////
\\\\\\\\\\

• Welcome to PPCG! Nice to see another PowerShell user around here. You can shave a few bytes by taking input as param($n) instead of $args, like follows for 27 bytes -- param($n)('\','/')[$n%2]*$n – AdmBorkBork Aug 31 '16 at 12:29 # Mathematica, 29 bytes "\\"["/"][[#~Mod~2]]~Table~#&  Cruelly exploits the fact that [[1]] returns the first argument of a function while [[0]] returns the function (head) itself, applied to the strangely valid function named "\\" which is being "evaluated" at "/". • It's slightly less weird when you consider that something of the form a[b] is just a generic expression with head a (index 0) and element b (index 1), and functions are just special kinds of expression (actually, it would be more correct to say that functions aren't expressions at all, but are simply rules for transforming expressions which usually have form f[x...]). :) – Martin Ender Aug 31 '16 at 10:16 • I've seen many languages abused on this site, but I think this is the first abuse of Mathematica that I've seen. Good job! – DanTheMan Aug 31 '16 at 13:43 # Ruby, 15 bytes ->n{'\/'[n%2]*n}  See it on eval.in: https://eval.in/632030 # Fourier, 27 bytes 92~SI~N%2{1}{47~S}N(Sai^~i)  Try it online! # Brachylog, 15 bytes :2%:"\/"rm:?jbw  Try it online! ### Explanation :2% Input mod 2… :"\/"rm …is the index of the element in string "\/",… :?j …element that we juxtapose Input times to itself… bw …and print to STDOUT after removing one slash/backslash  # CJam, 9 bytes ri_"\/"=*  Try it online! ### Explanation ri e# Read input and convert to integer N. _ e# Duplicate N. "\/"= e# Use N as cyclic index into "\/", giving '\ for even and '/ for odd inputs. * e# Repeat N times.  # ><> (Fish), 30 Bytes :2%?'/'o1-:?!;30. 30. >'\'50p  First time using this language, but I think I at least saved a little room by conditionally using the / as either part of the output or a mirror to redirect flow. Probably still horribly inefficient though, I feel like it could probably be cut down a little more at the very least. Input is the initial stack, output is stdout Try it online! • Welcome to Programming Puzzles & Code Golf! – Dennis Sep 1 '16 at 3:22 • @Dennis Thanks! I appreciate the welcome. – Callum Kerr Sep 1 '16 at 4:28 # Dyalog APL, 11 bytes Requires ⎕IO←0 which is default on many systems. ⊢⍴'\/'⊃⍨2|⊢  ⊢ the argument ⍴ reshapes (repeats) '\/'⊃⍨ the string "/" selected by 2|⊢ the division remainder when the argument is divided by two TryAPL online! • Cool! Very similar to J. – Conor O'Brien Aug 31 '16 at 0:04 • @ConorO'Brien Yes, the only difference is that 2-trains in J are hooks, while they are atops in Dyalog, so an explicit left tine is needed. – Adám Aug 31 '16 at 0:13 • Ah, I was wondering why the argument was there. – Conor O'Brien Aug 31 '16 at 0:31 • Finally an APL answer with all characters rendered correctly for me! – Cyoce Aug 31 '16 at 15:21 • @Cyoce Yeah, I wish we could specify (and embed) fonts on SE. – Adám Aug 31 '16 at 15:44 # Java 7, 68 65 bytes void c(int i){for(int x=0;x++<i;)System.out.print(i%2<1?92:'/');}  3 bytes saved thanks to @user902383 and @SeanBean. Just like with this answer, the shortest code-golf seems to loop and print. Both recursive and void c(int i){System.out.print(new String(new char[i]).replace("\0",i%2<1?"\\":"/"));} seems to be longer. Ungolfed & test code: Try it here. class M{ static void c(int i){ for(int x = 0; x++ < i;){ System.out.print(i % 2 < 1 ? 92 : '/'); } } public static void main(String[] a){ for(int i = 0; i < 10; i++){ c(i); System.out.println(); } } }  Output: / \\ /// \\\\ ///// \\\\\\ /////// \\\\\\\\ /////////  • Hi Kevin. Why not a Lambda Expression? – Vale Aug 31 '16 at 8:45 • @Vale Hi Vale. Because I'm an old-fashion Java 7 programmer. :) If you have a Java 8 or 9 answer that is slightly different than my answer, feel free to post it. – Kevin Cruijssen Aug 31 '16 at 8:58 • @Vale (he's odd like that :P) – Shaun Wild Aug 31 '16 at 9:24 • i think if you change x=-1;++x<i to x=0;x++<i you can reduce by one byte – user902383 Aug 31 '16 at 10:00 • Also you can replace "\\" : "/" with 92:'/' ;) – Shaun Wild Aug 31 '16 at 10:24 # R, 47 46 bytes n=scan();cat(rep(c("\\","/")[n%%2+1],n),sep="")  In R, you have to escape backslashes. the argument sep also has to be fully specified since it comes after .... Thus annoyingly few opportunities to save chars :( Thanks to bouncyball for golfing away a byte. • Save one byte by using indexing: n=scan();cat(rep(c('\\','/')[n%%2+1],n),sep='') – bouncyball Aug 31 '16 at 20:54 # T-SQL 50 bytes Of course no STDIN here, so let's assume a hardcoded INT variable like this: DECLARE @ INT then the solution is: PRINT IIF(@%2=0,REPLICATE('\',@),REPLICATE('/',@))  ## Pip, 8 bytes "\/"@aXa  Straightforward. Uses modular indexing to select the character and string repetition to multiply it. Try it online! This question presents an interesting comparison between Pip, Pyth, and Jelly, the latter two each having scored 5 bytes. All three languages have implicit output, with single-char operators for modular indexing and string repetition, and no requirement to escape backslashes in strings. There are two key differences, though: 1. Under certain circumstances, Pyth and Jelly need only one delimiter to define a string; 2. Pyth and Jelly have syntax such that the input doesn't need to be explicitly represented in the code (though for very different reasons, as Maltysen explained to me). Neither one of these features is likely to show up in Pip1 (I don't like the aesthetics of unbalanced delimiters, and point-free syntax or implicit operands seem like they would be too alien to my infix expression parser), but I'm okay with playing third fiddle. Even though "readability" is extremely relative when golfing, I'd argue that those three extra bytes make the Pip program a lot easier to understand at a glance--and in my book, that's a worthwhile tradeoff. 1 Although, single-character strings in Pip use a single ' delimiter, inspired by CJam and by quoting in Lisp. • I'm not sure that readability is a plus, in code golf? Not at the cost of bytes ! – GreenAsJade Aug 31 '16 at 11:02 • @GreenAsJade I expect a lot of people feel the same. I would just make one distinction: code golf != golflang design. Now you may well argue that the same principle (shorter is always better) applies to language design too. I'm just saying that for me, usability and even aesthetics are considerations. – DLosc Aug 31 '16 at 15:59 • Pro tip for making a golfing language: don't use infix – Cyoce Aug 31 '16 at 22:50 • – DLosc Sep 1 '16 at 0:52 • pyth doesn't have implicit point free syntax. it works pretty much the same way it does in python, making it readable yet short. your comment about the unbalanced delimiters on the other hand is pretty true – Maltysen Sep 2 '16 at 4:26 # Pyth, 5 bytes *@"\/  Test suite. Modular-indexing into the string \/ and then repeat. • You're FGITW'ing your own challenge? – xnor Aug 30 '16 at 23:44 • @xnor it isn't exactly my challenge. – Leaky Nun Aug 30 '16 at 23:49 # Perl 6, 16 bytes {<\ />[$_%2]x$_}  ## Usage: for 1..6 { say$_, {<\ />[$_%2]x$_}( $_ ) }  1/ 2\\ 3/// 4\\\\ 5///// 6\\\\\\  # Julia, 20 bytes !x="$("/\\"[x%2])"^x


# SpecBAS - 28 bytes

1 INPUT n: ?"\/"(ODD(n)+1)*n


ODD returns 1 if number is odd, then uses that as an index to print the correct character n number of times. Have to add 1 as SpecBAS strings start at character 1.

# Java 8, 56 bytes

(i,j)->{for(j=i;j-->0;)System.out.print(i%2<1?92:'/');};


I'd like to thank @Kevin Cruijssen in advanced for golfing my answer further.

# Ungolfed Test Program

public static void main(String[] args) {
BiConsumer<Integer, Integer> consumer = (i, j) -> {
for (j = i; j-- > 0;) {
System.out.print(i % 2 < 1 ? 92 : '/');
}
};

consumer.accept(5, 0);
consumer.accept(1, 0);
consumer.accept(8, 0);
}


# Actually, 10 bytes

Golfing suggestions welcome. Try it online!

;'\'/2(%I*


How it works

       Implicit input of n
;      Duplicate n
'\'/   The strings "/" and "\"
2(%    n mod 2
I      If n mod 2, then "/", else "\"
*      Multiply by n. Implicit print at the end.
`