S3Q2 · Horizontal Bar Chart¶
⚡ Quick Reference
Type: Full I/O -formatted output
Core idea: parse all pairs first, find the longest key, then print each key right-aligned in that width followed by : and # × value.
n = int(input())
pairs = [input().split(":") for _ in range(n)]
width = max(len(k) for k, _ in pairs)
for key, val in pairs:
print(f"{key:>{width}}:{'#' * int(val)}")
Key rules:
- Must read all pairs before printing -need max key length first
- f"{key:>{width}}" right-aligns key in a field of width characters
- Bars are '#' * count -no spaces between : and #
Problem Statement¶
Problem (I/O type)
Read n key-value pairs (separated by :). Print a horizontal bar chart where keys are right-aligned so all colons line up vertically.
Example:
6
apple:10
banana:5
plum:6
cherry:3
dates:8
strawberry:15
apple:##########
banana:#####
plum:######
cherry:###
dates:########
strawberry:###############
Understanding the problem¶
Two passes are needed:
- First pass -read all pairs, find the longest key (
"strawberry"= 10 chars) - Second pass -print each key right-aligned in a field of width 10
"strawberry" → width = 10
apple → 5 leading spaces (10 - 5 = 5)
banana → 4 leading spaces (10 - 6 = 4)
plum → 6 leading spaces (10 - 4 = 6)
f-string right-alignment: f\"{s:>width}\"
f"{key:>{width}}" right-aligns key in a field of width characters, padding with spaces on the left. The > means right-align; < would be left-align; ^ would be centred.
Tracing the example¶
width = len("strawberry") = 10
| Key | len | Padding | Bar | Printed line |
|---|---|---|---|---|
| apple | 5 | 5 spaces | ########## |
·····apple:########## |
| banana | 6 | 4 spaces | ##### |
····banana:##### |
| plum | 4 | 6 spaces | ###### |
······plum:###### |
| cherry | 6 | 4 spaces | ### |
····cherry:### |
| dates | 5 | 5 spaces | ######## |
·····dates:######## |
| strawberry | 10 | 0 spaces | ############### |
strawberry:############### |
(dots represent spaces)
Solution approaches¶
n = int(input())
pairs = [input().split(":") for _ in range(n)]
width = max(len(k) for k, _ in pairs)
for key, val in pairs:
print(f"{key:>{width}}:{'#' * int(val)}")
Read all into a list, compute width once, then print with f-string alignment.
n = int(input())
pairs = []
for _ in range(n):
line = input()
key, val = line.split(":")
pairs.append((key, int(val)))
width = max(len(key) for key, _ in pairs)
for key, val in pairs:
padding = " " * (width - len(key))
bar = "#" * val
print(f"{padding}{key}:{bar}")
Explicit padding calculation -width - len(key) leading spaces. Every step named for clarity.
n = int(input())
pairs = [input().split(":") for _ in range(n)]
width = max(len(k) for k, _ in pairs)
for key, val in pairs:
print(key.rjust(width) + ":" + "#" * int(val))
str.rjust(width) right-justifies a string in a field of width characters -equivalent to f"{key:>{width}}" but without an f-string.
n = int(input())
pairs = [input().split(":") for _ in range(n)]
width = max(len(k) for k, _ in pairs)
output = list(map(
lambda kv: f"{kv[0]:>{width}}:{'#' * int(kv[1])}",
pairs
))
print("\n".join(output))
map(lambda kv: ..., pairs) applies the formatting to every pair. "\n".join(output) prints all lines in one call.
Key takeaways¶
Read all before printing
Right-alignment requires knowing the maximum key length first. Always collect all input before producing any output when the formatting depends on the full dataset.
f"{s:>{width}}" for right-alignment
The > in a format spec means right-align. < = left, ^ = centre. These work for any type inside an f-string -strings, numbers, anything.
"#" * n for bars
String multiplication "#" * n creates a bar of exactly n characters. The same pattern works for any character-based visualisation -stars, dashes, equals signs.