Display a Progress Bar
The goal of this challenge is simple: given a ratio of whole numbers, output a 50-character long progress bar representing the ratio.
Rules
Input
- Your program must take two numbers as input. These numbers will be the numerator and denominator of the ratio, in that order. Your program cannot take the calculated result of the ratio as input. So, an input of $1/4$ is fine, but not $0.25$.
- The numbers may be entered separately, or together with a
/
as a separator, whichever works best with your chosen language. - The numerator can be any integer greater than or equal to $0$, and the denominator can be any integer greater than $0$.
- The ratio cannot be greater than 1, or less than zero. So $0 \le r \le 1$ where $r$ is the ratio. Thus, ratios like $12/3$ are not allowed.
Output
- Your program must output a progress bar that is 50 characters in length, not counting the enclosing brackets.
- The progress bar must fill from left to right.
- The "filled" part of the progress bar must be made of pipes
|
, and the "empty" part of the progress bar must be made of dashes-
. - The progress bar must be enclosed in square brackets
[]
. - Whitespace is fine at the end of the output, but not anywhere else.
- When calculating the number of "filled" characters to output, you may round in whichever direction your language supports.
Scoring
This is a code-golf challenge, so the shortest code in bytes wins!
Examples
Example 1
Input: 1/2
Output: [|||||||||||||||||||||||||-------------------------]
Example 2
Input: 2/3
Output: [|||||||||||||||||||||||||||||||||-----------------]
Example 3
Input: 234/300
Output: [|||||||||||||||||||||||||||||||||||||||-----------]
Example 4
Input: 5/50
Output: [|||||---------------------------------------------]
Vyxal 3, 26 bytes ``` ÷50× …
6d ago
Japt, 18 bytes "[{/V50 …
4d ago
[Jelly], 13 bytes 50R>× …
6d ago
Uiua, 19 bytes ``` $"[]"⊏⊙ …
6d ago
[Python 3], 48 bytes …
4d ago
5 answers
Uiua, 19 bytes
$"[_]"⊏⊙"-|">÷⟜⇡50÷
$"[_]"⊏⊙"-|">÷⟜⇡50÷
÷ # divide arguments
÷⟜⇡50 # range from 0 to 1 in steps of 1/50
> # are greater than?
⊏⊙"-|" # select using booleans: - if false and | if true
$"[_]" # format with square brackets
💎
Created with the help of Luminespire.
Vyxal 3, 26 bytes
÷50×.5+:'|×'[p$50$-'-×']W“
÷50×.5+:'|×'[p$50$-'-×']W“
÷50×.5+ # compute (50xratio)+0.5
: # push it twice
'|× # "|" that many times
'[p # prepend "["
$50$- # retrieve the second copy of the number, and substract it from 50
'-× # "-" that many times
'] # "]" literal
# stack is now ["[||..."] ["--..."] ["]"]
W“ # wrap the entire stack in an array and join it on nothing, yielding the correct string
💎
Created with the help of Luminespire.
0 comment threads
Jelly, 13 bytes
50R>×ɗị⁾-|Ø[j
Full program only--insofar as supporting the input requirements is concerned. Jelly implicitly Python evals the arguments to every program, so slash-separated input of two numbers becomes a float.
50R For every [1 .. 50],
> is it strictly greater than
×ɗ 50 times the ratio?
ị⁾-| Replace 0 with | and 1 with -,
Ø[j and surround with [].
0 comment threads