Skip to content

Commit 25e536d

Browse files
committed
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.
1 parent 71ac523 commit 25e536d

File tree

2 files changed

+96
-157
lines changed

2 files changed

+96
-157
lines changed

exercises/minesweeper/example.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
def board(inp):
2+
if(inp == []):
3+
return []
24
verify_board(inp)
35
rowlen = len(inp[0])
46
collen = len(inp)
@@ -7,22 +9,22 @@ def board(inp):
79
for i2 in range(rowlen):
810
if b[i1][i2] != ' ':
911
continue
10-
cnt = inp[i1 - 1][i2 - 1:i2 + 2].count('*') + \
11-
inp[i1][i2 - 1:i2 + 2].count('*') + \
12-
inp[i1 + 1][i2 - 1:i2 + 2].count('*')
12+
low = max(i2 - 1, 0)
13+
high = min(i2 + 2, rowlen + 2)
14+
cnt = inp[i1][low:high].count('*')
15+
if(i1 > 0):
16+
cnt += inp[i1 - 1][low:high].count('*')
17+
if(i1 < collen - 1):
18+
cnt += inp[i1 + 1][low:high].count('*')
1319
if cnt == 0:
1420
continue
1521
b[i1][i2] = str(cnt)
1622
return ["".join(r) for r in b]
1723

1824

1925
def verify_board(inp):
20-
# Null board or a null row
21-
if not inp or not all(r for r in inp):
22-
raise ValueError("Invalid board")
2326
# Rows with different lengths
2427
rowlen = len(inp[0])
25-
collen = len(inp)
2628
if not all(len(r) == rowlen for r in inp):
2729
raise ValueError("Invalid board")
2830
# Unknown character in board
@@ -31,9 +33,3 @@ def verify_board(inp):
3133
cset.update(r)
3234
if cset - set('+- *|'):
3335
raise ValueError("Invalid board")
34-
# Borders not as expected
35-
if any(inp[i1] != '+' + '-' * (rowlen - 2) + '+'
36-
for i1 in [0, -1]) or any(inp[i1][i2] != '|'
37-
for i1 in range(1, collen - 1)
38-
for i2 in [0, -1]):
39-
raise ValueError("Invalid board")

exercises/minesweeper/minesweeper_test.py

Lines changed: 87 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -14,193 +14,136 @@
1414
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0
1515

1616
class MinesweeperTest(unittest.TestCase):
17-
# Tests in canonical data incompatible with board class
17+
1818
def test_no_rows(self):
19-
with self.assertRaises(ValueError):
20-
board([])
19+
self.assertEqual(board([]), [])
2120

2221
def test_no_columns(self):
23-
with self.assertRaises(ValueError):
24-
board([""])
22+
self.assertEqual(board([""]), [""])
2523

2624
def test_no_mines(self):
27-
inp = ["+---+",
28-
"| |",
29-
"| |",
30-
"| |",
31-
"+---+"]
32-
out = ["+---+",
33-
"| |",
34-
"| |",
35-
"| |",
36-
"+---+"]
25+
inp = [" ",
26+
" ",
27+
" "]
28+
out = [" ",
29+
" ",
30+
" "]
3731
self.assertEqual(board(inp), out)
3832

3933
def test_board_with_only_mines(self):
40-
inp = ["+---+",
41-
"|***|",
42-
"|***|",
43-
"|***|",
44-
"+---+"]
45-
out = ["+---+",
46-
"|***|",
47-
"|***|",
48-
"|***|",
49-
"+---+"]
34+
inp = ["***",
35+
"***",
36+
"***"]
37+
out = ["***",
38+
"***",
39+
"***"]
5040
self.assertEqual(board(inp), out)
5141

5242
def test_mine_surrounded_by_spaces(self):
53-
inp = ["+---+",
54-
"| |",
55-
"| * |",
56-
"| |",
57-
"+---+"]
58-
out = ["+---+",
59-
"|111|",
60-
"|1*1|",
61-
"|111|",
62-
"+---+"]
43+
inp = [" ",
44+
" * ",
45+
" "]
46+
out = ["111",
47+
"1*1",
48+
"111"]
6349
self.assertEqual(board(inp), out)
6450

6551
def test_space_surrounded_by_mines(self):
66-
inp = ["+---+",
67-
"|***|",
68-
"|* *|",
69-
"|***|",
70-
"+---+"]
71-
out = ["+---+",
72-
"|***|",
73-
"|*8*|",
74-
"|***|",
75-
"+---+"]
52+
inp = ["***",
53+
"* *",
54+
"***"]
55+
out = ["***",
56+
"*8*",
57+
"***"]
7658
self.assertEqual(board(inp), out)
7759

