def rotateImage(image):
""" Rotates an NxN matrix, or image, by 90 degrees
(clockwise) in place.
Args:
image (list of lists of obj): An NxN matrix of any object.
Returns:>
(list of lists of obj): The given image rotated by 90 degrees
clockwise.
"""
number_of_layers = (len(image) + 1) // 2
for layer in range(number_of_layers):
rotate_image_layer(image, layer)
return image
def rotate_image_layer(image, layer):
""" Rotates a single layer of an NxN matrix, or image,
by 90 degrees clockwise.
Note:
The outer most layer is 0, and the inner most layer
is `len(image) + 1 // 2`.
Args:
image (list of lists of obj): An NxN matrix of any object.
layer (int): The layer number of the given image.
"""
start = layer
end = len(image) - layer - 1
for col in range(start, end):
row = end - col + start
temp = image[row][start]
image[row][start] = image[end][row]
image[end][row] = image[col][end]
image[col][end] = image[start][col]
image[start][col] = temp
Pages
▼
No comments:
Post a Comment