29
\$\begingroup\$

Given an input integer, output the volume corresponding to the value. The value will always be in the range \$[0,100]\$.

Examples

If the input \$=0\$:

 _/|
|  |\/
|_ |/\
  \|

If the input \$>0\$ and \$<33\$:

 _/|
|  |\
|_ |/
  \|

If the input \$\ge33\$ and \$<66\$:

 _/| \
|  |\ \
|_ |/ /
  \| /

If the input \$\ge66\$, \$\le100\$:

 _/| \ \
|  |\ \ \
|_ |/ / /
  \| / /

Rules

  • Input/output can be given by any convenient method.
  • You can print it to STDOUT or return it as a function result.
  • Either a full program or a function are acceptable.
  • Any amount of extraneous whitespace is permitted, provided the characters line up appropriately.
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$
  • \$\begingroup\$ You should have based the volume on 0 - 10 so that someone could answer with "mine goes to 11" \$\endgroup\$ – Shaun Bebbers Nov 14 at 13:35

12 Answers 12

12
\$\begingroup\$

JavaScript (ES6), 98 bytes

n=>` _/|2
|  |\\0
|_ |/1
  \\|3`.replace(/\d/g,k=>n?(k&1?' /':' \\').repeat(n/33^n>98):['/\\'[k]])

Try it online!

How?

We use the following template:

 _/|2
|  |\0
|_ |/1
  \|3

If \$n=0\$ (volume muted):

  • \$0\$ is replaced with / and \$1\$ is replaced with \
  • the other digits are removed

If \$n\neq 0\$:

  • odd digits are replaced with the string " /" repeated \$k\$ times
  • even digits are replaced with the string " \" repeated \$k\$ times

where \$k\$ is defined as:

$$k=\cases{\left\lfloor n/33\right\rfloor&\text{$n<99$}\\ 2&\text{$n\ge 99$} }$$

\$\endgroup\$
  • 1
    \$\begingroup\$ for 99 and 100, output has an unnecessary line \$\endgroup\$ – Nahuel Fouilleul Nov 8 at 13:28
  • \$\begingroup\$ @NahuelFouilleul Thanks for reporting this. Now fixed. \$\endgroup\$ – Arnauld Nov 8 at 13:49
7
\$\begingroup\$

05AB1E, 65 64 bytes

„ /SõD‚„\ ‚D4δ∍D)T•4a֙镓| 0\
1/“ÅвJrI33÷IĀ+©è‡∊'_1•uтÄ•22в®è‚ǝ

When I started I thought I would be able to make this pretty short with the approach I had in mind, but it had some annoying edge cases to fix.. Can definitely be golfed, though. Will take another look later on.

Try it online or verify some more test cases.

Explanation:

„ /              # Push string " /"
   S             # Convert it to a list of characters: [" ","/"]
õD‚              # Push an empty string "", duplicate it, and pair them together: ["",""]
„\               # Push string "\ "
   Â             # Bifurcate it (shorter for duplicate and reverse copy)
    ‚            # Pair them together: ["\ ","\ "]
D                # Duplicate it
 4δ∍             # Extend both inner strings to size 4: ["\ \ ","\ \ "]
D                # Duplicate it
)                # Wrap all lists on the stack into a list:
                 #  [[" ","/"],["",""],["\ ","\ "],["\ \ ","\ \ "],["\ \ ","\ \ "]]
T                # Push 10
•4aÖ™é•          # Push compressed integer 17523821317
“| 0\
1/“              # Push string "| 0\\n1/"
   Åв            # Convert the integer to custom base-"| 0\\n1/" 
     J           # And join these characters together to a single string:
                 #  "  /| 1\n
                 #   |  |\0"
r                # Reverse the items on the stack (list, 10, string to string, 10, list)
 I33÷            # Push the input, and integer-divide it by 33
     IĀ          # Push the input again, and truthy it (0 if 0; else 1)
       +         # And add them together (0→0; 1..32→1; 33..65→2; 66..98→3; 98..100→4)
        ©        # Store it in variable `®` (without popping)
         è       # And index it into the list we created at the start
          ‡      # Then transliterate the ["1","0"] of 10 to the strings in this pair
∊                # Now vertically mirror the entire string
                 #  (i.e. "  /|  \n
                 #         |  |\/" becomes:
                 #   "  /|  \n
                 #    |  |\/\n
                 #    |  |/\\n
                 #      \|  "
    •/Tδ•        # Push compressed integer 7095187
         ₂в      # Converted to base-26 as list: [15,13,17,21,21]
           ®     # Push variable `®` again
            è    # Index it into this list
   1         ‚   # Pair it with a leading 1
 '_           ǝ '# And insert a "_" at those 0-based positions inside the string
                 # (after which the result is output implicitly)

See this 05AB1E tips of mine (section How to compress large integers? and How to compress integer lists?) to understand why •4aÖ™é• is 17523821317; •/Tδ• is 7095187, and •/Tδ•₂в is [15,13,17,21,21].

\$\endgroup\$
6
\$\begingroup\$

Python 2, 111 101 90 bytes

