-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind-sequence.py
80 lines (61 loc) · 1.74 KB
/
find-sequence.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''
author: Jacob Egner
date: 2015-07-27
island: electronic station
puzzle prompt:
http://www.checkio.org/mission/find-sequence/
puzzle prompt source repo:
https://github.com/Bryukh-Checkio-Tasks/checkio-task-find-sequence
for latest version of my solution, see my checkio solution repo:
https://github.com/jmegner/CheckioPuzzles
'''
gc_sequenceLen = 4
def checkio(grid):
for r in range(len(grid)):
for c in range(len(grid[r])):
if sequenceStartsHere(grid, r, c):
return True
return False
def sequenceStartsHere(grid, r, c):
for horizMult, vertMult in ((1, 0), (0, 1), (1, 1), (-1, 1)):
foundSequence = True
for offset in range(gc_sequenceLen):
r2 = r + offset * horizMult
c2 = c + offset * vertMult
if(r2 < 0 or c2 < 0
or r2 >= len(grid) or c2 >= len(grid[r2])
or grid[r][c] != grid[r2][c2]
):
foundSequence = False
break;
if foundSequence:
return True
return False
if __name__ == '__main__':
assert checkio([
[1, 2, 1, 1],
[1, 1, 4, 1],
[1, 3, 1, 6],
[1, 7, 2, 5]
]) == True, "Vertical"
assert checkio([
[7, 1, 4, 1],
[1, 2, 5, 2],
[3, 4, 1, 3],
[1, 1, 8, 1]
]) == False, "Nothing here"
assert checkio([
[2, 1, 1, 6, 1],
[1, 3, 2, 1, 1],
[4, 1, 1, 3, 1],
[5, 5, 5, 5, 5],
[1, 1, 3, 1, 1]
]) == True, "Long Horizontal"
assert checkio([
[7, 1, 1, 8, 1, 1],
[1, 1, 7, 3, 1, 5],
[2, 3, 1, 2, 5, 1],
[1, 1, 1, 5, 1, 4],
[4, 6, 5, 1, 3, 1],
[1, 1, 9, 1, 2, 1]
]) == True, "Diagonal"