Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rotate_270(matrix):
- n = len(matrix)
- new_matrix = [[matrix[i][j] for j in range(n)] for i in range(n)]
- # Transpose the matrix
- for r in range(n):
- for c in range(r, n):
- temp = new_matrix[r][c]
- new_matrix[r][c] = new_matrix[c][r]
- new_matrix[c][r] = temp
- until = int(n / 2)
- # swap rows
- for r in range(until):
- temp = new_matrix[r]
- new_matrix[r] = new_matrix[n - r - 1]
- new_matrix[n - r - 1] = temp
- return new_matrix
- def rotate_180(matrix):
- n = len(matrix)
- until = int(n / 2)
- new_matrix = [[matrix[i][j] for j in range(n)] for i in range(n)]
- # swap rows
- for r in range(until):
- temp = new_matrix[r]
- new_matrix[r] = new_matrix[n - r - 1]
- new_matrix[n - r - 1] = temp
- # reverse each row's array
- for r in range(n):
- new_matrix[r] = inverted_array(new_matrix[r])
- return new_matrix
- def rotate_90(matrix):
- n = len(matrix)
- new_matrix = [[0 for j in range(n)] for i in range(n)]
- # Transpose matrix
- for r in range(n):
- for c in range(n):
- new_matrix[r][c] = matrix[c][r]
- for r in range(n):
- new_matrix[r] = inverted_array(new_matrix[r])
- return new_matrix
- def inverted_array(array):
- length = len(array)
- new_array = [array[i] for i in range(length)]
- until = int(length / 2)
- for i in range(until):
- temp = new_array[i]
- new_array[i] = new_array[length - i - 1]
- new_array[length - i - 1] = temp
- return new_array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement