-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path65.py
More file actions
79 lines (64 loc) · 1.43 KB
/
Copy path65.py
File metadata and controls
79 lines (64 loc) · 1.43 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
This problem was asked by Amazon.
Given a N by M matrix of numbers, print out the matrix in a clockwise spiral.
For example, given the following matrix:
[[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
You should print out the following:
1
2
3
4
5
10
15
20
19
18
17
16
11
6
7
8
9
14
13
12
"""
from typing import List
matrix_1 = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]]
matrix_2 = [[ 1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]]
def clockwise_print(matrix: List[List[int]]):
top = 0
bottom = len(matrix) - 1
left = 0
right = len(matrix[0]) - 1
while top <= bottom and left <= right:
for col in range(left, right + 1):
print(matrix[top][col])
top += 1
for row in range(top, bottom + 1):
print(matrix[row][right])
right -= 1
if top <= bottom:
for col in range(right, left - 1, -1):
print(matrix[bottom][col])
bottom -= 1
if left <= right:
for row in range(bottom, top - 1, -1):
print(matrix[row][left])
left += 1
clockwise_print(matrix_1)
clockwise_print(matrix_2)
# index nightmare, maze traversal
# understanding constraints of movement for iteration over a data structure
# DONE