-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-2.py
65 lines (51 loc) · 1.94 KB
/
ch-2.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
63
64
#!/bin/env python
#===============================================================================
#
# FILE: ch-2.py
#
# USAGE: ./ch-2.py
#
# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-088/
# Task 2
# Spiral Matrix
#
# AUTHOR: Lubos Kolouch
# VERSION: 1.0
# CREATED: 11/28/2020 01:02:17 PM
#===============================================================================
def get_spiral(in_arr):
x_max = len(in_arr)
y_max = len(in_arr[0])
pos = [0,0]
direction = ">"
seen_cells = {}
out_arr = []
while 1:
if len(out_arr) == x_max * y_max:
return out_arr
if seen_cells.get((pos[0], pos[1]), 0 ) == 0:
out_arr.append(in_arr[pos[0]][pos[1]])
seen_cells[(pos[0], pos[1])] = 1
# can move in the direction we are walking?
if direction == ">":
if (pos[1] + 1 < x_max) and not (seen_cells.get( (pos[0], pos[1]+1), 0) ):
pos[1] += 1
else:
direction = "v"
elif direction == "v":
if (pos[0] + 1 < y_max) and not (seen_cells.get( (pos[0]+1, pos[1]), 0) ):
pos[0] += 1
else:
direction = "<"
elif direction == "<":
if (pos[0] - 1 >= 0) and not (seen_cells.get( (pos[0]-1, pos[1]), 0) ):
pos[0] -= 1
else:
direction = "^"
elif direction == "^":
if (pos[1] - 1 >= 0) and not (seen_cells.get( (pos[0], pos[1]-1), 0) ):
pos[1] -= 1
else:
direction = ">"
assert get_spiral([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 6, 9, 8, 7, 4, 5]
assert get_spiral([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) == [1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 ]