# Print a 3D shape

Write a program or a function to print the following cube (if you allow me to call it so) in different sizes:

^L^L^L^L^L^L^L^L^L^L^L
//^L^L^L^L^L^L^L^L^L^L^L
////^L^L^L^L^L^L^L^L^L^L^L
//////^L^L^L^L^L^L^L^L^L^L^L
////////^L^L^L^L^L^L^L^L^L^L^L
//////////^L^L^L^L^L^L^L^L^L^L^L
////////////^L^L^L^L^L^L^L^L^L^L^L
//////////////^L^L^L^L^L^L^L^L^L^L^L
////////////////^L^L^L^L^L^L^L^L^L^L^L
//////////////////^L^L^L^L^L^L^L^L^L^L^L
////////////////////^L^L^L^L^L^L^L^L^L^L^L
\\\\\\\\\\\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\\\ " " " " " " " " " " "
\\\\\\\\ " " " " " " " " " " "
\\\\\\ " " " " " " " " " " "
\\\\ " " " " " " " " " " "
\\ " " " " " " " " " " "
" " " " " " " " " " "

The shape is made of pairs of characters: ^L, ", \\, //

### Input:

An integer greater than 1, representing the size of the shape.
The one showed above has a size of 11 because each side of each face is made of 11 pairs of characters.
Note that only two faces are fully displayed.

### Output:

A cube of the given size.

Here's another example, with a size of 5:

^L^L^L^L^L
//^L^L^L^L^L
////^L^L^L^L^L
//////^L^L^L^L^L
////////^L^L^L^L^L
\\\\\\\\ " " " " "
\\\\\\ " " " " "
\\\\ " " " " "
\\ " " " " "
" " " " "

This is , the shortest in bytes wins.

Even though you are not required to do so, it would be nice if you could provide an easy way to check the output of your code

• May we choose which characters to use, as long as all 8 are unique? – Shaggy Jan 23 at 20:22
• @shaggy I think it's better to stick with the original characters. It's true that the algorithm would be quite the same even using other characters, but you may lose the "3D illusion". So then it wouldn't be an algorithm to print a 3D shape anymore – Sheik Yerbouti Jan 23 at 20:42

# Python 2, 81 bytes

k=n=input()
while 1:k-=1;i=k^k/n;print' '*i+(n+~i)*2*'\/'[k/n-1]+'^ L"'[k<0::2]*n

Try it online!

Reuses some ideas from my answer on Draw an ASCII hexagon of side length n, including using the index k/n-1 both to select between / and \ in an order where the \ doesn't need escaping, as well as to terminate the loop with error at the end.

# Ruby, 78 bytes

