-
Notifications
You must be signed in to change notification settings - Fork 8
/
zigzagTraverse.py
60 lines (55 loc) · 1.4 KB
/
zigzagTraverse.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
def isOutOfBounds(row, col, height, width):
return row < 0 or row > height or col < 0 or col > width
def zigzagTraverse(array):
height = len(array)-1
width = len(array[0])-1
result = []
row, col = 0, 0
goingDown = True
goingRight = False
# how do we make decision based on choices below :
# moving down [row+=1][col]
# moving right [row][col+=1]
# moving diagonally upright [row-=1][col+=1]
# moving diagonally downleft [col-=1][row-=1]
"""
-
[1,3,4, 10]
| / / / |
[2,5,9, 11] ==> [1,2,3,4]
/ / /
[6,8,12,15]
| / / /|
[7,13,14,16]
_
"""
while not isOutOfBounds(row, col, height, width):
result.append(array[row][col])
if goingDown:
if col == 0 or row == height:
goingDown = False
if row == height:
col += 1
else:
row += 1
else:
row += 1
col -= 1
else:
if row == 0 or col == width:
goingDown = True
if col == width:
row += 1
else:
col += 1
else:
row -= 1
col += 1
return result
array = [
[1, 3, 4, 10],
[2, 5, 9, 11],
[6, 8, 12, 15],
[7, 13, 14, 16]
]
print(zigzagTraverse(array))