Skip to content

Commit 107cc5a

Browse files
authored
Merge branch 'master' into grains-changes
2 parents e052f92 + 09b499c commit 107cc5a

File tree

9 files changed

+91
-98
lines changed

9 files changed

+91
-98
lines changed

exercises/grep/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def format_files(matched_lines):
2323
return result
2424

2525

26-
def format_lines(matched_lines, files, flags):
26+
def format_lines(matched_lines, flags, files):
2727
result = []
2828

2929
for file_name, line_number, line in matched_lines:
@@ -42,7 +42,7 @@ def format_lines(matched_lines, files, flags):
4242
return ''.join(result)
4343

4444

45-
def grep(pattern, files, flags=''):
45+
def grep(pattern, flags, files):
4646
matched_lines = []
4747

4848
for file_name in files:
@@ -54,4 +54,4 @@ def grep(pattern, files, flags=''):
5454
if '-l' in flags:
5555
return format_files(matched_lines)
5656

57-
return format_lines(matched_lines, files, flags)
57+
return format_lines(matched_lines, flags, files)

exercises/grep/grep.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def grep(pattern, files, flags=''):
1+
def grep(pattern, flags, files):
22
pass

exercises/grep/grep_test.py

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -115,125 +115,106 @@ def tearDownClass(self):
115115

116116
def test_one_file_one_match_no_flags(self):
117117
self.assertMultiLineEqual(
118-
grep("Agamemnon", [ILIADFILENAME]),
118+
grep("Agamemnon", "", [ILIADFILENAME]),
119119
"Of Atreus, Agamemnon, King of men.\n"
120120
)
121121

122122
def test_one_file_one_match_print_line_numbers_flag(self):
123123
self.assertMultiLineEqual(
124-
grep("Forbidden", [PARADISELOSTFILENAME], "-n"),
124+
grep("Forbidden", "-n", [PARADISELOSTFILENAME]),
125125
"2:Of that Forbidden Tree, whose mortal tast\n"
126126
)
127127

128128
def test_one_file_one_match_case_insensitive_flag(self):
129129
self.assertMultiLineEqual(
130-
grep("FORBIDDEN", [PARADISELOSTFILENAME], "-i"),
130+
grep("FORBIDDEN", "-i", [PARADISELOSTFILENAME]),
131131
"Of that Forbidden Tree, whose mortal tast\n"
132132
)
133133

134134
def test_one_file_one_match_print_file_names_flag(self):
135135
self.assertMultiLineEqual(
136-
grep("Forbidden", [PARADISELOSTFILENAME], "-l"),
137-
PARADISELOSTFILENAME + '\n'
138-
)
136+
grep("Forbidden", "-l", [PARADISELOSTFILENAME]),
137+
PARADISELOSTFILENAME + '\n')
139138

140139
def test_one_file_one_match_match_entire_lines_flag(self):
141140
self.assertMultiLineEqual(
142141
grep("With loss of Eden, till one greater Man",
143-
[PARADISELOSTFILENAME], "-x"),
144-
"With loss of Eden, till one greater Man\n"
145-
)
142+
"-x", [PARADISELOSTFILENAME]),
143+
"With loss of Eden, till one greater Man\n")
146144

147145
def test_one_file_one_match_multiple_flags(self):
148146
self.assertMultiLineEqual(
149-
grep(
150-
"OF ATREUS, Agamemnon, KIng of MEN.",
151-
[ILIADFILENAME],
152-
"-n -i -x"
153-
),
154-
"9:Of Atreus, Agamemnon, King of men.\n"
155-
)
147+
grep("OF ATREUS, Agamemnon, KIng of MEN.",
148+
"-n -i -x", [ILIADFILENAME]),
149+
"9:Of Atreus, Agamemnon, King of men.\n")
156150

157151
def test_one_file_several_matches_no_flags(self):
158152
self.assertMultiLineEqual(
159-
grep("may", [MIDSUMMERNIGHTFILENAME]),
153+
grep("may", "", [MIDSUMMERNIGHTFILENAME]),
160154
"Nor how it may concern my modesty,\n"
161155
"But I beseech your grace that I may know\n"
162-
"The worst that may befall me in this case,\n"
163-
)
156+
"The worst that may befall me in this case,\n")
164157

