Photo editors apply simple grid operations one after another. Build a tiny editor.
What to build
A program that applies a list of operations to an image, in order, and prints the result.
Requirements
crop TOP LEFT HEIGHT WIDTHkeeps that rectangle (it always lies inside the image).flip hmirrors left to right;flip vturns the image upside down.rotateturns the image 90 degrees clockwise;invertreplaces each value v by 255 − v.- Print the final size as
H x W, then the rows, values separated by spaces.
Input
Line 1: H and W. Then H rows of W values. Then a count N and N lines, one operation each.
Output
The size line, then the image rows.
Constraints
1 ≤ H, W ≤ 50; at most 20 operations.
Example
Input:
2 3
1 2 3
4 5 6
1
flip h
Output:
2 x 3
3 2 1
6 5 4
Why it works
Each operation builds a new grid from the old one, so applying them in order is a chain of small, testable steps; rotating swaps the height and width.
Examples
2 3 1 2 3 4 5 6 1 flip h
2 x 3 3 2 1 6 5 4
3 3 10 20 30 40 50 60 70 80 90 2 crop 1 1 2 2 invert
2 x 2 205 195 175 165
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Keep the image as a list of rows and replace it after each operation: a crop is `[row[left:left + width] for row in img[top:top + height]]`.
Hint 2
To rotate clockwise, take the columns with `zip(*img)` and reverse each one.
Solution
Show one correct solution (try it yourself first)
h, w = map(int, input().split())
img = [list(map(int, input().split())) for _ in range(h)]
for _ in range(int(input())):
op = input().split()
if op[0] == "crop":
top, left, ch, cw = map(int, op[1:])
img = [row[left:left + cw] for row in img[top:top + ch]]
elif op[0] == "flip" and op[1] == "h":
img = [row[::-1] for row in img]
elif op[0] == "flip":
img = img[::-1]
elif op[0] == "rotate":
img = [list(col)[::-1] for col in zip(*img)]
elif op[0] == "invert":
img = [[255 - v for v in row] for row in img]
print(len(img), "x", len(img[0]))
for row in img:
print(" ".join(map(str, row)))