-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path058.py
More file actions
34 lines (24 loc) · 829 Bytes
/
058.py
File metadata and controls
34 lines (24 loc) · 829 Bytes
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
# https://www.freecodecamp.org/learn/daily-coding-challenge/2025-10-07
def find_landing_spot(matrix):
danger = {}
for r in range(len(matrix)):
for c in range(len(matrix[r])):
if matrix[r][c] == 0:
danger[(r, c)] = calc_danger(r, c, matrix)
sort_danger = sorted(danger.items(), key=lambda d: d[1])
return list(sort_danger[0][0])
def calc_danger(r, c, matrix):
neighbours = []
# Above.
if r-1 in range(len(matrix)):
neighbours.append(matrix[r-1][c])
# Left.
if c-1 in range(len(matrix[r])):
neighbours.append(matrix[r][c-1])
# Right.
if c+1 in range(len(matrix[r])):
neighbours.append(matrix[r][c+1])
# Below.
if r+1 in range(len(matrix)):
neighbours.append(matrix[r+1][c])
return sum(neighbours)