Skip to content

Commit

Permalink
Minesweeper: sync expected test results and input data with problem-s…
Browse files Browse the repository at this point in the history
…pecifications (#1804)
  • Loading branch information
cmccandless authored May 31, 2019
2 parents 6fa8185 + 9582690 commit 1103a74
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 92 deletions.
57 changes: 31 additions & 26 deletions exercises/minesweeper/example.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
def board(inp):
if(inp == []):
def annotate(minefield):
if(minefield == []):
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] != ' ':
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(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:

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
b[i1][i2] = str(cnt)
return ["".join(r) for r in b]

board[index1][index2] = str(counts)
return ["".join(row) for row in board]

def verify_board(inp):

def verify_board(minefield):
# Rows with different lengths
rowlen = len(inp[0])
if not all(len(r) == rowlen for r in inp):
row_len = len(minefield[0])
if not all(len(row) == row_len for row in minefield):
raise ValueError("Invalid board")

# Unknown character in board
cset = set()
for r in inp:
cset.update(r)
if cset - set(' *'):
character_set = set()
for row in minefield:
character_set.update(row)
if character_set - set(' *'):
raise ValueError("Invalid board")
2 changes: 1 addition & 1 deletion exercises/minesweeper/minesweeper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def board(input_board_array):
def annotate(minefield):
# Function body starts here
pass
130 changes: 65 additions & 65 deletions exercises/minesweeper/minesweeper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,150 +2,150 @@
Implementation note:
The board function must validate its input and raise a
ValueError with a meaningfull error message if the
ValueError with a *meaningful* error message if the
input turns out to be malformed.
"""

import unittest

from minesweeper import board
from minesweeper import annotate


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class MinesweeperTest(unittest.TestCase):

def test_no_rows(self):
self.assertEqual(board([]), [])
self.assertEqual(annotate([]), [])

def test_no_columns(self):
self.assertEqual(board([""]), [""])
self.assertEqual(annotate([""]), [""])

def test_no_mines(self):
inp = [" ",
" ",
" "]
minefield = [" ",
" ",
" "]
out = [" ",
" ",
" "]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_board_with_only_mines(self):
inp = ["***",
"***",
"***"]
def test_annotate_with_only_mines(self):
minefield = ["***",
"***",
"***"]
out = ["***",
"***",
"***"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_mine_surrounded_by_spaces(self):
inp = [" ",
" * ",
" "]
minefield = [" ",
" * ",
" "]
out = ["111",
"1*1",
"111"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_space_surrounded_by_mines(self):
inp = ["***",
"* *",
"***"]
minefield = ["***",
"* *",
"***"]
out = ["***",
"*8*",
"***"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_horizontal_line(self):
inp = [" * * "]
minefield = [" * * "]
out = ["1*2*1"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_horizontal_line_mines_at_edges(self):
inp = ["* *"]
minefield = ["* *"]
out = ["*1 1*"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_vertical_line(self):
inp = [" ",
"*",
" ",
"*",
" "]
minefield = [" ",
"*",
" ",
"*",
" "]
out = ["1",
"*",
"2",
"*",
"1"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_vertical_line_mines_at_edges(self):
inp = ["*",
" ",
" ",
" ",
"*"]
minefield = ["*",
" ",
" ",
" ",
"*"]
out = ["*",
"1",
" ",
"1",
"*"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_cross(self):
inp = [" * ",
" * ",
"*****",
" * ",
" * "]
minefield = [" * ",
" * ",
"*****",
" * ",
" * "]
out = [" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "]
self.assertEqual(board(inp), out)

def test_large_board(self):
inp = [" * * ",
" * ",
" * ",
" * *",
" * * ",
" "]
self.assertEqual(annotate(minefield), out)

def test_large_annotate(self):
minefield = [" * * ",
" * ",
" * ",
" * *",
" * * ",
" "]
out = ["1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

# Additional test for this track
def test_board9(self):
inp = [" ",
" * ",
" ",
" ",
" * "]
def test_annotate9(self):
minefield = [" ",
" * ",
" ",
" ",
" * "]
out = [" 111",
" 1*1",
" 111",
"111 ",
"1*1 "]
self.assertEqual(board(inp), out)
self.assertEqual(annotate(minefield), out)

def test_different_len(self):
inp = [" ",
"* ",
" "]
minefield = [" ",
"* ",
" "]
with self.assertRaisesWithMessage(ValueError):
board(inp)
annotate(minefield)

def test_invalid_char(self):
inp = ["X * "]
minefield = ["X * "]
with self.assertRaisesWithMessage(ValueError):
board(inp)
annotate(minefield)

# Utility functions
def setUp(self):
Expand Down

0 comments on commit 1103a74

Please sign in to comment.