# Draw an ASCII-art rainbow

Draw an ASCII-art rainbow!

# Input

An integer between 0 and 1000, inclusive.

# Examples

Input 0:


/\
||


Input 1:

 _
/ \
| |


Input 2:

 __
/  \
|  |


Input 3:

 ___
/   \
|   |


The first row is a space followed by n underscores.

The second row is a slash followed by n spaces, and a backslash.

The third row is a pipe followed by n spaces, and a pipe.

Trailing spaces are allowed.

This is . Shortest code wins.

• May we take input as 0009 or 0561 instead of 9 and 561 if it is less than 1000? – Dion Aug 3 '20 at 16:10
• @Dion Yes, that is acceptable. – nph Aug 3 '20 at 16:11
• I'd call this an arch, not a rainbow – Noone AtAll Aug 4 '20 at 13:05
• @NooneAtAll A rainbow is an arch, isn't it? – user96495 Aug 6 '20 at 2:24
• it's Π house shaped – Tony Stewart Sunnyskyguy EE75 Aug 6 '20 at 16:00

# i386 (16-bit mode) Bootsector, 512 bytes

All i386 bootsectors have to be 512 bytes, so... Without the padding required by i386, it is 137 bytes. Since I can't easily post a TIO link, here's a screenshot of it running:

This code registers the BIOS interrupt int 0x69 to take the number in the bx register and use it as input. Thus, doing

mov bx, 3
int 0x69


is equivalent to the last test case. The disassembled, Intel-syntax source is:

[org 0x7c00]

xor ax, ax
mov es, ax
cli
mov dx, interrupt
mov [es:0x69*4], dx
mov ax, cs
mov [es:0x69*4+2], ax
sti

xor bx, bx
int 0x69
call newline
mov bx, 1
int 0x69
call newline
mov bx, 2
int 0x69
call newline
mov bx, 3
int 0x69
cli
hlt

interrupt:
or bx, bx
jz special
push bx
mov ax, 0x0e20
int 0x10
mov al, '_'
.loop1:
int 0x10
dec bx
jnz .loop1
call newline
mov al, '/'
int 0x10
mov al, ' '
pop bx
push bx
.loop2:
int 0x10
dec bx
jnz .loop2
mov al, '\'
int 0x10
call newline
mov al, '|'
int 0x10
pop bx
mov al, ' '
.loop3:
int 0x10
dec bx
jnz .loop3
mov al, '|'
int 0x10
iret

newline:
mov ax, 0x0e0d
int 0x10
mov al, 0x0a
int 0x10
ret

special:
mov si, s
mov ah, 0x0e
.sloop:
lodsb
int 0x10
or al, al
jnz .sloop
iret

s: db '/', '\', 10, 13, '|', '|'

times 510-($-$$) db 0 dw 0xaa55  (Bit verbose, I know, but hey, it's assembly.) # Charcoal, 11 bytes ↑|↗/×_Ｎ¶\¶|  Try it online! Link is to verbose version of code. This shape is too simple to tax Charcoal's drawing primitives, so this is just basically printing strings to the canvas. Explanation: ↑|  Print the left | and move the cursor up a line. ↗/  Print the / and move the cursor to the start of the _s. ×_Ｎ  Print the desired number of _s. This leaves the cursor just to the right of the last _. ¶\¶|  Move down a line, print a \, and print a | directly beneath. Just for fun I thought I'd write a 25-byte version which allows you to vary the height and thickness too: ＮθＮηＵＯηＮ|↑Ｇ→↑η/↗ＵＯθη_‖ＢＯθ  Try it online! Link is to verbose version of code. Takes input as inner width, thickness and inner height. Note that Charcoal does not support zero-sized boxes, so all of the inputs need to be at least 1. • First example is missing the link! I looked at fixing it but I was too tired to realise the lower link was different... – Dom Hastings Aug 5 '20 at 5:33 • @DomHastings Thanks, I've edited the link back in now. (Thank goodness for edit history!) – Neil Aug 5 '20 at 8:49 # Rust, 76575351 45 bytes -4 bytes thanks to madlaina |n|print!(" {:_<1$}
/{0:1$}\\ |{0:1$}|","",n)


Try it online

|n|format!(" ")+&"_".repeat(n)+"
/"+&" ".repeat(n)+"\\
|"+&" ".repeat(n)+"|"


Try it online

Just the boring answer. Borrows are needed to satisfy the type checker, unfortunately.

• You can save 4 bytes by leaving out the space and < in the format strings for the spaces, as left-aligned, space-padding is already the default :) – madlaina Aug 3 '20 at 18:25

