forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_transform.py
62 lines (56 loc) · 1.66 KB
/
copy_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def rotate_clockwise(matrix):
new = []
for row in reversed(matrix):
for i, elem in enumerate(row):
try:
new[i].append(elem)
except IndexError:
new.insert(i, [])
new[i].append(elem)
return new
def rotate_counterclockwise(matrix):
new = []
for row in matrix:
for i, elem in enumerate(reversed(row)):
try:
new[i].append(elem)
except IndexError:
new.insert(i, [])
new[i].append(elem)
return new
def top_left_invert(matrix):
new = []
for row in matrix:
for i, elem in enumerate(row):
try:
new[i].append(elem)
except IndexError:
new.insert(i, [])
new[i].append(elem)
return new
def bottom_left_invert(matrix):
new = []
for row in reversed(matrix):
for i, elem in enumerate(reversed(row)):
try:
new[i].append(elem)
except IndexError:
new.insert(i, [])
new[i].append(elem)
return new
if __name__ == '__main__':
def print_matrix(matrix, name):
print('{}:\n['.format(name))
for row in matrix:
print(' {}'.format(row))
print(']\n')
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
print_matrix(matrix, 'initial')
print_matrix(rotate_clockwise(matrix), 'clockwise')
print_matrix(rotate_counterclockwise(matrix), 'counterclockwise')
print_matrix(top_left_invert(matrix), 'top left invert')
print_matrix(bottom_left_invert(matrix), 'bottom left invert')