-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cavity Map - Algorythms.py
72 lines (59 loc) · 1.6 KB
/
Cavity Map - Algorythms.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
def cavityMap(grid):
arr = [grid[0]] # Eerste regel naar nieuwe grid, want geen X mogelijk.
print(arr)
for i in range(1, len(grid) - 1): # Tussenregels checken op cavity
print("We bekijken: {}".format(grid[i]))
new_line = grid[i] # eerst de bestaande regel overnemen
# print("new_line: {}".format(new_line))
for j in range(1, len(grid[i]) - 1):
u = int(grid[i - 1][j])
l = int(grid[i][j - 1])
r = int(grid[i][j + 1])
d = int(grid[i + 1][j])
if u < int(grid[i][j]) and l < int(grid[i][j]) and r < int(grid[i][j]) and d < int(grid[i][j]):
print("X")
new_line = new_line[:j] + 'X' + new_line[j+1:]
print(new_line)
arr.append(new_line)
arr.append(grid[-1])
return arr
if __name__ == '__main__':
n = int(input())
grid = []
for _ in range(n):
grid_item = input()
grid.append(grid_item)
result = cavityMap(grid)
print(result)
"""https://www.hackerrank.com/challenges/cavity-map/problem?h_r=next-challenge&h_v=zen
Input:
4
1112
1912
1892
1234
Output:
1112
1X12
18X2
1234
Input:
989
191
111
Output:
989
1X1
111
This one is better:
def cavityMap(grid):
s = ''.join(line for line in grid); t = s[:]
l = len(grid[0]); n = len(grid)
for i in range(l+1, (n-1)*l,l):
for j in range(i, i+l-2):
values = map(int, [s[j+1], s[j-1], s[j-l], s[j+l]])
if int(s[j]) > max(values):
t = ''.join([t[:j],'X',t[j+1:]])
for i in range(0, n*l,l):
yield t[i:i+l]
"""