# 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? Jan 23, 2021 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 Jan 23, 2021 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.

# 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

# 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 ' ' Jan 24, 2021 at 14:42

# 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!

# 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. Jan 25, 2021 at 7:37

# PowerShell, 88 bytes

param($n)$n..1|%{($l=' '*--$_)+'//'*($j=$n-$_-1)+'^L'*$n
$b=,($l+'\\'*$j+' "'*$n)+$b}$b

Try it online!

# PowerShell, 104102 92 bytes, Alternative

param($n),{$l='//';'^L';$i++}*$n+,{$l='\\';--$i;' "'}*$n|%{.$_}|%{' '*($n-$i-1)+$l*$i+$_*$n}

Try it online!

The script generates the array of scriptblocks and executes its in the same context. Less golfed:

param($n)filter g{' '*($n-$i-1)+$l*$i+$_*$n} ,{$l='//';'^L'|g;$i++}*$n+,{$l='\\';--$i;' "'|g}*$n|%{.$_}

# 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. Jan 26, 2021 at 14:22

# 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! Jan 23, 2021 at 18:02
• @Davide It depends on whether you use ‖Ｃ or ‖Ｍ, see my updated answer.
– Neil
Jan 23, 2021 at 18:06
• Oh that's powerful Jan 23, 2021 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, 2021 at 3:51
• a-i-1 --> a+~1 save 1 byte Jan 24, 2021 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 Jan 24, 2021 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 Jan 24, 2021 at 16:15
• @Davide It's notation for a wchar_t string literal, Jan 24, 2021 at 19:35
• Thank you so much for this information! Jan 24, 2021 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. Jan 24, 2021 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)."
"):printf("%".(5*$n-$i)."s",$f('\\',4*$n-2*$i).$f(' "',$n)." "); Try it online! Here is an infamous PHP answer based on printf. Couldn't think of an uglier looking piece of code for now, but I'll work on it (starting to think it would be better to build the whole string)! • Nice! Good dedication to check the alternative as well! You can also save a few more bytes using strtr too: Try it online! Jan 26, 2021 at 10:30 • @DomHastings huge optimization! if you want to post it as your own answer be sure I'll upvote it, or else I'll edit. Yes for these kind of simple problems I like to try to think by myself first rather than just port other answers Jan 26, 2021 at 10:36 • Not at all, I was looking at a version but even using strtr I was at 133, so it's mostly your work! Please edit in! :) Yeah, I try to avoid looking at other approaches too, at least until I think I'm done! Jan 26, 2021 at 10:39 # 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!

This specific version fixes a bug that would otherwise require i=a=$1 to be in quotes. TIO doesn't have that version obviously, so the linked one is two bytes longer. # batch 317309301291284 bytes 276 bytes Cls&@Set$=@Set
%$%/Ah=%1*2,z=0 %$%F=@For /L %%a in (1 1 &%$%P=@^<nul set/p&%$%C= @If ,%%y LEQ %1
%f:a=y%!h!)Do @(%$%/Aw=%1-z %F%!w!)Do %P%=ESCC %F%!z!)Do%C%(%P%=//)Else %P%=\\ %F%%1)Do%C%(%P%=^^L)Else %P%^"=ESCC"" %C:EQ=SS%(%$%/A z+=1)Else%C:,=not %%$%/Az-=1 Echo() Cube.bat Notes: • must be run from the command line using: Echo(cube.bat n|Cmd /V:on where n is an integer representing cube size and cube.bat is the filename of the file • lines 1 and3 end wth trailing spaces • requires windows 10 with virtual terminal support • ESC is a stand in for the ANSI escape character, As it can't be pasted here, the scripts raw paste data can be found here. • Or it can be generated by opening the command prompt and typing: • >"%TEMP%\escape.chr" Echo(ALT+027 then pressing ENTER • Or output using a for loop with Echo off: • (For /F "skip=4 DELIMS=" %a in ('Echo(prompt$E^|cmd')Do Echo(%a) >"%TEMP%\escape.chr"

Vt sequence ESCC ('cursor right') is used to get around <nul set /p's inability to output leading spaces.

example output:

Ḷ,Ṛ$ạ’;,ʋ³ẋ@"ɗ€"“ “//“^L”,“ “\\“ "”ẎY 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) # GolfScript, 68 bytes ~:i,.-1%]zip.-1%]{{~" "*\["//""\\\\"]0=*+["^L"' "']0=i*+}%1:0;n*}%n* Try it online! Its really messy, but it works and I don't really know a better way to do it. # Excel, 114 bytes =LET(x,A1,q,SEQUENCE(2*x),b,q>x,r,IF(b,q-x-1,x-q),REPT(" ",r)&REPT(IF(b,"\\","//"),x-r)&REPT(IF(b,"""""","^L"),x)) Link to Spreadsheet # Regenerate, 84 bytes (( {#2-1}| {$~1-1})(//$3|)(^L){$~1}\n){$~1}(($6|)(\\{#7-2}|\\{#3})( "){$~1}\n){$~1}

Takes the size as a command-line input. Try it here!

### Explanation

Spaces are replaced with underscores for improved visibility.

Top half:

(( {#2-1}| {$~1-1})(//$3|)(^L){$~1}\n){$~1}
(                                    )       Group 1: one row of the top half
(                )                           Group 2: leading spaces
_{#2-1}                                      Either one less space than the previous row
|_{$~1-1} or (input-1) spaces if this is the first row ( ) Group 3: slashes //$3                       Either the previous row's slashes plus two more
|                      or empty string if this is the first row
(^L)                Group 4: ^L
{$~1} Repeat group 4 (input) times \n Newline {$~1}  Repeat group 1 (input) times

Bottom half:

(( $6|)(\\{#7-2}|\\{#3})( "){$~1}\n){$~1} ( ) Group 5: one row of the bottom half ( ) Group 6: leading spaces _$6                                        Either the previous row's spaces plus two more
|                                       or empty string if this is the first row
(               )                    Group 7: backslashes
\\{#7-2}                             Either two fewer slashes than the previous row
|\\{#3}                      or the same number of slashes as in the last match
of group 3 if this is the first row
(_")                Group 8: space-quote
{$~1} Repeat group 8 (input) times \n Newline {$~1}  Repeat group 5 (input) times

# 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 69 bytes

output is a list of lines

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

Try it online!

• If you can print one space less on the left. But anyway yes, you did it Jan 27, 2021 at 15:16
• I can but it will cost me a few bytes Jan 27, 2021 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. Jan 27, 2021 at 17:31
• @Davide done, and I even managed to shave a few more bytes Jan 28, 2021 at 9:18

# Vyxalj, 26 bytes

ʁd\/*m‛^L‛ ""?*ƛw?ẋ;fZvṅøĊ

Try it Online!

# Japt-R, 31 bytes

Æç'Li^)i'/pXÑÃû
c¡Xd/\\^ L"Ãw

Try it

Æç'Li^)i'/pXÑÃû\nc¡Xd...Ãw     :Implicit input of integer U
Æ                                :Map each X in the range [0,U)
ç                               :  U times repeat
'Li^                           :    "L" prepended with "^"
)                          :  End repeat
i                         :  Prepend
'/p                      :    "/" repeated
XÑ                    :    X*2 times
Ã                   :End map
û                  :centre pad each element with spaces to the length of the longest
\n                :Reassign to U
c               :Concatenate
¡              :  Map each X in U
Xd...       :    For each pair of characters in the string, replace all occurrences of the first in X with the second
Ã      :  End map
w     :  Reverse
:Implicit output joined with newlines