165158
def test_one_file_several_matches_print_line_numbers_flag(self):
166159
self.assertMultiLineEqual(
167-
grep("may", [MIDSUMMERNIGHTFILENAME], "-n"),
160+
grep("may", "-n", [MIDSUMMERNIGHTFILENAME]),
168161
"3:Nor how it may concern my modesty,\n"
169162
"5:But I beseech your grace that I may know\n"
170-
"6:The worst that may befall me in this case,\n"
171-
)
163+
"6:The worst that may befall me in this case,\n")
172164

173165
def test_one_file_several_matches_match_entire_lines_flag(self):
174166
self.assertMultiLineEqual(
175-
grep("may", [MIDSUMMERNIGHTFILENAME], "-x"),
176-
""
177-
)
167+
grep("may", "-x", [MIDSUMMERNIGHTFILENAME]),
168+
"")
178169

179170
def test_one_file_several_matches_case_insensitive_flag(self):
180171
self.assertMultiLineEqual(
181-
grep("ACHILLES", [ILIADFILENAME], "-i"),
172+
grep("ACHILLES", "-i", [ILIADFILENAME]),
182173
"Achilles sing, O Goddess! Peleus' son;\n"
183-
"The noble Chief Achilles from the son\n"
184-
)
174+
"The noble Chief Achilles from the son\n")
185175

186176
def test_one_file_several_matches_inverted_flag(self):
187177
self.assertMultiLineEqual(
188-
grep("Of", [PARADISELOSTFILENAME], "-v"),
178+
grep("Of", "-v", [PARADISELOSTFILENAME]),
189179
"Brought Death into the World, and all our woe,\n"
190180
"With loss of Eden, till one greater Man\n"
191181
"Restore us, and regain the blissful Seat,\n"
192182
"Sing Heav'nly Muse, that on the secret top\n"
193-
"That Shepherd, who first taught the chosen Seed\n"
194-
)
183+
"That Shepherd, who first taught the chosen Seed\n")
195184

196185
def test_one_file_one_match_file_flag_takes_precedence_over_line(self):
197186
self.assertMultiLineEqual(
198-
grep("ten", [ILIADFILENAME], "-n -l"),
199-
ILIADFILENAME + '\n'
200-
)
187+
grep("ten", "-n -l", [ILIADFILENAME]),
188+
ILIADFILENAME + '\n')
201189

202190
def test_one_file_no_matches_various_flags(self):
203191
self.assertMultiLineEqual(
204-
grep("Gandalf", [ILIADFILENAME], "-n -l -x -i"),
205-
""
206-
)
192+
grep("Gandalf", "-n -l -x -i", [ILIADFILENAME]),
193+
"")
207194

208195
def test_multiple_files_one_match_no_flags(self):
209196
self.assertMultiLineEqual(
210-
grep("Agamemnon", FILENAMES),
211-
"iliad.txt:Of Atreus, Agamemnon, King of men.\n"
212-
)
197+
grep("Agamemnon", "", FILENAMES),
198+
"iliad.txt:Of Atreus, Agamemnon, King of men.\n")
213199

214200
def test_multiple_files_several_matches_no_flags(self):
215201
self.assertMultiLineEqual(
216-
grep("may", FILENAMES),
202+
grep("may", "", FILENAMES),
217203
"midsummer-night.txt:Nor how it may concern my modesty,\n"
218204
"midsummer-night.txt:But I beseech your grace that I may know\n"
219-
"midsummer-night.txt:The worst that may befall me in this case,\n"
220-
)
205+
"midsummer-night.txt:The worst that may befall me in this case,\n")
221206

222207
def test_multiple_files_several_matches_print_line_numbers_flag(self):
223208
expected = (
224209
"midsummer-night.txt:5:But I beseech your grace that I may know\n"
225210
"midsummer-night.txt:6:The worst that may befall me in this case,"
226211
"\nparadise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
227-
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top\n"
228-
)
229-
self.assertMultiLineEqual(
230-
grep("that", FILENAMES, "-n"),
231-
expected
232-
)
212+
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top\n")
213+
self.assertMultiLineEqual(grep("that", "-n", FILENAMES), expected)
233214

