Draw an ASCII Volume

Given a volume level V in the range 1-10. Your program should draw the following ASCII art.

When V=1:

\
|
/


When V=2:

  \
\  |
| |
/  |
/


When V=3:

    \
\  |
\  | |
| | |
/  | |
/  |
/


And etc. Trailing spaces are allowed.

This is a code-golf challenge so the shortest solution wins.

• Similar Feb 23, 2022 at 7:42

Vyxal, 17 bytes

ɾ\|*\\?ɾ꘍$YvøMøĊ§  Try it Online! This was quite a nice approach that may have ended up a bit clunky due to lack of overloads. The idea is simple: Create the rotated form:  \ / | \ / ||| \ / |||||  And rotate it 90° with the § builtin. We can create the above by interleaving the following:  | ||| ||||| \ / \ / \ /  And that's basically what this does, with a few convenient Vyxal builtins. So close to beating Charcoal...  * # Repeat... \| # "|" ɾ # By 1...n ?ɾ # 1...n ꘍ # Spaces appended to... \\ # "\"$Y       # Interleave them
vøM    # Mirror each line, leaving the middle alone and flipping slashes
øĊ  # Align each line to the center
§ # Rotate the whole thing 90°

• Excellent character choice for the rotation operator in vyxal Feb 23, 2022 at 14:22

Python 3.8 (pre-release), 169157144131989290 87 bytes

Thanks @ophact for helping me save some a lot of bytes

Thanks @alephalpha for helping me golf it down to under 100 bytes with a completely new strategy

Thanks @ emanresu A for another -8 bytes

lambda n:[print(*" "*~-abs(i)+(i!=0)*"\\/"[i>0],(n-abs(i))*" |")for i in range(-n,n+1)]


Try it online!

First time trying to golf in Python. There's probably tons of stuff to golf here :D

My answer has literally been golfed to death entirely by the efforts of other people (not me).

• 149 bytes For the future: use list comprehensions, remove spaces, use operators in such a way that you can remove spaces around the expressions (for instance, no space needed between a string literal and a keyword). Feb 23, 2022 at 8:11
• @ophact thanks for the tips! Feb 23, 2022 at 8:21
• 131 bytes by switching to lambda and making use of python's := (walrus) operator. The lambda mode is usually much shorter than def... [...] return ... Feb 23, 2022 at 8:25
• @alephalpha 85 bytes as full program in Python 2.
– ovs
Feb 23, 2022 at 8:52
• Don't get demotivated, use this as a learning experience. If you decide to continue golfing in Python, you can use all the tips we've taught you. Feb 23, 2022 at 9:27

Python 2, 78 bytes

n=i=input()
while~n-i:x=abs(i);print('  '*x+'\/'[i<0]+' ')[2:],'| '*(n-x);i-=1


Try it online!

JavaScript (ES6), 92 bytes

