A histogram with bins is the quickest way to see whether an image is dark, bright or dull.
What to build
A program that counts pixels into bins of equal width and names the fullest bin.
Requirements
- Bins are B levels wide and cover 0 to 255: bin i holds the values from i × B to i × B + B − 1.
- Print every bin as
LOW-HIGH: COUNT, from the darkest bin to the brightest. - Then print
fullest bin LOW-HIGH; if several bins tie, choose the darkest.
Input
Line 1: H, W and B (B divides 256). Then H rows of W values from 0 to 255.
Output
256 / B bin lines and the fullest-bin line.
Constraints
1 ≤ H, W ≤ 100.
Example
Input:
2 3 64
0 63 64
128 255 200
Output:
0-63: 2
64-127: 1
128-191: 1
192-255: 2
fullest bin 0-63
Why it works
Integer division by the bin width sends every value to exactly one bin, so the counts add up to the number of pixels.
Examples
Example 1 · Input
2 3 64 0 63 64 128 255 200
Expected output
0-63: 2 64-127: 1 128-191: 1 192-255: 2 fullest bin 0-63
Example 2 · Input
1 4 128 10 200 130 127
Expected output
0-127: 2 128-255: 2 fullest bin 0-127
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Make a list of 256 // B zeros and add 1 to `counts[v // B]` for every pixel.
Hint 2
`counts.index(max(counts))` gives the first fullest bin.
Solution
Show one correct solution (try it yourself first)
h, w, b = map(int, input().split())
counts = [0] * (256 // b)
for _ in range(h):
for v in map(int, input().split()):
counts[v // b] += 1
for i, n in enumerate(counts):
print(f"{i * b}-{i * b + b - 1}: {n}")
best = counts.index(max(counts))
print(f"fullest bin {best * b}-{best * b + b - 1}")