n=input()
for s in'\ _/|','\|  |\/','/|_ |/\\','/  \|':print s[1:6+0**n],s[:n>32],s[:n/66]

Try it online!

-11 bytes, thanks to xnor

\$\endgroup\$
  • 1
    \$\begingroup\$ Nice method! I found a way to shorten it by encoding b in the first character of each string rather than as a zip, and extracting from there: Try it online! \$\endgroup\$ – xnor Nov 9 at 5:38
  • \$\begingroup\$ @xnor Wow nice! Thanks :) \$\endgroup\$ – TFeld Nov 10 at 7:49
6
\$\begingroup\$

Lua, 308 259 246 242 219 180 bytes

i=io.read("*n")
print(i<1 and[[ _/|
|  |\/
|_ |/\
  \|]]or i<33 and[[ _/|
|  |\
|_ |/
  \|]]or i<66 and[[ _/| \
|  |\ \
|_ |/ /
  \| /]]or[[ _/| \ \
|  |\ \ \
|_ |/ / /
  \| / /]])

Try it online!

New to code golf and coding in general.
Thanks to all comments for their help!

\$\endgroup\$
  • \$\begingroup\$ Hey, quick tip: the input is guaranteed to be within $[0, 100]$ so you don't need to check that it is less than 101. Also your third line doesn't appear to be necessary. \$\endgroup\$ – 79037662 Nov 8 at 14:44
  • \$\begingroup\$ Third line? Edited now so may be different, but all were necessary excluding the 100/0 input check.. \$\endgroup\$ – Corsaka Nov 8 at 14:48
  • 1
    \$\begingroup\$ yes my comment was made before your edit :) Anyways, you are still unnecessarily checking for $i<101$, when it is guaranteed that $i<101$. \$\endgroup\$ – 79037662 Nov 8 at 14:51
  • \$\begingroup\$ You can replace i==0 with i<1 \$\endgroup\$ – MathIsFun7225 Nov 9 at 17:25
  • 1
    \$\begingroup\$ also use [[long strings like this to not escape \ and newlines]] \$\endgroup\$ – LMD Nov 9 at 18:43
3
\$\begingroup\$

PowerShell, 113 bytes

param($n)' _/|'+($y=' \'*($x=($n-ge33)+($n-ge66)))
'|  |\'+($y,'/')[!$n]
'|_ |/'+(($z=' /'*$x),'\')[!$n]
"  \|$z"

Try it online!

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

Perl 5 (-p), 86 bytes

inspired from @Arnauld's solution

$_=' _/|3
|  |\\1
|_ |/0
  \|2'=~s,\d,$_?" $&"x($_/33-$_/99):$&<2&&$&^1,ger;y;0213;//\

Try it online!

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

PowerShell, 94 92 bytes

param($n)33,66-le$n|%{$y+=' \';$z+=' /'}
" _/|$y
|  |\$y"+'/'*!$n
"|_ |/$z"+'\'*!$n
"  \|$z"

Try it online!

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

Canvas, 32 27 26 bytes

┤#Mi‾‟⁸?⁸‾{÷u2m2\×+] ¶/+}═

Try it here!

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

Charcoal, 40 bytes

NθF AF›θ℅ι \C¹¦¹⸿\F¬θ/‖M↓⸿←P↑⁴←↖¹←_↑² _/

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

Nθ

Input the volume.

F AF›θ℅ι \

Output a space and a \ for each character ordinal in space and A that the volume is greater than. In other words, output once for inputs of 33 or more and twice for inputs of 66 or more.

C¹¦¹

Copy the \s one square diagonally.

⸿\

Output an extra \ at the start of the second line.

F¬θ/

If the volume was zero then add a /.

‖M↓

Reflect to complete the volume.

⸿←P↑⁴←↖¹←_↑² _/

Draw the speaker symbol.

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

Python 3, 109 bytes

lambda n:'\n'.join(i[:5+0**n]+(n//33*(' '+j))[:4]for i,j in zip([' _/|','|  |\\/','|_ |/\\','  \|'],'\\\//'))

Try it online!

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

Keg, -hd 167 bytes

¿:0=[` _/|\n|  |\\/\n|_ |/\\\n  \\|`|:1\!"∂[` _/|\n|  |\\\n|_ |/\n  \\|`|:\!B"∂[` _/| \\\n|  |\\ \\\n|_ |/ /\n  \\| /`|` _/| \\ \\\n|  |\\ \\ \\\n|_ |/ / /\n  \\| / /`

Try it online!

Definitely golfable by using variables and string formatting. That's what Imma go do now. Anyhow, it acts as a sort of switch statement to determine which range the volume is in and prints the corresponding string.

Nevermind. Variables only make the program longer.

\$\endgroup\$
0
\$\begingroup\$

Lua, 196 bytes

i=tonumber(io.read())
t=[[ _/|%s
|  |\%s
|_ |/%s
  \|%s]]
t=(i==0 and t:format("","/","\\","")) or t
while i>=33 do t=t:format(" \\%s"," \\%s"," /%s"," /%s");i=i-34 end
t=t:gsub("%%s","")
print(t)

Try it online!

A bit shorter (below 200) than the other Lua answer.

\$\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.