13
\$\begingroup\$

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.

\$\endgroup\$
1

12 Answers 12

8
\$\begingroup\$

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°
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Excellent character choice for the rotation operator in vyxal \$\endgroup\$
    – Jonah
    Feb 23 at 14:22
7
\$\begingroup\$

Python 3.8 (pre-release), 169 157 144 131 98 92 90 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).

\$\endgroup\$
15
  • \$\begingroup\$ 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). \$\endgroup\$
    – ophact
    Feb 23 at 8:11
  • \$\begingroup\$ @ophact thanks for the tips! \$\endgroup\$
    – Aiden Chow
    Feb 23 at 8:21
  • \$\begingroup\$ 131 bytes by switching to lambda and making use of python's := (walrus) operator. The lambda mode is usually much shorter than def... [...] return ... \$\endgroup\$
    – ophact
    Feb 23 at 8:25
  • 2
    \$\begingroup\$ @alephalpha 85 bytes as full program in Python 2. \$\endgroup\$
    – ovs
    Feb 23 at 8:52
  • 6
    \$\begingroup\$ 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. \$\endgroup\$
    – ophact
    Feb 23 at 9:27
7
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ +1 for the math part! \$\endgroup\$ Feb 26 at 11:45
6
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ Wow how does this even work \$\endgroup\$
    – Aiden Chow
    Feb 23 at 8:06
6
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 16 bytes

FN«P↖²P↓⊕ι→↗»‖B↓

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

FN«

Loop V times.

P↖²

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.

P↓⊕ι

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

→↗

Move to the top of the next vertical line.

»‖B↓

Reflect to complete the art.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 18 bytes

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

Try it online or verify all test cases.

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)
\$\endgroup\$
2
\$\begingroup\$

Ruby, 69 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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) \$\endgroup\$ Feb 24 at 0:27
  • 1
    \$\begingroup\$ 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);} \$\endgroup\$
    – Muskovets
    Feb 24 at 17:33
  • \$\begingroup\$ @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. \$\endgroup\$
    – swpalmer
    Feb 24 at 23:10
  • \$\begingroup\$ @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. \$\endgroup\$
    – Muskovets
    Feb 26 at 15:37
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.