234215
def test_multiple_files_one_match_print_file_names_flag(self):
235216
self.assertMultiLineEqual(
236-
grep("who", FILENAMES, "-l"),
217+
grep("who", "-l", FILENAMES),
237218
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')
238219

239220
def test_multiple_files_several_matches_case_insensitive_flag(self):
@@ -247,44 +228,38 @@ def test_multiple_files_several_matches_case_insensitive_flag(self):
247228
"\nmidsummer-night.txt:If I refuse to wed Demetrius.\n"
248229
"paradise-lost.txt:Brought Death into the World, and all our woe,"
249230
"\nparadise-lost.txt:Restore us, and regain the blissful Seat,\n"
250-
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n"
251-
)
252-
self.assertMultiLineEqual(
253-
grep("TO", FILENAMES, "-i"),
254-
expected
255-
)
231+
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n")
232+
self.assertMultiLineEqual(grep("TO", "-i", FILENAMES), expected)
256233

257234
def test_multiple_files_several_matches_inverted_flag(self):
258235
self.assertMultiLineEqual(
259-
grep("a", FILENAMES, "-v"),
236+
grep("a", "-v", FILENAMES),
260237
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
261238
"iliad.txt:The noble Chief Achilles from the son\n"
262239
"midsummer-night.txt:If I refuse to wed Demetrius.\n"
263240
)
264241

265242
def test_multiple_files_one_match_match_entire_lines_flag(self):
266243
self.assertMultiLineEqual(
267-
grep("But I beseech your grace that I may know",
268-
FILENAMES, "-x"),
244+
grep("But I beseech your grace that I may know", "-x", FILENAMES),
269245
"midsummer-night.txt:But I beseech your grace that I may know\n")
270246

271247
def test_multiple_files_one_match_multiple_flags(self):
272248
self.assertMultiLineEqual(
273-
grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN",
274-
FILENAMES, "-n -i -x"),
249+
grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN", "-n -i -x",
250+
FILENAMES),
275251
"paradise-lost.txt:4:With loss of Eden, till one greater Man\n")
276252

277253
def test_multiple_files_no_matches_various_flags(self):
278254
self.assertMultiLineEqual(
279-
grep("Frodo", FILENAMES, "-n -l -x -i"),
255+
grep("Frodo", "-n -l -x -i", FILENAMES),
280256
""
281257
)
282258

283259
def test_multiple_files_several_matches_file_flag_takes_precedence(self):
284260
self.assertMultiLineEqual(
285-
grep("who", FILENAMES, "-n -l"),
286-
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n'
287-
)
261+
grep("who", "-n -l", FILENAMES),
262+
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')
288263

289264
def test_multiple_files_several_matches_inverted_match_entire_lines(self):
290265
expected = (
@@ -314,7 +289,7 @@ def test_multiple_files_several_matches_inverted_match_entire_lines(self):
314289
"\n"
315290
)
316291
self.assertMultiLineEqual(
317-
grep("Illustrious into Ades premature,", FILENAMES, "-x -v"),
292+
grep("Illustrious into Ades premature,", "-x -v", FILENAMES),
318293
expected
319294
)
320295

exercises/isbn-verifier/example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
def verify(isbn):
1+
def is_valid(isbn):
22
chars = list(isbn.replace('-', ''))
33
if chars and chars[-1] == 'X':
44
chars[-1] = '10'
5-
if not len(chars) == 10 or not all(c.isdigit() for c in chars):
5+
if not len(chars) == 10 or not all(char.isdigit() for char in chars):
66
return False
77
indices = list(range(10, 0, -1))
8-
return sum(int(c) * i for c, i in zip(chars, indices)) % 11 == 0
8+
return sum(int(char) * idx for char, idx in zip(chars, indices)) % 11 == 0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def verify(isbn):
1+
def is_valid(isbn):
22
pass

exercises/isbn-verifier/isbn_verifier_test.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
import unittest
22

3-
from isbn_verifier import verify
3+
from isbn_verifier import is_valid
44

55

66
# Tests adapted from `problem-specifications//canonical-data.json` @ v2.7.0
77

88
class IsbnVerifierTest(unittest.TestCase):
99

1010
def test_valid_isbn_number(self):
11-
self.assertIs(verify('3-598-21508-8'), True)
11+
self.assertIs(is_valid('3-598-21508-8'), True)
1212

1313
def test_invalid_check_digit(self):
14-
self.assertIs(verify('3-598-21508-9'), False)
14+
self.assertIs(is_valid('3-598-21508-9'), False)
1515