This builds the output character per character. (So it may be longer than generating the strings explicitly, but it's a little more fun. ^^)

n=>(y=n,g=x=>y<-n?'': |\\/
[d=y*y-x*x++/4,x>n*2?(y--,x=0,4):x&1?d-x?0:2^y<0:d<0|0]+g(x))


Try it online!

How?

For each position $$\(x,y)\$$ with $$\0\le x<2n\$$ and $$\-n\le y \le n\$$, we compute:

$$d=y^2-x^2/4$$

For $$\n=3\$$, this gives:

$$\begin{array} {|c|c|} \hline & 0 & 1 & 2 & 3 & 4 & 5 \\ \hline 3 & 9 & 8.75 & 8 & 6.75 & \color{red}{5} & 2.75 \\ \hline 2 & 4 & 3.75 & \color{red}{3} & 1.75 & 0 & \color{blue}{-2.25} \\ \hline 1 & \color{red}{1} & 0.75 & 0 & \color{blue}{-1.25} & -3 & \color{blue}{-5.25} \\ \hline 0 & 0 & \color{blue}{-0.25} & -1 & \color{blue}{-2.25} & -4 & \color{blue}{-6.25} \\ \hline -1 & \color{red}{1} & 0.75 & 0 & \color{blue}{-1.25} & -3 & \color{blue}{-5.25} \\ \hline -2 & 4 & 3.75 & \color{red}{3} & 1.75 & 0 & \color{blue}{-2.25} \\ \hline -3 & 9 & 8.75 & 8 & 6.75 & \color{red}{5} & 2.75 \\ \hline \end{array}$$

We then apply the following rules:

• If $$\x\$$ is even:
• If $$\x=d-1\$$, we insert either a \ or a / according to the sign of $$\y\$$ (red cells)
• Otherwise, we insert a space
• If $$\x\$$ is odd:
• If $$\d<0\$$, we insert a | (blue cells)
• Otherwise, we insert a space
• +1 for the math part! Feb 26, 2022 at 11:45

Pari/GP, 85 bytes

n->for(i=-n,n,print(concat([if(j>l=abs(i)," |",j<l,"  ",i<0,"\\ ","/ ")|j<-[1..n]])))


Try it online!

• Wow how does this even work Feb 23, 2022 at 8:06

Ruby, 69 bytes

->n{(-n..n).map{|c|((" "*2*z=c.abs)+(c>0?"/ ":"\\ ")+" |"*n)[2,2*n]}}


Try it online!

Charcoal, 16 bytes

ＦＮ«Ｐ↖²Ｐ↓⊕ι→↗»‖Ｂ↓


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

ＦＮ«


Loop V times.

Ｐ↖²


Draw each of the \s along the top as a line of two without moving the cursor, so that the second one gets overwritten by the vertical line below.

Ｐ↓⊕ι


Draw the top half of each vertical line without moving the cursor.

→↗


Move to the top of the next vertical line.

»‖Ｂ↓


Reflect to complete the art.

05AB1E, 18 bytes

Ýx¨R'\ús„ |×ζθ».∊


Explanation:

          #  e.g. input=3
Ý         # Push a list in the range [0, (implicit) input]
#  STACK: [0,1,2,3]
x        # Double each value (without popping)
#  STACK: [0,1,2,3],[0,2,4,6]
¨       # Remove the last item
#  STACK: [0,1,2,3],[0,2,4]
R      # Reverse it
#  STACK: [0,1,2,3],[4,2,0]
'\   '# Push string "\"
#  STACK: [0,1,2,3],[4,2,0],"\"
ú   # Pad it with leading spaces based on the integer-list
#  STACK: [0,1,2,3],["    \","  \","\"]
s        # Swap so the earlier list is at the top
#  STACK: ["    \","  \","\"],[0,1,2,3]
„ |     # Push string " |"
#  STACK: ["    \","  \","\"],[0,1,2,3]," |"
×    # Repeat it the integers amount of times
#  STACK: ["    \","  \","\"],[""," |"," | |"," | | |"]
ζ  # Create pairs of the two lists, which uses a space as implicit filler
# since they're of unequal lengths
#  STACK: [["    \",""],["  \"," |"],["\"," | |"],[" "," | | |"]]
# Pop and push the pairs separated to the stack
#  STACK: ["    \",""],["  \"," |"],["\"," | |"],[" "," | | |"]
θ        # Leave just the last/second item of the top pair
#  STACK: ["    \",""],["  \"," |"],["\"," | |"]," | | |"
»       # Join the stack with newline delimiter, which will implicitly join
# lists by spaces first
#  STACK: "    \ \n  \  |\n\  | |\n | | |"
.∊     # Vertically mirror with overlap
#  STACK: "    \ \n  \  |\n\  | |\n | | |\n\  | |\n  \  |\n    \ "
# (which is output implicitly as result)


Java, 241, 238 bytes

public class V{public static void main(String[]a){int V=new java.util.Scanner(System.in).nextInt(),d=-1,i=V;var s="";while(i<=V){if(i==0)d=1;else s+=("  ".repeat(i-1)+(d<0?"\\ ":"/ "));s+=" |".repeat(V-i)+'\n';i+=d;}System.out.print(s);}}


Obviously Java is a rather verbose language for code golf, but I like to see how well I can do with it anyway. It isn't clear if "Given a value V" means to read it as an input, or if the value for V is allowed to be hard coded, so long as values in the range (1-10) work. If the value for V can be set in the code, the answer can be reduced to 198 bytes.

If we go so far as to allow a line of code that can be entered into jshell, then we can get it to 145 or 146 bytes (depending on V being one or two digits):

int V=3,d=-1,i=V;var s="";while(i<=V){if(i==0)d=1;else s+=("  ".repeat(i-1)+(d<0?"\\ ":"/ "));s+=" |".repeat(V-i)+'\n';i+=d;}System.out.print(s);


In this case the input value for V is entered directly in the program. I suspect this violates Default for Code Golf: Program, Function or Snippet? though.

• Welcome to Code Golf, and nice answer! Hard-coding input in a variable isn't typically allowed here, yeah. (It's perfectly fine to include both versions like you do now though) Feb 24, 2022 at 0:27
• You could shove off another 86 characters from the class-based solution by using a lambda (Java 8+): (int V)->{int d=-1,i=V;var s="";while(i<=V){if(i==0)d=1;else s+=(" ".repeat(i-1)+(d<0?"\\ ":"/ "));s+=" |".repeat(V-i)+'\n';i+=d;}System.out.print(s);} Feb 24, 2022 at 17:33
• @Muskovets Only if I don't submit a complete program. If I need to read V from stdin, make a complete runnable program, the lambda is actually more bytes than my comparable last code snippet. Feb 24, 2022 at 23:10
• @swpalmer The rules did say you can submit a function instead of a runnable program last time I checked.. Look at the JS, Ruby, etc. answers, they all use lambdas. Feb 26, 2022 at 15:37

GNU APL, 43 42 Chars, 67 66 Bytes

{⍉(2/-⍵-⍳⍵)⌽⊃⊃,/{1(¯1+2×⍵)1/¨"\ /"" | "}¨⍳⍵}


⊃⊃,/{1(¯1+2×⍵)1/¨'\ /' ' | '}¨⍳⍵ replicates characters according to the input and builds the matrix, for example V=3:

\ /
|
\   /
|||
\     /
|||||


(2/-⍵-⍳⍵)⌽ rotates rows to their position, and finally ⍉ does a transpose:

    \
\  |
\  | |
| | |
/  | |
/  |
/


Update:

Since GNU APL support double quote string, '\ /' ' | ' can be rewritten as "\ /"" | ", with one space removed.

Retina 0.8.2, 65 bytes

.+
$*:¶$&$*:¶$&$*: : | \G \| ¶$.%'$* \$%
r \|\G
$.%$* / $%'¶  Try it online! Explanation: .+$*:¶$&$*:¶$&$*:


Convert the input into three rows of V :s as placeholders for the |s (which cost an extra byte to match).

:
|


Replace the :s with  |, so that the |s have the correct spacing.

\G \|
¶$.%'$* \ $%  Turn the first row of  |s into a "triangle", preceded with \s. r \|\G$.%$* /$%'¶


Turn the last row of  |s into a "triangle", preceded with /s.

APL+WIN, 59 bytes

Prompts for n. Index origin = 0

(∊2/¨⌽-i)⊖⍉⊃,('\',¨(-1+n)↑¨'/'),[0.1]' ',¨(n←1+2×i←⍳⎕)⍴¨'|'


Try it online! Thanks to Dyalog Classic