Mosaic turn readout
EasyOnline Assessment
Track a wall mosaic through a series of turns and report what its top edge and left edge show at the end.
A wall display is a square mosaic of n by n tiles, each carrying a whole number (its brightness). A technician sends it a string of commands, applied one after another from the first character to the last:
R: rotate the whole mosaic a quarter turn clockwise. The left column becomes the top row, read from the bottom up.L: rotate the whole mosaic a quarter turn counter-clockwise. The right column becomes the top row, read from top to bottom.M: mirror the mosaic left to right, so the first column swaps with the last, the second with the second-to-last, and so on.
Each command acts on the mosaic as it looks after all earlier commands.
After the last command, read the top row from left to right and the left column from top to bottom of the mosaic as it is displayed.
Input:
grid:nrows ofnintegers, the mosaic before any command, top row first.moves: a string made of the charactersR,LandM(possibly empty).
Output: a list of two lists [top, left], each with n integers: the final top row and the final left column. They share their first number, the top-left tile.
Example 1
- Input:
- [[[1,2,3],[4,5,6],[7,8,9]],""]
- Output:
- [[1,2,3],[1,4,7]]
- Explanation
- With no commands the mosaic is unchanged. The top row is
[1, 2, 3]and the left column, read downward, is[1, 4, 7].
Example 2
- Input:
- [[[1,2,3],[4,5,6],[7,8,10]],"R"]
- Output:
- [[7,4,1],[7,8,10]]
- Explanation
- One clockwise turn makes the old left column, read bottom-up (7, 4, 1), the new top row. The old bottom row (7, 8, 10, left to right) becomes the new left column, read top to bottom. The answer is
[[7, 4, 1], [7, 8, 10]].
Example 3
- Input:
- [[[1,2,3],[4,5,6],[7,8,10]],"RM"]
- Output:
- [[1,4,7],[1,2,3]]
- Explanation
- Turn clockwise first: the mosaic becomes
[[7, 4, 1], [8, 5, 2], [10, 6, 3]]. Then mirror it, flipping each row:[[1, 4, 7], [2, 5, 8], [3, 6, 10]]. The top row is[1, 4, 7]and the left column is[1, 2, 3].
Constraints
1 <= n <= 8-1000 <= grid[r][c] <= 10000 <= len(moves) <= 3000