# APL (Dyalog Unicode), 21 bytes (SBCS)

Full program, prompting for n from stdin.

' /|',' \|',⍨3↑1⎕⍴'_'


Try it online!

'_' an underscore

1⎕⍴'_' cyclically reshaped into a matrix with one row and n columns

3↑ take the first three rows, padding with spaces

' \|',⍨ append this character-list as a trailing column

' /|', prepend this character-list as a leading column

# Python 2, 47 bytes

lambda n:" %s\n/%s\\\n|%s|"%("_"*n," "*n," "*n)


Try it online!

Inserting the n-dependent components into a template using string formatting.

48 bytes

lambda n:" "+"_"*n+"\n/"+" "*n+"\\\n|"+" "*n+"|"


Try it online!

Straight up concatenation.

• Outgolfed by @Caagr98. – Dingus Aug 12 '20 at 13:53

# 05AB1E, 26 21 bytes

### My original 26 bytes approach:

" 0 /1\|1|"3ô„_ vy¹иJNs:}»


Explanation:

" 0 /1\|1|"3ô„_ vy¹иJNs:}»
" 0 /1\|1|"                   push string template (the idea is to replace 0 and 1 with _ and space respectively using loop count index
3ô                 split by chunks of 3
„_               push 2 chars _ and space
v        }     for each character in the previous string
y¹и           repeat it by the number in input
J          join the new string
Ns        push the index before that string to easily replace
:       replace in the string template
»    join by new lines


Try It Online

„_ S×ā" 1/2|2"2ô.º»r‡


Try It Online

• " 0 /1\|1|"3ô can be " 0/1|1"2ô.º for -1. And иJ has a builtin × for another -1. – Kevin Cruijssen Aug 4 '20 at 9:27
• You can also do the » before the loop so you can remove the } for another -1. – Kevin Cruijssen Aug 4 '20 at 9:33
• Taking it one step further, you can remove the loop entirely by using 12 instead of 01 and the builtin ā combined with a transliterate ‡: " 1/2|2"2ô.º»„_ SI×ās‡. Which can be reduced by another byte by using the implicit input with a reverse: „_ S×ā" 1/2|2"2ô.º»r‡ (21 bytes), which still uses your original approach, except without loop. :) PS: I've also posted a 17-byter 05AB1E answer just yet with a different approach. – Kevin Cruijssen Aug 4 '20 at 9:37
• Thanks for those great comments :) will edit with your notes. – SomoKRoceS Aug 4 '20 at 18:09

# C (gcc), 72 70 bytes

-2 bytes thanks to ceilingcat!

