Files
python/exercises/minesweeper/example.py
Sean 16ba6f52d1 minesweeper: update tests to version 1.1.0 (#1062)
* minesweeper: update tests to version 1.1.0

Add missing test version comment.
Add ordered tests with names matching canonical data descriptions.
Disable empty board tests which are incompatible with board class.
Move selected old tests to additional tests section.
Convert no row/column tests to expect validation to raise ValueError.
Closes #998

* minesweeper: update test data to canonical format 1.0.0

Update test data to borderless format which matches canonical data format.
Change board verification to not test for borders and default example to
properly return a completely empty board and account for new index when there
is no border.
2017-11-13 13:36:02 +00:00

36 lines
1014 B
Python

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")