Draw a whole trend in one word with block characters.
What to build
A program that turns a list of numbers into a sparkline.
Requirements
- Map each value to one of
▁▂▃▄▅▆▇█: the index is int((v − lo) / (hi − lo) × 7 + 0.5), where lo and hi are the smallest and largest values. If all values are equal, every value is▁. - Print the sparkline, then
min LO, max HIwith both written with Python's:gformat.
Input
One line of numbers separated by spaces.
Output
Two lines.
Constraints
1 to 200 numbers.
Example
Input:
1 2 3 4 5 6 7 8
Output:
▁▂▃▄▅▆▇█
min 1, max 8
Why it works
Scaling between the smallest and largest value uses all eight heights, so the shape of the trend is visible however large or small the numbers are.
Examples
Example 1 · Input
1 2 3 4 5 6 7 8
Expected output
▁▂▃▄▅▆▇█ min 1, max 8
Example 2 · Input
5 5 5
Expected output
▁▁▁ min 5, max 5
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The block for v is BLOCKS[int((v − lo) / (hi − lo) × 7 + 0.5)].
Hint 2
If every value is the same, there is no range to divide by.
Solution
Show one correct solution (try it yourself first)
BLOCKS = "▁▂▃▄▅▆▇█"
values = [float(v) for v in input().split()]
lo, hi = min(values), max(values)
if hi == lo:
line = BLOCKS[0] * len(values)
else:
line = "".join(BLOCKS[int((v - lo) / (hi - lo) * 7 + 0.5)] for v in values)
print(line)
print(f"min {lo:g}, max {hi:g}")