f(n){printf(" %s\n/%*c\n|%*2$c",memset(calloc(n,2),95,n),n+1,92,'|');}  Try it online! # MAWP 1.1, 95 bytes %@~52WWM52WWM52WWM84W;![1A99M1M5W;]%52W;68W1A;![1A84W;]%68W2W4A;52W;56W1M4W;![1A84W;]%56W1M4W;.  Try it! Explanation: % Remove initial 1 from stack @~ Push input as integers and reverse stack 52WWM Add top of stack multiplied by 10 to second stack element [5,4,3,2] ==> [5,4,23] 52WWM52WWM Two more times [5,4,3,2] ==> [2345] 84W; Print a space (8*4=32) ! Duplicate top of stack [ Start of loop 1A Subtract 1 99M1M5W; Print an underscore ((9+9+1)*5=95) ] End of loop. If result from subtraction doesn't equal to 0 jump to start of loop % Remove 0 52W; Print a newline (5*2=10) 68W1A; Print a slash (6*8-1=47) ![1A84W;]% Do the same loop as before but printing spaces instead 68W2W4A; Print a backslash (6*8*2-4=92) 52W; Print a newline 56W1M4W; Print a pipe ((5*6+1)*4=124) ![1A84W;]% Same loop as second 56W1M4W; Print a pipe .  # Integral, 37 31 Bytes ♂8♦⌡_⌡g►⌡/⌡•8g►⌡\⌡►•⌡|⌡♦8g►⌡|⌡►  First real answer in my new language! Try it Explanation: (Implicit input) ♂ Triplicate top of stack 8 Push space ♦ Swap ⌡_⌡ Push underscore g Repeat string ► Concatenate ⌡/⌡ Push slash • Stack: ...abc -> ...bca 8 Push space g Repeat string ► Concatenate ⌡\⌡ Push backslash • Stack: ...abc -> ...bca ⌡|⌡ Push pipe ♦ Swap 8 Push space g Repeat string ► Concatenate ⌡|⌡ Push pipe ► Concatenate  • While answering your own challenges is perfectly acceptable, I recommend giving people a week before you go ahead and do so. This is because, as the challenge author, you've already had plenty of time to think about the problem. – Adám Aug 3 '20 at 15:55 • @petStorm I added that. It is now v. – nph Aug 4 '20 at 11:58 • @Adám how does it stop people from submitting their own answers? Especially in a new language that almost nobody has seen before – Dion Aug 5 '20 at 7:45 # 05AB1E, 18 17 bytes …_ ε×… /|NèºSsý,  Explanation: …_ # Push string "_ " ε # Map over each character in this string: × # Repeat it the (implicit) input amount of times as string … /| # Push string " /|" Nè # Use the map-index to index into this string º # Mirror the character (" "→" "; "/"→"/\"; "|"→"||") S # Convert the string to a pair of characters s # Swap so the "_"/" "-string is at the top of the stack ý # Join the pair with this string as delimiter , # And output it with trailing newline  # COW, 420 bytes MoOMoOMoOMoOMoOMoOMoOMMMMoOMoOMoOmoOMMMMOOMMMMOOmoOMoOmOoMOomooMMMMOomoooommoOMoOMoOMoOMoOMooMMMmoOMMMMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMMMMOOmoOMoOMoOMOOmoOMoOmOoMOomoomOoMOomooMMMmoOmoOMMMmOoMMMMoOmOomOomOoMMMMOOmoOmoOmoOMoomOomOomOoMOomooMMMmOoMoomoOmoOmoOMoomOomOoMMMMOOmoOMoomOoMOomoomoOmoOmoOMOoMOoMOoMoomOomOomOomOoMoomoOMMMmoOMMMMOOmoOmoOMoOmOomOoMOomooMMMmoOmoOMMMMoomOomOomOoMOOmoOMoomOoMOomooMMMMoo  Try it online! Pretty happy with 140 instructions (3 bytes per instruction) considering that the ASCII values of the six required characters sum to 400. ### Commented MoOMoOMoOMoOMoOMoOMoO # push 7 to 1st memory block blocks: [[7]], register: nil MMM # copy to register blocks: [[7]], register: 7 MoOMoOMoO # add 3 to 1st block blocks: [[10]], register: 7 moOMMM # paste 7 to 2nd block blocks: [10, [7]], register: nil MOOMMMMOOmoOMoOmOoMOomooMMMMOomoo # set 3rd block to 28 = 7 + 6 + 5 + 4 + 3 + 2 + 1 blocks: [10, [0], 28], register: nil oom # read input n (e.g. 3) into 2nd block blocks: [10, [3], 28], register: nil moOMoOMoOMoOMoO # add 4 to 3rd block blocks: [10, 3, [32]], register: nil Moo # print ASCII character 32 (space) blocks: [10, 3, [32]], register: nil MMMmoOMMM # copy and paste 32 to 4th block blocks: [10, 3, 32, [32]], register: nil MoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoOMoO # add 15 to 4th block blocks: [10, 3, 32, [47]], register: nil MMMMOOmoOMoOMoOMOOmoOMoOmOoMOomoomOoMOomooMMM # add 2 to 6th block 47 times blocks: [10, 3, 32, [47], 0, 94], register: nil moOmoOMMMmOoMMM # copy and paste 94 to 5th block blocks: [10, 3, 32, 47, [94], 94], register: nil MoO # add 1 to 5th block blocks: [10, 3, 32, 47, [95], 94], register: nil mOomOomOoMMMMOOmoOmoOmoOMoomOomOomOoMOomooMMM # print ASCII character 95 (underscore) n times blocks: [10, [3], 32, 47, 95, 94], register: nil mOoMoo # print ASCII character 10 (newline) blocks: [[10], 3, 32, 47, 95, 94], register: nil moOmoOmoOMoo # print ASCII character 47 (forward slash) blocks: [10, 3, 32, [47], 95, 94], register: nil mOomOoMMMMOOmoOMoomOoMOomoo # print n spaces blocks: [10, [0], 32, 47, 95, 94], register: 3 moOmoOmoOMOoMOoMOo # subtract 3 from 5th block blocks: [10, 0, 32, 47, [92], 94], register: 3 Moo # print ASCII character 92 (backslash) blocks: [10, 0, 32, 47, [92], 94], register: 3 mOomOomOomOoMoo # print newline blocks: [[10], 0, 32, 47, 92, 94], register: 3 moOMMMmoOMMMMOOmoOmoOMoOmOomOoMOomooMMM # add 32 to 5th block blocks: [10, 3, [32], 47, 124, 94], register: nil moOmoOMMM # copy 124 to register blocks: [10, 3, 32, 47, [124], 94], register: 124 Moo # print ASCII character 124 (pipe) blocks: [10, 3, 32, 47, [124], 94], register: 124 mOomOomOoMOOmoOMoomOoMOomoo # print n spaces blocks: [10, [0], 32, 47, 124, 94], register: 124 MMMMoo # paste 124 to 2nd block and print as ASCII character blocks: [10, [124], 32, 47, 124, 94], register: nil  • That is some beefy source code you got there. – Razetime Aug 8 '20 at 14:53 • Now do Whirl – Engineer Toast Oct 16 '20 at 18:21 • This is truly beautiful – Dark Malthorp Oct 16 '20 at 18:58 # MATL, 20 bytes ' /|'4i:)B95*' \|'v!  Try it online! ### How it works ' /|' % Push this string: first column of the output, but as a row (*) 4 % Push 4 i:) % Implicitly input n; range; modular indexing. This gives a row vector % [4 4 ... 4] of length n B % Convert to binary. Each number gives a row in the output. So this % produces the matrix [1 0 0; 1 0 0; ...; 1 0 0] with n rows 95* % Multiply each entry by 95 (ASCII code of '_'). This gives the central % columns of the output, but transposed, as an n×3 matrix (**) ' \|' % Push this string: last column of the output, but as a row (***) v % Concatenate (*), (**) and (***) vertically ! % Transpose. Implicitly display  # Jelly, 19 bytes “_ ”×“ “/\“||”j"Y  A full program accepting a non-negative integer which prints the resulting ASCII art. Try it online! ### How? “_ ”×“ “/\“||”j"Y - Main Link: integer, n (e.g. 3) “_ ” - list of characters = ['_', ' ', ' '] × - multiply -> ['___', ' ', ' '] (now strings, a bit of a hack in Jelly) “ “/\“||” - list of lists of characters = [[' ', ' '], ['/', '\'], ['|', '|']] " - zip together applying: j - join -> [[' ', '___', ' '], ['/', ' ', '\'], ['|', ' ', '|']] Y - join with newlines -> [' ', '___', ' ', '\n', '/', ' ', '\', '\n', '|', ' ', '|'] - implicit, smashing print >>> ___ >>>/ \ >>>| |  # JavaScript (ES8), 50 bytes n=>${'_'.repeat(n)}
/${s=''.padEnd(n)}\\ |${s}|


