Skip to content

Commit 54390b1

Browse files
authored
house: return singleton list for single verse (#1354)
* house: return singleton list for single verse RE #1347 ([comment](#1347 (comment))) * house: fix example solution to pass changed tests
1 parent 54c9694 commit 54390b1

File tree

2 files changed

+86
-217
lines changed

2 files changed

+86
-217
lines changed

exercises/house/example.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
parts = [('lay in', 'the house that Jack built'),
1+
parts = [('lay in', 'the house that Jack built.'),
22
('ate', 'the malt'),
33
('killed', 'the rat'),
44
('worried', 'the cat'),
@@ -14,19 +14,13 @@
1414

1515
def verse(verse_num):
1616
v = ['This is {}'.format(parts[verse_num][1])]
17-
v.extend(['that {0} {1}'.format(parts[i][0], parts[i][1])
17+
v.extend(['that {0} {1}'.format(*parts[i])
1818
for i in range(verse_num - 1, -1, -1)])
19-
v[-1] += '.'
20-
return v
19+
return ''.join(v)
2120

2221

2322
def recite(start_verse, end_verse):
24-
if start_verse == end_verse:
25-
return verse(start_verse - 1)
26-
else:
27-
result = []
28-
for verse_num in range(start_verse-1, end_verse):
29-
result.extend(verse(verse_num))
30-
result.append("")
31-
result.pop()
32-
return result
23+
result = []
24+
for verse_num in range(start_verse-1, end_verse):
25+
result.append(verse(verse_num))
26+
return result

exercises/house/house_test.py

Lines changed: 79 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -14,267 +14,142 @@ def test_verse_one(self):
1414

1515
def test_verse_two(self):
1616
expected = [
17-
"This is the malt",
18-
"that lay in the house that Jack built.",
17+
"This is the malt"
18+
"that lay in the house that Jack built."
1919
]
2020
self.assertEqual(recite(2, 2), expected)
2121

2222
def test_verse_three(self):
2323
expected = [
24-
"This is the rat",
25-
"that ate the malt",
26-
"that lay in the house that Jack built.",
24+
"This is the rat"
25+
"that ate the malt"
26+
"that lay in the house that Jack built."
2727
]
2828
self.assertEqual(recite(3, 3), expected)
2929

3030
def test_verse_four(self):
3131
expected = [
32-
"This is the cat",
33-
"that killed the rat",
34-
"that ate the malt",
35-
"that lay in the house that Jack built.",
32+
"This is the cat"
33+
"that killed the rat"
34+
"that ate the malt"
35+
"that lay in the house that Jack built."
3636
]
3737
self.assertEqual(recite(4, 4), expected)
3838

3939
def test_verse_five(self):
4040
expected = [
41-
"This is the dog",
42-
"that worried the cat",
43-
"that killed the rat",
44-
"that ate the malt",
45-
"that lay in the house that Jack built.",
41+
"This is the dog"
42+
"that worried the cat"
43+
"that killed the rat"
44+
"that ate the malt"
45+
"that lay in the house that Jack built."
4646
]
4747
self.assertEqual(recite(5, 5), expected)
4848

4949
def test_verse_six(self):
5050
expected = [
51-
"This is the cow with the crumpled horn",
52-
"that tossed the dog",
53-
"that worried the cat",
54-
"that killed the rat",
55-
"that ate the malt",
56-
"that lay in the house that Jack built.",
51+
"This is the cow with the crumpled horn"
52+
"that tossed the dog"
53+
"that worried the cat"
54+
"that killed the rat"
55+
"that ate the malt"
56+
"that lay in the house that Jack built."
5757
]
5858
self.assertEqual(recite(6, 6), expected)
5959

6060
def test_verse_seven(self):
6161
expected = [
62-
"This is the maiden all forlorn",
63-
"that milked the cow with the crumpled horn",
64-
"that tossed the dog",
65-
"that worried the cat",
66-
"that killed the rat",
67-
"that ate the malt",
68-
"that lay in the house that Jack built.",
62+
"This is the maiden all forlorn"
63+
"that milked the cow with the crumpled horn"
64+
"that tossed the dog"
65+
"that worried the cat"
66+
"that killed the rat"
67+
"that ate the malt"
68+
"that lay in the house that Jack built."
6969
]
7070
self.assertEqual(recite(7, 7), expected)
7171

7272
def test_verse_eight(self):
7373
expected = [
74-
"This is the man all tattered and torn",
75-
"that kissed the maiden all forlorn",
76-
"that milked the cow with the crumpled horn",
77-
"that tossed the dog",
78-
"that worried the cat",
79-
"that killed the rat",
80-
"that ate the malt",
81-
"that lay in the house that Jack built.",
74+
"This is the man all tattered and torn"
75+
"that kissed the maiden all forlorn"
76+
"that milked the cow with the crumpled horn"
77+
"that tossed the dog"
78+
"that worried the cat"
79+
"that killed the rat"
80+
"that ate the malt"
81+
"that lay in the house that Jack built."
8282
]
8383
self.assertEqual(recite(8, 8), expected)
8484

8585
def test_verse_nine(self):
8686
expected = [
87-
"This is the priest all shaven and shorn",
88-
"that married the man all tattered and torn",
89-
"that kissed the maiden all forlorn",
90-
"that milked the cow with the crumpled horn",
91-
"that tossed the dog",
92-
"that worried the cat",
93-
"that killed the rat",
94-
"that ate the malt",
95-
"that lay in the house that Jack built.",
87+
"This is the priest all shaven and shorn"
88+
"that married the man all tattered and torn"
89+
"that kissed the maiden all forlorn"
90+
"that milked the cow with the crumpled horn"
91+
"that tossed the dog"
92+
"that worried the cat"
93+
"that killed the rat"
94+
"that ate the malt"
95+
"that lay in the house that Jack built."
9696
]
9797
self.assertEqual(recite(9, 9), expected)
9898

9999
def test_verse_10(self):
100100
expected = [
101-
"This is the rooster that crowed in the morn",
102-
"that woke the priest all shaven and shorn",
103-
"that married the man all tattered and torn",
104-
"that kissed the maiden all forlorn",
105-
"that milked the cow with the crumpled horn",
106-
"that tossed the dog",
107-
"that worried the cat",
108-
"that killed the rat",
109-
"that ate the malt",
110-
"that lay in the house that Jack built.",
101+
"This is the rooster that crowed in the morn"
102+
"that woke the priest all shaven and shorn"
103+
"that married the man all tattered and torn"
104+
"that kissed the maiden all forlorn"
105+
"that milked the cow with the crumpled horn"
106+
"that tossed the dog"
107+
"that worried the cat"
108+
"that killed the rat"
109+
"that ate the malt"
110+
"that lay in the house that Jack built."
111111
]
112112
self.assertEqual(recite(10, 10), expected)
113113

114114
def test_verse_11(self):
115115
expected = [
116-
"This is the farmer sowing his corn",
117-
"that kept the rooster that crowed in the morn",
118-
"that woke the priest all shaven and shorn",
119-
"that married the man all tattered and torn",
120-
"that kissed the maiden all forlorn",
121-
"that milked the cow with the crumpled horn",
122-
"that tossed the dog",
123-
"that worried the cat",
124-
"that killed the rat",
125-
"that ate the malt",
126-
"that lay in the house that Jack built.",
116+
"This is the farmer sowing his corn"
117+
"that kept the rooster that crowed in the morn"
118+
"that woke the priest all shaven and shorn"
119+
"that married the man all tattered and torn"
120+
"that kissed the maiden all forlorn"
121+
"that milked the cow with the crumpled horn"
122+
"that tossed the dog"
123+
"that worried the cat"
124+
"that killed the rat"
125+
"that ate the malt"
126+
"that lay in the house that Jack built."
127127
]
128128
self.assertEqual(recite(11, 11), expected)
129129

130130
def test_verse_12(self):
131131
expected = [
132-
"This is the horse and the hound and the horn",
133-
"that belonged to the farmer sowing his corn",
134-
"that kept the rooster that crowed in the morn",
135-
"that woke the priest all shaven and shorn",
136-
"that married the man all tattered and torn",
137-
"that kissed the maiden all forlorn",
138-
"that milked the cow with the crumpled horn",
139-
"that tossed the dog",
140-
"that worried the cat",
141-
"that killed the rat",
142-
"that ate the malt",
143-
"that lay in the house that Jack built.",
132+
"This is the horse and the hound and the horn"
133+
"that belonged to the farmer sowing his corn"
134+
"that kept the rooster that crowed in the morn"
135+
"that woke the priest all shaven and shorn"
136+
"that married the man all tattered and torn"
137+
"that kissed the maiden all forlorn"
138+
"that milked the cow with the crumpled horn"
139+
"that tossed the dog"
140+
"that worried the cat"
141+
"that killed the rat"
142+
"that ate the malt"
143+
"that lay in the house that Jack built."
144144
]
145145
self.assertEqual(recite(12, 12), expected)
146146

147147
def test_multiple_verses(self):
148-
expected = [
149-
"This is the cat",
150-
"that killed the rat",
151-
"that ate the malt",
152-
"that lay in the house that Jack built.",
153-
"",
154-
"This is the dog",
155-
"that worried the cat",
156-
"that killed the rat",
157-
"that ate the malt",
158-
"that lay in the house that Jack built.",
159-
"",
160-
"This is the cow with the crumpled horn",
161-
"that tossed the dog",
162-
"that worried the cat",
163-
"that killed the rat",
164-
"that ate the malt",
165-
"that lay in the house that Jack built.",
166-
"",
167-
"This is the maiden all forlorn",
168-
"that milked the cow with the crumpled horn",
169-
"that tossed the dog",
170-
"that worried the cat",
171-
"that killed the rat",
172-
"that ate the malt",
173-
"that lay in the house that Jack built.",
174-
"",
175-
"This is the man all tattered and torn",
176-
"that kissed the maiden all forlorn",
177-
"that milked the cow with the crumpled horn",
178-
"that tossed the dog",
179-
"that worried the cat",
180-
"that killed the rat",
181-
"that ate the malt",
182-
"that lay in the house that Jack built.",
183-
]
148+
expected = [recite(i, i)[0] for i in range(4, 9)]
184149
self.assertEqual(recite(4, 8), expected)
185150

186151
def test_full_rhyme(self):
187-
expected = [
188-
"This is the house that Jack built.",
189-
"",
190-
"This is the malt",
191-
"that lay in the house that Jack built.",
192-
"",
193-
"This is the rat",
194-
"that ate the malt",
195-
"that lay in the house that Jack built.",
196-
"",
197-
"This is the cat",
198-
"that killed the rat",
199-
"that ate the malt",
200-
"that lay in the house that Jack built.",
201-
"",
202-
"This is the dog",
203-
"that worried the cat",
204-
"that killed the rat",
205-
"that ate the malt",
206-
"that lay in the house that Jack built.",
207-
"",
208-
"This is the cow with the crumpled horn",
209-
"that tossed the dog",
210-
"that worried the cat",
211-
"that killed the rat",
212-
"that ate the malt",
213-
"that lay in the house that Jack built.",
214-
"",
215-
"This is the maiden all forlorn",
216-
"that milked the cow with the crumpled horn",
217-
"that tossed the dog",
218-
"that worried the cat",
219-
"that killed the rat",
220-
"that ate the malt",
221-
"that lay in the house that Jack built.",
222-
"",
223-
"This is the man all tattered and torn",
224-
"that kissed the maiden all forlorn",
225-
"that milked the cow with the crumpled horn",
226-
"that tossed the dog",
227-
"that worried the cat",
228-
"that killed the rat",
229-
"that ate the malt",
230-
"that lay in the house that Jack built.",
231-
"",
232-
"This is the priest all shaven and shorn",
233-
"that married the man all tattered and torn",
234-
"that kissed the maiden all forlorn",
235-
"that milked the cow with the crumpled horn",
236-
"that tossed the dog",
237-
"that worried the cat",
238-
"that killed the rat",
239-
"that ate the malt",
240-
"that lay in the house that Jack built.",
241-
"",
242-
"This is the rooster that crowed in the morn",
243-
"that woke the priest all shaven and shorn",
244-
"that married the man all tattered and torn",
245-
"that kissed the maiden all forlorn",
246-
"that milked the cow with the crumpled horn",
247-
"that tossed the dog",
248-
"that worried the cat",
249-
"that killed the rat",
250-
"that ate the malt",
251-
"that lay in the house that Jack built.",
252-
"",
253-
"This is the farmer sowing his corn",
254-
"that kept the rooster that crowed in the morn",
255-
"that woke the priest all shaven and shorn",
256-
"that married the man all tattered and torn",
257-
"that kissed the maiden all forlorn",
258-
"that milked the cow with the crumpled horn",
259-
"that tossed the dog",
260-
"that worried the cat",
261-
"that killed the rat",
262-
"that ate the malt",
263-
"that lay in the house that Jack built.",
264-
"",
265-
"This is the horse and the hound and the horn",
266-
"that belonged to the farmer sowing his corn",
267-
"that kept the rooster that crowed in the morn",
268-
"that woke the priest all shaven and shorn",
269-
"that married the man all tattered and torn",
270-
"that kissed the maiden all forlorn",
271-
"that milked the cow with the crumpled horn",
272-
"that tossed the dog",
273-
"that worried the cat",
274-
"that killed the rat",
275-
"that ate the malt",
276-
"that lay in the house that Jack built.",
277-
]
152+
expected = [recite(i, i)[0] for i in range(1, 13)]
278153
self.assertEqual(recite(1, 12), expected)
279154

280155

0 commit comments

Comments
 (0)