-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcavity_map.py
54 lines (42 loc) · 1.27 KB
/
cavity_map.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
#https://www.hackerrank.com/challenges/cavity-map/problem?isFullScreen=true
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'cavityMap' function below.
#
# The function is expected to return a STRING_ARRAY.
# The function accepts STRING_ARRAY grid as parameter.
#
def cavityMap(grid):
# Write your code here
for i in range(1, len(grid) - 1):
for j in range(1, len(grid[i]) - 1):
item = int(grid[i][j])
top = grid[i - 1][j]
left = grid[i][j - 1]
right = grid[i][j + 1]
bottom = grid[i + 1][j]
if left == "X" or right == "X" or top == "X" or bottom == "X":
continue
top = int(top)
left = int(left)
bottom = int(bottom)
right = int(right)
if item > top and item > left and item > bottom and item > right:
grid[i] = grid[i][:j] + 'X' + grid[i][j+1:]
return grid
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
n = int(input().strip())
grid = []
for _ in range(n):
grid_item = input()
grid.append(grid_item)
result = cavityMap(grid)
fptr.write('\n'.join(result))
fptr.write('\n')
fptr.close()