Try it online!

# JavaScript (ES6), 50 bytes

n=>' '+_
/ \\
| |.replace(/_| /g,c=>c.repeat(n))


Try it online!

• @AryanBeezadhur ECMAScript, 8th edition. Also known as ECMAScript 2017, in which padEnd() was first introduced. – Arnauld Nov 8 '20 at 16:17

# Pyth, 24 bytes

%" %s
/%s\\
|%s|"*RQ"_


Try it online!

## Explanation

%"..."*RQ"_
"_    : The string literal "_  "
*RQ      : Multiply each element of the string by input (["___", "   ", "   "])
%"..."         : Do string formating on string with args from previous comamnd


# Japt, 20 bytes

" {ç'_}
/{ç}\\
|{ç}|


Try it

ç is one of Japt's repeat methods for integers; you can probably figure the rest out from there!

f n|x<-' '<$[1..n]=' ':('_'<$x)++"\n/"++x++"\\\n|"++x++"|"


Try it online!

# PowerShell, 43 41 bytes

-2 bytes thanks to mazzy

param($n)' '+'_'*$n
' '*$n|%{"/$_\
|$_|"}  Try it online! Eh, it's okayslightly better Alternative 41 byte solution offered by mazzy • – mazzy Aug 4 '20 at 20:14 • reductio ad absurdum: 41 bytes – mazzy Aug 4 '20 at 20:19 # Java 11, 61 bytes n->" "+"_".repeat(n)+"\n/x\\\n|x|".replace("x"," ".repeat(n))  Try it online. Explanation: n-> // Method with integer parameter and String return-type " " // Return a space +"_".repeat(n) // Appended with the input amount of "_" +"\n/x\\\n|x|" // Appended with " // /x\ // |x|", .replace("x", // of which the "x" are replaced with: " ".repeat(n)) // The input amount of spaces  # Python 3, 41 bytes lambda n:(f" /|{'_ '*n} \\|\n\n"*3)[::3]  Try it online! How could I forget about f-strings? # Python 3, 43 bytes lambda n:(" /|%s \\|\n\n"%("_ "*n)*3)[::3]  Try it online! Constructs the string transposed, then transposes it using modular arithmetic. • Welcome to the site! Your 43 byte solution also works in Python 2, so you should be able to claim this standing bounty for outgolfing @xnor's 47 byte answer. – Dingus Aug 8 '20 at 7:01 # Ruby, 36 bytes ->n{" #{?_*n} /#{a=" "*n}\\ |#{a}|"}  Try it online! # Scala, 47 bytes n=>print(s"${"_"*n}\n/${" "*n}\\\n|${" "*n}|")


