What is my volume?

32

7

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.

user85052

Posted 2019-11-08T10:04:53.713

Reputation:

You should have based the volume on 0 - 10 so that someone could answer with "mine goes to 11" – Shaun Bebbers – 2019-11-14T13:35:49.317

Answers

14

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$} }$$

Arnauld

Posted 2019-11-08T10:04:53.713

Reputation: 111 334

1for 99 and 100, output has an unnecessary line – Nahuel Fouilleul – 2019-11-08T13:28:03.400

@NahuelFouilleul Thanks for reporting this. Now fixed. – Arnauld – 2019-11-08T13:49:14.080

7

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].

Kevin Cruijssen

Posted 2019-11-08T10:04:53.713

Reputation: 67 575

6

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

TFeld

Posted 2019-11-08T10:04:53.713

Reputation: 19 246

1

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!

– xnor – 2019-11-09T05:38:42.380

@xnor Wow nice! Thanks :) – TFeld – 2019-11-10T07:49:21.897

6

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!

Corsaka

Posted 2019-11-08T10:04:53.713

Reputation: 581

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. – 79037662 – 2019-11-08T14:44:03.030

Third line? Edited now so may be different, but all were necessary excluding the 100/0 input check.. – Corsaka – 2019-11-08T14:48:52.243

1yes my comment was made before your edit :) Anyways, you are still unnecessarily checking for $i<101$, when it is guaranteed that $i<101$. – 79037662 – 2019-11-08T14:51:23.513

You can replace i==0 with i<1 – MathIsFun7225 – 2019-11-09T17:25:20.110

do p=print to shorten it – LMD – 2019-11-09T18:43:07.410

1also use [[long strings like this to not escape \ and newlines]] – LMD – 2019-11-09T18:43:55.280

Use "if-expressions" in order to golf your code. (Shorter than the existing Lua solution!)

– None – 2019-11-14T03:57:08.413

Oh, I forgot about or/and's other uses. Explanation for people uneducated: Lua's or returns the first argument if true, the second if false - the opposite is true for and. – Corsaka – 2019-11-14T18:41:47.000

3

PowerShell, 113 bytes

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

Try it online!

Veskah

Posted 2019-11-08T10:04:53.713

Reputation: 3 580

2

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!

Nahuel Fouilleul

Posted 2019-11-08T10:04:53.713

Reputation: 5 582

2

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.

Neil

Posted 2019-11-08T10:04:53.713

Reputation: 95 035

2

PowerShell, 94 92 bytes

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

Try it online!

mazzy

Posted 2019-11-08T10:04:53.713

Reputation: 4 832

2

Canvas, 32 27 26 bytes

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

Try it here!

dzaima

Posted 2019-11-08T10:04:53.713

Reputation: 19 048

1

Python 3, 109 bytes

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

Try it online!

Jitse

Posted 2019-11-08T10:04:53.713

Reputation: 3 566

1

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.

Lyxal

Posted 2019-11-08T10:04:53.713

Reputation: 5 253

0

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.

LMD

Posted 2019-11-08T10:04:53.713

Reputation: 377