7860
def test_horizontal_line(self):
79-
inp = ["+-----+",
80-
"| * * |",
81-
"+-----+"]
82-
out = ["+-----+",
83-
"|1*2*1|",
84-
"+-----+"]
61+
inp = [" * * "]
62+
out = ["1*2*1"]
8563
self.assertEqual(board(inp), out)
8664

8765
def test_horizontal_line_mines_at_edges(self):
88-
inp = ["+-----+",
89-
"|* *|",
90-
"+-----+"]
91-
out = ["+-----+",
92-
"|*1 1*|",
93-
"+-----+"]
66+
inp = ["* *"]
67+
out = ["*1 1*"]
9468
self.assertEqual(board(inp), out)
9569

9670
def test_vertical_line(self):
97-
inp = ["+-+",
98-
"| |",
99-
"|*|",
100-
"| |",
101-
"|*|",
102-
"| |",
103-
"+-+"]
104-
out = ["+-+",
105-
"|1|",
106-
"|*|",
107-
"|2|",
108-
"|*|",
109-
"|1|",
110-
"+-+"]
71+
inp = [" ",
72+
"*",
73+
" ",
74+
"*",
75+
" "]
76+
out = ["1",
77+
"*",
78+
"2",
79+
"*",
80+
"1"]
11181
self.assertEqual(board(inp), out)
11282

11383
def test_vertical_line_mines_at_edges(self):
114-
inp = ["+-+",
115-
"|*|",
116-
"| |",
117-
"| |",
118-
"| |",
119-
"|*|",
120-
"+-+"]
121-
out = ["+-+",
122-
"|*|",
123-
"|1|",
124-
"| |",
125-
"|1|",
126-
"|*|",
127-
"+-+"]
84+
inp = ["*",
85+
" ",
86+
" ",
87+
" ",
88+
"*"]
89+
out = ["*",
90+
"1",
91+
" ",
92+
"1",
93+
"*"]
12894
self.assertEqual(board(inp), out)
12995

13096
def test_cross(self):
131-
inp = ["+-----+",
132-
"| * |",
133-
"| * |",
134-
"|*****|",
135-
"| * |",
136-
"| * |",
137-
"+-----+"]
138-
out = ["+-----+",
139-
"| 2*2 |",
140-
"|25*52|",
141-
"|*****|",
142-
"|25*52|",
143-
"| 2*2 |",
144-
"+-----+"]
97+
inp = [" * ",
98+
" * ",
99+
"*****",
100+
" * ",
101+
" * "]
102+
out = [" 2*2 ",
103+
"25*52",
104+
"*****",
105+
"25*52",
106+
" 2*2 "]
145107
self.assertEqual(board(inp), out)
146108

147109
def test_large_board(self):
148-
inp = ["+------+",
149-
"| * * |",
150-
"| * |",
151-
"| * |",
152-
"| * *|",
153-
"| * * |",
154-
"| |",
155-
"+------+"]
156-
out = ["+------+",
157-
"|1*22*1|",
158-
"|12*322|",
159-
"| 123*2|",
160-
"|112*4*|",
161-
"|1*22*2|",
162-
"|111111|",
163-
"+------+"]
110+
inp = [" * * ",
111+
" * ",
112+
" * ",
113+
" * *",
114+
" * * ",
115+
" "]
116+
out = ["1*22*1",
117+
"12*322",
118+
" 123*2",
119+
"112*4*",
120+
"1*22*2",
121+
"111111"]
164122
self.assertEqual(board(inp), out)
165123

166124
# Additional test for this track
167125
def test_board9(self):
168-
inp = ["+-----+",
169-
"| |",
170-
"| * |",
171-
"| |",
172-
"| |",
173-
"| * |",
174-
"+-----+"]
175-
out = ["+-----+",
176-
"| 111|",
177-
"| 1*1|",
178-
"| 111|",
179-
"|111 |",
180-
"|1*1 |",
181-
"+-----+"]
126+
inp = [" ",
127+
" * ",
128+
" ",
129+
" ",
130+
" * "]
131+
out = [" 111",
132+
" 1*1",
133+
" 111",
134+
"111 ",
135+
"1*1 "]
182136
self.assertEqual(board(inp), out)
183137

184138
def test_different_len(self):
185-
inp = ["+-+",
186-
"| |",
187-
"|* |",
188-
"| |",
189-
"+-+"]
190-
with self.assertRaises(ValueError):
191-
board(inp)
192-
193-
def test_faulty_border(self):
194-
inp = ["+-----+",
195-
"* * |",
196-
"+-- --+"]
139+
inp = [" ",
140+
"* ",
141+
" "]
197142
with self.assertRaises(ValueError):
198143
board(inp)
199144

200145
def test_invalid_char(self):
201-
inp = ["+-----+",
202-
"|X * |",
203-
"+-----+"]
146+
inp = ["X * "]
204147
with self.assertRaises(ValueError):
205148
board(inp)
206149

0 commit comments

Comments
 (0)