The quickest way to resize is to copy the nearest pixel.
What to build
A program that resizes an image with nearest-neighbour sampling.
Requirements
- Output pixel (y, x) copies source pixel (y × H // newH, x × W // newW).
- Print newH rows of newW values.
Input
Line 1: H, W, newH and newW. Then H rows of W values.
Output
newH rows.
Constraints
1 ≤ H, W, newH, newW ≤ 60.
Example
Input:
2 2 4 4
1 2
3 4
Output:
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
Why it works
Each output position is scaled back into the source and rounded down to a whole pixel, so every output pixel copies exactly one source pixel.
Examples
Example 1 · Input
2 2 4 4 1 2 3 4
Expected output
1 1 2 2 1 1 2 2 3 3 4 4 3 3 4 4
Example 2 · Input
4 4 2 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Expected output
1 3 9 11
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Output row y comes from source row y × H // newH, and output column x from source column x × W // newW.
Hint 2
Build each output row with a comprehension over x.
Solution
Show one correct solution (try it yourself first)
h, w, nh, nw = map(int, input().split())
img = [input().split() for _ in range(h)]
for y in range(nh):
print(" ".join(img[y * h // nh][x * w // nw] for x in range(nw)))