forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
40 lines (33 loc) · 1.21 KB
/
example.py
File metadata and controls
40 lines (33 loc) · 1.21 KB
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
def annotate(minefield):
if(minefield == []):
return []
verify_board(minefield)
row_len = len(minefield[0])
col_len = len(minefield)
board = [list(row) for row in minefield]
for index1 in range(col_len):
for index2 in range(row_len):
if board[index1][index2] != ' ':
continue
low = max(index2 - 1, 0)
high = min(index2 + 2, row_len + 2)
counts = minefield[index1][low:high].count('*')
if(index1 > 0):
counts += minefield[index1 - 1][low:high].count('*')
if(index1 < col_len - 1):
counts += minefield[index1 + 1][low:high].count('*')
if counts == 0:
continue
board[index1][index2] = str(counts)
return ["".join(row) for row in board]
def verify_board(minefield):
# Rows with different lengths
row_len = len(minefield[0])
if not all(len(row) == row_len for row in minefield):
raise ValueError("Invalid board")
# Unknown character in board
character_set = set()
for row in minefield:
character_set.update(row)
if character_set - set(' *'):
raise ValueError("Invalid board")