## Longer version, 94 bytes

n=>print(Seq((' ',' ',"_"),('/','\\'," "),('|','|'," "))map(t=>t._1+t._3*n+t._2)mkString "\n")


Try them in Scastie

# C (gcc), 109 91 bytes

Saved a whopping 18 bytes thanks to ceilingcat!!!

#define p printf(L"| |\\ / _ "+i)
i;j;f(n){for(i=9;i--;i--,p,puts(""))for(i-=p,j=n;j--;)p;}


Try it online!

• @ceilingcat Very nice - thanks! :-) – Noodle9 Aug 4 '20 at 9:15

# Perl 5-p, 40 bytes

say$"._ x$_,$/,$_='/'.$"x$_.'\\';y/ /|/c


Try it online!

# Husk, 17 bytes

TJR⁰"_  "½" /| ¦|


Try it online!

## Explanation

TJR⁰"_  "½" /| ¦|   Input is a number, say n = 3, accessed via ⁰.
" /| ¦|   String literal " /| \|".
The parser replaces ¦ by \ and the closing " is implicit.
½          Split in half: x = [" /|", " \|"]
"_  "           String literal.
R⁰                Repeat n times: y = ["_  ", "_  ", "_  "]
J                  Join x by this list: [" /|", "_  ", "_  ", "_  ", " \|"]
Since x and y are lists of strings, y is inserted between
each pair of elements in x and the result is flattened
to keep the types consistent.
T                   Transpose: [" ___ ", "/   \", "|   |"]
Implicitly print, separated by newlines.


# MAWP v1.1, 92 bytes

%@_1A84W;[1A~25WWM~]%!![1A92W1M5W;]%67M;85W7M;[1A84W;]%45W3M4W;67M;65W1M4W;[1A84W;]65W1M4W;.


Try it!

# 1+, 174 bytes

."11+""*"**;1^<#[#(?|11##11+"1+""*""*+++;1+"\"/<1+1<1+#)]11+"""**+;11+"1+""""*++*+;1^<11++#(|?)[#(|11##11+""*"**;1+"\"/<1+1<1+#)]11+"1+"*""*++;11+"""**+;11+"*""1+""*+*+";()/;


Don't ask me how this work, I don't know at all!

I didn't yet try to golf it down (although I probably can offer a 50% discount on the bytecount when I have time), and there's still a lot of repetitions. But at least I made it.

1+ 174 vs 92 MAWP v1.1. Fiasco.

• They called me a madman for my MAWP... :D – Dion Aug 10 '20 at 11:06

# Pepe, 118 bytes

REeEeEEEEErEeErREEeeeEReREEEEEEeRREeeEeeeeeReeereeEreeeEeEEEEREEEEeeEREEeeeEReeereeEeEEEeereeERrEeEEEEEeerreEeReeereEe


Try it online!

• Ha you are the one who is no longer on Esolangs – null Aug 10 '20 at 13:18

# MATLAB/Octave, 36 32 bytes

@(n)[' /|';'_  '+~(1:n)';' \|']'


Try it online!

Simple anonymous function that stitches together the first column ( /|), n middle columns (_ ), and the final column ( \|), then returns the result.

Because the strings are rows rather than columns, everything is stitched together vertically, and then the result is transposed to get the desired orientation.

This works as trailing spaces are allowed, so a fully populated matrix can be formed. If trailing spaces were disallowed, the code would be 6 bytes longer by wrapping the result in trim(...).

• Saved 4 bytes by duplicating the string literal using array expansion rather than with repmat. Thanks @LuisMendo.
• @LuisMendo Interesting. Never come across that one. I assume it's Octave specific? – Tom Carpenter Sep 2 '20 at 17:12
• Matlab has implicit expanstion since Release 2016b. Octave has had that feature (called broadcasting, but it's the same) for longer, at least since version 3.6.0. So the code I suggested works both in Octave and in reasonably modern Matlab – Luis Mendo Sep 2 '20 at 17:41
• @LuisMendo interesting. A large part of my work ends up benig programming MATLAB stuff, and have somehow missed that feature. Cool to learn new things! – Tom Carpenter Sep 2 '20 at 19:18
• Ooooh! I've just realised what it's doing, on first glance it looked like the (1:n) bit was inside the string literal, but just realised its not. Yes that makes more sense now. – Tom Carpenter Sep 2 '20 at 19:21
• Yes, it's a cool feature, not just for golfing :-) – Luis Mendo Sep 2 '20 at 21:00

# Cinnamon Gum, 15 bytes

00000000: 70 05 1f 78 e5 2d 36 4e 0b 94 1d 5b d2 c7 fd


Try it online!