Cutting a Thumbnail

Data with PythonEasy+10 XP+10

Cutting a Thumbnail

A black-and-white photo is a grid of brightness values. image[r][c] — or image[r, c] once it's an array — is row r, column c, with row 0 at the top and column 0 on the left.

A photo app makes thumbnails in two steps:

  1. Cut out a rectangle: its top-left corner is at row top, column left, and it is height rows tall and width columns wide.
  2. Mirror the piece it cut out, as asked by flip:
flipwhat happens to the cut-out piece
"none"nothing
"left-right"left and right swap: each row is reversed
"top-bottom"top and bottom swap: the order of the rows is reversed

Task: write thumbnail(image, top, left, height, width, flip) and return the finished piece as a list of lists (if you use NumPy, .tolist() turns an array back into one). The rectangle always fits inside the photo.

Example

Examples

image = [[10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44]]
top = 1
left = 1
height = 2
width = 3
flip = "none"
returns [[21, 22, 23], [31, 32, 33]]
image = [[10, 11, 12, 13, 14], [20, 21, 22, 23, 24], [30, 31, 32, 33, 34], [40, 41, 42, 43, 44]]
top = 0
left = 2
height = 3
width = 3
flip = "left-right"
returns [[14, 13, 12], [24, 23, 22], [34, 33, 32]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run