-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interview_Question.py
114 lines (95 loc) · 3.19 KB
/
Interview_Question.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
"""
Author : Fatih Kahraman
Mail : [email protected]
"""
"""
Convert non-adjacent ones to zero and return this array.
"""
class Corner:
def __init__(self, array):
self.array = array
self.cornerList = []
self.neighbors = [] # all connect item
self.catchCoordinate = [] # catch this time
self.lastCatchCoordinate = [] # last catch
def findCorner(self):
for y, line in enumerate(self.array):
for x, value in enumerate(line):
if value:
if (x == 0 or x == 5) or (y == 0 or y == 5):
self.cornerList.append([y,x])
else:
pass
else:
pass
self.neighbors = self.cornerList.copy()
self.lastCatchCoordinate = self.cornerList.copy()
# Find neighbot with recursion
def findNeighbor(self):
for coordinate in self.lastCatchCoordinate:
self.checkUp(coordinate[1], coordinate[0])
self.lastCatchCoordinate = self.catchCoordinate
self.neighbors += self.catchCoordinate
if self.catchCoordinate:
self.catchCoordinate = []
self.findNeighbor()
# Convert 1 to 0 non-neighbor numbers
def processArray(self):
for y, line in enumerate(self.array):
for x, value in enumerate(line):
if not [y,x] in self.neighbors:
self.array[y][x] = 0
return self.array
def checkUp(self, x, y):
if not y == 0:
if self.array[y - 1][x]:
if not [y-1, x] in self.neighbors:
self.catchCoordinate.append([y-1, x])
self.checkLeft(x, y)
else:
self.checkLeft(x, y)
else:
self.checkLeft(x, y)
def checkLeft(self, x, y):
if not x == 0:
if self.array[y][x - 1]:
if not [y, x-1] in self.neighbors:
self.catchCoordinate.append([y, x-1])
self.checkRight(x, y)
else:
self.checkRight(x, y)
else:
self.checkRight(x, y)
def checkRight(self, x, y):
if not x == 5:
if self.array[y][x + 1]:
if not [y, x+1] in self.neighbors:
self.catchCoordinate.append([y, x+1])
self.checkDown(x, y)
else:
self.checkDown(x, y)
else:
self.checkDown(x, y)
def checkDown(self, x, y):
if not y == 5:
if self.array[y + 1][x]:
if not [y+1, x] in self.neighbors:
self.catchCoordinate.append([y+1, x])
return
else:
return
else:
return
if __name__ == '__main__':
givenArray = [[1,0,0,0,0,0],
[0,1,0,1,1,1],
[0,0,1,0,1,0],
[1,1,0,0,1,0],
[1,0,1,1,0,0],
[1,0,0,0,0,1]]
corner = Corner(givenArray)
corner.findCorner()
corner.findNeighbor()
corner.processArray()
for line in corner.processArray():
print(line)