Skip to content

Instantly share code, notes, and snippets.

@danquixote
Last active November 22, 2017 07:30
Show Gist options
  • Save danquixote/64d12eebef39c50f4adf2204d6dbf2ef to your computer and use it in GitHub Desktop.
Save danquixote/64d12eebef39c50f4adf2204d6dbf2ef to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from rotate_testcases import *#kludge
def rotate_matrix_90_deg_right(matrix):
outer_list = []
matrix = list(reversed(matrix))
matrix_len = len(matrix)
max_row_len = max([len(row) for row in matrix])#should be same len
if matrix_len >= max_row_len:
for i in range(0, matrix_len):
lst = []
for row in matrix:
if i < len(row):
lst.append(row[i])
if len(lst) == len(row):
outer_list.append(lst)
else:
for i in range(0, max_row_len):
lst = []
for j in range(0, matrix_len):
lst.append(matrix[j][i])
outer_list.append(lst)
return outer_list
if __name__ == '__main__':
assert(rotate_matrix_90_deg_right(M1) == M1OUT)
assert(rotate_matrix_90_deg_right(M2) == M2OUT)
assert(rotate_matrix_90_deg_right(M3) == M3OUT)
M1 = [
[1,2,3],
[4,5,6],
[7,8,9],
[10,11,12],
]
M1OUT = [
[10,7,4,1],
[11,8,5,2],
[12,9,6,3],
]
M2 = [
[4,5,6],
[7,8,9],
[10,11,12],
]
M2OUT = [
[10,7,4],
[11,8,5],
[12,9,6],
]
M3 = [
[4,5,6],
[7,8,9],
]
M3OUT = [
[7,4],
[8,5],
[9,6],
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment