2014-03-28 22:19:26 -03:00
|
|
|
def board(inp):
|
2017-11-13 05:36:02 -08:00
|
|
|
if(inp == []):
|
|
|
|
|
return []
|
2014-03-28 22:19:26 -03:00
|
|
|
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
|
2017-11-13 05:36:02 -08:00
|
|
|
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('*')
|
2014-03-28 22:19:26 -03:00
|
|
|
if cnt == 0:
|
|
|
|
|
continue
|
|
|
|
|
b[i1][i2] = str(cnt)
|
|
|
|
|
return ["".join(r) for r in b]
|
2015-11-08 09:07:52 +01:00
|
|
|
|
|
|
|
|
|
2014-03-28 22:19:26 -03:00
|
|
|
def verify_board(inp):
|
|
|
|
|
# Rows with different lengths
|
|
|
|
|
rowlen = len(inp[0])
|
2015-11-08 09:07:52 +01:00
|
|
|
if not all(len(r) == rowlen for r in inp):
|
2014-03-28 22:19:26 -03:00
|
|
|
raise ValueError("Invalid board")
|
|
|
|
|
# Unknown character in board
|
|
|
|
|
cset = set()
|
|
|
|
|
for r in inp:
|
|
|
|
|
cset.update(r)
|
2017-11-13 05:36:02 -08:00
|
|
|
if cset - set(' *'):
|
2015-11-08 09:07:52 +01:00
|
|
|
raise ValueError("Invalid board")
|