forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
35 lines (33 loc) · 1014 Bytes
/
example.py
File metadata and controls
35 lines (33 loc) · 1014 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
35
def board(inp):
if(inp == []):
return []
verify_board(inp)
rowlen = len(inp[0])
collen = len(inp)
b = [list(r) for r in inp]
for i1 in range(collen):
for i2 in range(rowlen):
if b[i1][i2] != ' ':
continue
low = max(i2 - 1, 0)
high = min(i2 + 2, rowlen + 2)
cnt = inp[i1][low:high].count('*')
if(i1 > 0):
cnt += inp[i1 - 1][low:high].count('*')
if(i1 < collen - 1):
cnt += inp[i1 + 1][low:high].count('*')
if cnt == 0:
continue
b[i1][i2] = str(cnt)
return ["".join(r) for r in b]
def verify_board(inp):
# Rows with different lengths
rowlen = len(inp[0])
if not all(len(r) == rowlen for r in inp):
raise ValueError("Invalid board")
# Unknown character in board
cset = set()
for r in inp:
cset.update(r)
if cset - set(' *'):
raise ValueError("Invalid board")