1616
def test_valid_with_X_check_digit(self):
17-
self.assertIs(verify('3-598-21507-X'), True)
17+
self.assertIs(is_valid('3-598-21507-X'), True)
1818

1919
def test_invalid_check_digit_other_than_X(self):
20-
self.assertIs(verify('3-598-21507-A'), False)
20+
self.assertIs(is_valid('3-598-21507-A'), False)
2121

2222
def test_invalid_character_in_isbn(self):
23-
self.assertIs(verify('3-598-P1581-X'), False)
23+
self.assertIs(is_valid('3-598-P1581-X'), False)
2424

2525
def test_invalid_X_other_than_check_digit(self):
26-
self.assertIs(verify('3-598-2X507-9'), False)
26+
self.assertIs(is_valid('3-598-2X507-9'), False)
2727

2828
def test_valid_isbn_without_separating_dashes(self):
29-
self.assertIs(verify('3598215088'), True)
29+
self.assertIs(is_valid('3598215088'), True)
3030

3131
def test_valid_isbn_without_separating_dashes_with_X_check_digit(self):
32-
self.assertIs(verify('359821507X'), True)
32+
self.assertIs(is_valid('359821507X'), True)
3333

3434
def test_invalid_isbn_without_check_digit_and_dashes(self):
35-
self.assertIs(verify('359821507'), False)
35+
self.assertIs(is_valid('359821507'), False)
3636

3737
def test_invalid_too_long_isbn_with_no_dashes(self):
38-
self.assertIs(verify('3598215078X'), False)
38+
self.assertIs(is_valid('3598215078X'), False)
3939

4040
def test_invalid_too_short_isbn(self):
41-
self.assertIs(verify('00'), False)
41+
self.assertIs(is_valid('00'), False)
4242

4343
def test_invalid_isbn_without_check_digit(self):
44-
self.assertIs(verify('3-598-21507'), False)
44+
self.assertIs(is_valid('3-598-21507'), False)
4545

4646
def test_invalid_check_digit_X_used_for_0(self):
47-
self.assertIs(verify('3-598-21515-X'), False)
47+
self.assertIs(is_valid('3-598-21515-X'), False)
4848

4949
def test_valid_empty_isbn(self):
50-
self.assertIs(verify(''), False)
50+
self.assertIs(is_valid(''), False)
5151

5252
def test_input_is_nine_characters(self):
53-
self.assertIs(verify('134456729'), False)
53+
self.assertIs(is_valid('134456729'), False)
5454

5555
def test_invalid_characters_are_not_ignored(self):
56-
self.assertIs(verify('3132P34035'), False)
56+
self.assertIs(is_valid('3132P34035'), False)
5757

5858
def test_input_is_too_long_but_contains_a_valid_isbn(self):
59-
self.assertIs(verify('98245726788'), False)
59+
self.assertIs(is_valid('98245726788'), False)
6060

6161

6262
if __name__ == '__main__':

exercises/markdown/example.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ def parse_markdown(markdown):
55
lines = markdown.split('\n')
66
html = ''
77
in_list = False
8+
in_list_append = False
89
for line in lines:
9-
res = parse_line(line, in_list)
10+
res = parse_line(line, in_list, in_list_append)
1011
html += res['line']
1112
in_list = res['in_list']
13+
in_list_append = res['in_list_append']
1214
if in_list:
1315
html += '</ul>'
1416
return html
@@ -47,7 +49,7 @@ def check_italic(line):
4749
return None
4850

4951

50-
def parse_line(line, in_list):
52+
def parse_line(line, in_list, in_list_append):
5153
res = check_headers(line)
5254

5355
list_match = re.match(r'\* (.*)', res)
@@ -60,7 +62,7 @@ def parse_line(line, in_list):
6062
res = wrap(list_match.group(1), 'li')
6163
else:
6264
if in_list:
63-
res += '</ul>'
65+
in_list_append = True
6466
in_list = False
6567

6668
if not re.match('<h|<ul|<li', res):
@@ -74,7 +76,12 @@ def parse_line(line, in_list):
7476
while check_italic(res):
7577
res = check_italic(res)
7678

79+
if in_list_append:
80+
res = '</ul>' + res
81+
in_list_append = False
82+
7783
return {
7884
'line': res,
79-
'in_list': in_list
85+
'in_list': in_list,
86+
'in_list_append': in_list_append
8087
}

0 commit comments

Comments
 (0)