Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Display a Progress Bar

+5
−0

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. So a number of 33.1 can be rounded either up to 34 or down to 33, whichever you choose.

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: [|||||||||||||||||||||||||||||||||-----------------]
or
Output: [||||||||||||||||||||||||||||||||||----------------]

Example 3

Input: 234/300
Output: [|||||||||||||||||||||||||||||||||||||||-----------]

Example 4

Input: 5/50
Output: [|||||---------------------------------------------]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Rounding of 33.33 (2 comments)

6 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+4
−0

Jelly, 13 bytes

50R>×ɗị⁾-|Ø[j

Try it online!

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 [].
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Vyxal 3, 26 bytes

÷50×.5+:'|×'[p$51$-'-×']W“

Vyxal It Online!

÷50×.5+:'|×'[p$51$-'-×']W“­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‎⁡⁠⁢⁢⁢‏⁠⁠‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁤‏‏​⁡⁠⁡‌⁢⁤​‎‏​⁢⁠⁡‌⁣⁡​‎‎⁡⁠⁢⁣⁡‏⁠‎⁡⁠⁢⁣⁢‏‏​⁡⁠⁡‌­
÷50×.5+                     # ‎⁡compute (50xratio)+0.5
       :                    # ‎⁢push it twice
        '|×                 # ‎⁣"|" that many times
           '[p              # ‎⁤prepend "["
              $51$-         # ‎⁢⁡retrieve the second copy of the number, and subtract it from 51
                   '-×      # ‎⁢⁢"-" 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Off by one error (3 comments)
+3
−0

Japt, 18 bytes

"[{/V*50 ç| ú-50}]

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Uiua, 19 bytes

$"[_]"⊏⊙"-|">÷⟜⇡50÷

pad

$"[_]"⊏⊙"-|">÷⟜⇡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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Correct rounding? (6 comments)
Off by one error (2 comments)
+2
−0

Python 3, 48 bytes

def f(n,d):return f"[{(50*n+d//2)//d*'|':-<50}]"

Try it online!

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

C (gcc), 88 bytes

char a[53]={91},*p=a+1,i;f(n,d){for(;i<50;)p[i++]=1.*n/d<=i/50.?45:124;p[i]=93;puts(a);}

Try it online!

There's two approaches to this in C that I came up with - either write into an array then print it, or print everything at once but use conditionals. I tried both but got about the same result so this version is the array one.

Dumping all variables at file scope makes initialization easier and we can abuse the usual C89 tricks of "implicit int" (but this makes the function "call once", so tests will have to restore all variables between multiple calls).

The actual algorithm is just floating point division then scale that from 0 to 50. < instead of <= would give incorrect rounding and that would be my on my algorithm, not the language, so I used <=.

Also this code has some pretty severe UB abuse with the i++... :)

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

Greater than rather than less than or equal to (2 comments)
Off by one error (2 comments)

Sign up to answer this question »