->n{puts (0...n).map{|i|puts ' '*(j=n+~i)+?/*i*2+'^L'*n;' '*i+?\\*j*2+' "'*n}}

Builds an array for the bottom half while printing the top half, then dumps the bottom half at the end. I also tried recursion but it was longer.

Try it online!

• you can remove the space between puts and ' ' – Razetime Jan 24 at 14:42

# K (ngn/k), 69 bytes

{ 0:,/',/$(" ""//""^L";" ""\\\\"" \"")@'&''(t;|t:+(|!x;!x;x))} Try it online! Not a great fit for the language, but an interesting exercise... • (t;|t:+(|!x;!x;x)) build a nested list containing the number of copies of each type of symbol to take. (i.e. spaces from x..0,0..x, slashes from 0..x,x..0, and x copies of ^L and "). • &'' convert to indices, e.g. 3 1 5 => 0 0 0 1 2 2 2 2 2 • (" ""//""^L";" ""\\\\"" \"") build another nested list (of the same shape), this time containing symbol versions of the output characters. This avoids having to enlist the individual strings and simplifies the list notation a bit. Since they contain special characters (as opposed to being alphanumeric), the contents need to be enclosed in "'s, with " and \ also needing to be escaped. • (...)@'(...) retrieve the correct number of copies of each type of symbol • ,/',/$ stringify the symbols, and raze them into the lines of the output
•  0: print them (this elides the wrapping "s and \ escapes otherwise present)

# 05AB1E, 28 26 bytes

-2 bytes thanks to Kevin Cruijssen!

Without this bug, »∊¶¡ could be just .

L<·'/×»∊¶¡„^L„ "‚I×Iδи˜ø.c

Try it online!

Commented:

L<·'/×»∊¶¡      # generate the left part
L               # push the range [1..n]
<              # decrement to get [0..n-1]
·             # double each value: [0, 2, ..., 2*(n-1)]
'/           # push the string '/'
×          # for each number in the list, repeat the string this many times
»∊¶¡      # vertically mirror to get lower left half

„^L„ "‚I×Iδи˜   # generate the right part
„^L             # push the string '^L'
„ "          # push the string ' "'
‚         # pair into a list: ['^L', ' "']
I×       # repeat each string input times
Iδи    # for each string create a list containing the string input times
˜   # flatten into a single list

ø              # zip both parts to get the rows
.c           # join each row, centralize and join by newlines

Try it with step-by-step output!

Some slightly longer alternatives:

L<·'/×»∊¶¡εNI@i„ "ë„^L}I×«}.c
L<·'/×»∊¶¡ε„^LNI@i„ "}I×yì}.c
L<·Â«εNI@i'\×„ "ë'/×„^L}I×«}.c
• ‚øJ.c can be ø.c for -2, because the ø uses two arguments implicitly with a string-list in the new 05AB1E version (unlike the legacy, where it would transpose a string-list as if it were a character-matrix), and the .c joins implicitly. – Kevin Cruijssen Jan 25 at 7:37

# Java 11, 148 136 bytes

n->{String r="",t=r;for(int i=n;i-->0;t+=" ")r=t+"//".repeat(i)+"^L".repeat(n)+"\n"+r+t+"\\\\".repeat(i)+" \"".repeat(n)+"\n";return r;}

-12 bytes thanks to @OlivierGrégoire.

Try it online.

Explanation:

n->{                        // Method with integer parameter and String return-type
String r="",              //  Result-String, starting empty
t=r;               //  Temp-string, starting empty as well
for(int i=n;i-->0         //  Loop i in the range [input, 0):
;                     //    After every iteration:
t+=" ")              //     Append a space to t
r=...+r+...;            //   Change the result-String to:
//    Prepend to the result-String:
t                     //     The spaces t
+"//".repeat(i)       //     plus '//' repeated i-1 amount of times
+"^L".repeat(n)       //     plus '^L' repeated the input amount of times
+"\n"                 //     plus a newline
//    Append to the result-String:
t                     //     The spaces t again
+"\\\\".repeat(i)     //     plus '\\' repeated i-1 amount of times
+" \"".repeat(n)      //     plus ' "' repeated the input amount of times
+"\n";                //     plus a newline again
return r;}                //  After the loop, return the result-String
• 136 bytes, mostly changing t=" ".repeat(...) with t+=" " and rearranging the rest a bit. – Olivier Grégoire Jan 26 at 14:22

# Canvas, 27 22 bytes

｛╷２×/×^L⁸×＋］：↕vL∙ "╋∔ｒ

Try it here!

## Explanation

{|2×/×^L⁸×+]:↕vL∙ "╋∔r
{          ]           map each i from 1 to n:
|2×                   decrement and double i
/×                 repeat "/" that many times
^L⁸×             repeat "^L" n times
+            add that to get one line
:          copy the top half
↕         mirror vertically (mirrors characters)
vL∙" ╋   replace 'vL' with '" '
r center the whole art

# JavaScript (Node.js), 121 bytes

s=>(j=n=>' '[b="repeat"]((a=n>=s)?n-s:s+~n)+(a?"//":"\\\\")[b](a?s*2-n:n+1)+(a?"^L":' "')[b](s)+'\n'+(~n?j(--n):''))(s*2)

Try it online!

# Charcoal, 42 32 bytes

ＮθＧ⌈⊖θ\↓‖Ｃ‖Ｍ↑Ｆθ«Ｐ×^Lθ↘»Ｆθ«Ｐ×" θ↙

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

Ｎθ

Input the size.

Ｇ⌈⊖θ\↓

Print a small triangle of \s.

‖Ｃ‖Ｍ↑

Reflect it twice, once horizontally to complete the bottom left block of \s, once vertically with mirroring to produce the top left block of /s.

Ｆθ«Ｐ×^Lθ↘»

Print the top right block of ^s and Ls.

Ｆθ«Ｐ×" θ↙

Print the bottom right block of "s and spaces.

• Doesn't the vertical reflection change the slashes in backslashes? If so, wow! – Sheik Yerbouti Jan 23 at 18:02
• @Davide It depends on whether you use ‖Ｃ or ‖Ｍ, see my updated answer. – Neil Jan 23 at 18:06
• Oh that's powerful – Sheik Yerbouti Jan 23 at 18:13

# Python 3.8 (pre-release), 97 93 92 bytes

lambda a:sum(zip(*((' '*(x:=a+~i)+'//'*i+'^L'*a,' '*i+r'\\'*x+' "'*a)for i in range(a))),())

Try it online!

Output is a tuple of lines.

-4 by @Seb

-1 by @Danis

• You can save a byte by changing '\\\\' to r'\\', and 3 more by using Python 3.8 for its walrus operator – Seb Jan 24 at 3:51
• a-i-1 --> a+~1 save 1 byte – Danis Jan 24 at 11:35

# C (gcc), 160 135 130 bytes

Saved 5 bytes thanks to ceilingcat!!!

p(n,c){for(;n--;)printf(L"\n//\\\\L^ \"  "+c+n%2);}i;t;f(n){for(t=i=1;i;i+=t,t=i>n?i=n,-1:t)p(n-i,9),p(!p(2*n,6-t),p(2*~-i,2-t));}

Try it online!

### Explanation (before some golfs)

d;p(n,c){                             // p is a helper function that prints
// n/2 pairs of chars
for(d=0;n--;d^=1)                   // loop n times, flipping d between
//   0 and 1 each time
printf(L"                         // print one char of a pair of
//   chars based on c (then select the
//   other one on the next loop as d
//   flips between 0 and 1):
//                       //   c==0: '//'
\\\\                   //   c==2: '\\'
^L                 //   c==4: '^L'
\"              //   c==6: '" '
//   c==7: any number of spaces
\n"         //   c==9: p(1,9) prints a newline
+c+d);                   // use c and d to select char
}                                     // returns 0
i;t;f(n){                             // f is the main function
for(t=i=1;                          // main loop i goes from 1 to n and
// back down to 1 again
i;                                // loop until i is 0
i+=t,                             // bump i up for 1st half and then
// bump i down for 2nd half
t=i>n?i=n,-1:t)              // flip t from 1 to -1 half way thru
p(2*~-i,1-t),          // print left side slashes
p(2*n,5-t),            // print right side of cube
p(1,9);                // print newline
}                                     //
• This is shorter than the one I made before to post the challenge. Nice usage of printf! I need to learn that – Sheik Yerbouti Jan 24 at 12:33
• How does it work this L in printf, before the string? I tried to find out, but I get only results about the L in %L – Sheik Yerbouti Jan 24 at 16:15
• @Davide It's notation for a wchar_t string literal, – Noodle9 Jan 24 at 19:35
• Thank you so much for this information! – Sheik Yerbouti Jan 24 at 21:58
• @Davide So the way it works is the 8-bit chars are zero expanded when converted to 32-bit wchars. printf sees the zeros as end of strings so only prints the single char.that's pointed to by the pointer after the int offset is added. – Noodle9 Jan 24 at 22:47

# Jelly, 30 bytes

ḶṚżḤƊØ.xⱮ;€3,5ẋ$;ṚḤ$$ị“/\^"L ” A monadic Link accepting a positive integer which yields a list of lists of characters (a list of lines). Try it online! (The footer calls the Link and joins the resulting lines with newline characters.) ### How? ḶṚżḤƊØ.xⱮ;€3,5ẋ;ṚḤ$$ị“/\^"L ” - Link: integer, n e.g. 3 Ḷ - lowered range [0,1,2] Ɗ - last three links as a monad - f(that): Ṛ - reverse [2,1,0] Ḥ - double [0,2,4] ż - zip together [[2,0],[1,2],[0,4]] Ɱ - map with: Ø. - bits [0,1] x - repeat elements [[0,0],[0,1,1],[1,1,1,1]]$               - last two links as a monad - f(n):
3,5                 -   three pair five        [3,5]
ẋ                -   repeat (n times)       [3,5,3,5,3,5]
;€                    - concatenate each = x     [[0,0,3,5,3,5,3,5],[0,1,1,3,5,3,5,3,5],[1,1,1,1,3,5,3,5,3,5]]
$- last two links as a monad - f(x):$           -   last two links as a monad - f(x):
Ṛ             -     reverse              [[1,1,1,1,3,5,3,5,3,5],[0,1,1,3,5,3,5,3,5],[0,0,3,5,3,5,3,5]]
Ḥ            -     double               [[2,2,2,2,6,10,6,10,6,10],[0,2,2,6,10,6,10,6,10],[0,0,6,10,6,10,6,10]]
;              -   concatenate            [[0,0,3,5,3,5,3,5],[0,1,1,3,5,3,5,3,5],[1,1,1,1,3,5,3,5,3,5],[2,2,2,2,6,10,6,10,6,10],[0,2,2,6,10,6,10,6,10],[0,0,6,10,6,10,6,10]]
“/\^"L ” - list of characters = '/\^"L '
ị         - index into (1-indexed & modular, i.e. 10->")
-> ['  ^L^L^L',
' //^L^L^L',
'////^L^L^L',
'\\\\ " " "',
' \\ " " "',
'   " " "',
]

# Stax, 27 bytes

ü╚lTà╨ú%Ñ►F↔33j♦ü♂D▄P;L.⌂î↑

Run and debug it

# PHP, 134 116 bytes

for($f=str_repeat;$i<$n=$argn;)$s=($t=$f(" ",$i++).$f("//",$n-$i).$f("^L",$n)." ").$s.strtr($t,'/^L','\\ "');echo$s;

Try it online!

Yes, much shorter to build the string..

EDIT: a huge 18 bytes saved thanks to optimization from Dom Hastings

Old version kept just for the fun:

# 157 bytes

for($f=str_repeat;$i<2*$n=$argn;$s.='//')$i++<$n?printf("%".($n-2+$i)."s%s",$s,$f("^L",$n)."
,{$l='//';'^L'|g;$i++}*$n+,{$l='\\';--$i;' "'|g}*$n|%{.$_} # Wolfram Language (Mathematica), 172 bytes (m=MapThread[Join,{DiamondMatrix[#][[1;;#+1,1;;#]],2BoxMatrix[#][[1;;#+1,1;;#+1]]}];Print/@StringJoin@@@(Join[m,4Reverse[m]]/.{0->" ",1->"//",2->"^L",4->"\\\\",8->" \""}))& Try it online! Brief explanation: Construct the upper part m with DiamondMatrix and BoxMatrix: m = MapThread[Join, {DiamondMatrix[n][[1 ;; n + 1, 1 ;; n]], 2BoxMatrix[n][[1 ;; n + 1, 1 ;; n + 1]]}] Derive the whole matrix M from m: M = Join[m, 4*Reverse@m] you can visualize it with: MatrixPlot[M, Frame -> None] when n = 10, it's like this: Replace each number from the matrix with corresponding string: M/.{0->" ",1->"//",2->"^L",4->"\\\\",8->" \""} Convert each row to string and print it: Print/@StringJoin@@@% # Zshcommit 2a96748, 123 121 119 bytes alias R=repeat R "i=a=$1" {s=${(l:--i:)};R b++ s+=//;R a s+=^L;<<<$s}
R a {s=${(l:i++:)};R a-i s+='\\';R a s+=' "';<<<$s}

Try it online!

Try it online!

# Lua (LuaJIT), 122 bytes

r=n.rep;for i=1,n*2 do print(i<=n+0 and r(' ',n-i)..r("/",i*2-2)..r('^L',n)or r(' ',i-1-n)..r("\\\\",n*2-i)..r(' "',n))end

Try it online!

(Above code abuses the TIO's input string-type to save 1 byte, see Not abused version)

# Perl 5 + -M5.10.0 -pa, 71 bytes

say,$\=y'/^L'\ "'r.$/.$\for map$"x("@F"-$_--)."//"x$_."^L"x"@F",1..\$_}{

Try it online!

# Julia, 79 73 bytes

output is a list of lines

N->[" ".^-(a=-N+1:0).*"//".^(b=0:N-1).*"^L"^N;" ".^b.*"\\".^-2a.*" \""^N]

Try it online!

• If you can print one space less on the left. But anyway yes, you did it – Sheik Yerbouti Jan 27 at 15:16
• I can but it will cost me a few bytes – MarcMush Jan 27 at 15:29
• Don't worry about a few bytes, the important thing is that you managed to properly reproduce the shape in as few bytes as your language allow you to do. – Sheik Yerbouti Jan 27 at 17:31
• @Davide done, and I even managed to shave a few more bytes – MarcMush Jan 28 at 9:18