Fill in

Turn the Tile

Flipping is easy. Turning a picture needs one more trick: zip.

zip(a, b) pairs up items: first with first, second with second. The star in zip(*grid) hands every row to zip as a separate argument, so zip groups the first items of every row, then the second items, and so on. Columns become rows!

grid = [[1, 2, 3],
        [4, 5, 6]]
print([list(col) for col in zip(*grid)])
# [[1, 4], [2, 5], [3, 6]]

This swap of rows and columns is called a transpose.

Turning clockwise

To turn a picture 90° clockwise: flip it top-bottom with grid[::-1], then transpose it. A tall picture comes out wide.

Pip tip: Turn a tile four times and you are back where you started. That is a great way to test your code!

Assignment

Fill in the three ___ blanks:

  1. transpose(grid) returns the grid's columns as a list of lists.
  2. rotate(grid) returns the grid turned 90° clockwise (flip top-bottom, then transpose).
  3. In the loop, replace tile with its rotated version after each show(), so the four frames show four turns.