1import re
2import copy
3
4first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]'
5
6
7def helper_function(text):
8 lines = text.split('\n')
9 matches = []
10 current_wishes = []
11
12 for i in range(1, len(lines)):
13 # using strip() to remove trailing spaces
14 current_line = lines[i].strip()
15 next_line = lines[i + 1].strip() if i + 1 < len(lines) else ""
16
17 current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE)
18
19 if current_match and current_match.group(1):
20 current_wishes.append(current_match.group(1))
21 # if the next line is the start of the wishes of another kid
22 if re.match(first_row_regex, next_line):
23 matches.append(copy.deepcopy(current_wishes))
24 current_wishes.clear()
25
26 matches.append(copy.deepcopy(current_wishes))
27 return matches
28
29
30def parse_wishlist(santas_list):
31 niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE)
32 names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE)
33 ages = re.findall(r'\[\D*(\d+)\D*]', santas_list, re.MULTILINE)
34 wishes = helper_function(santas_list)
35
36 return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
37 range(len(niceness_factors))]
EFF
======================================================================
ERROR: test_full (test.TestRegex)
A single test to rule them all.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 121, in test_full
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', '10', ('Плейстейшан'[106 chars]',))]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))
- [('3.14',
? - -
+ [(3.14,
'Иван Иванов',
'10',
('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - -
+ (1.94, 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 123, in test_full
self.assertEqual(expected_full, parse_wishlist(full))
File "/tmp/solution.py", line 36, in parse_wishlist
return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
File "/tmp/solution.py", line 36, in <listcomp>
return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
IndexError: list index out of range
======================================================================
FAIL: test_half (test.TestRegex)
A test without the more edgy cases.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 111, in test_half
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', '10', ('Плейстейшан'[106 chars]',))]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))
- [('3.14',
? - -
+ [(3.14,
'Иван Иванов',
'10',
('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - -
+ (1.94, 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 113, in test_half
self.assertEqual(expected_half, parse_wishlist(half))
AssertionError: Lists differ: [(3.1[13 chars]ов', 10, ('Плейстейшан', 'Количка с дистанцион[247 chars]м'))] != [(3.1[13 chars]ов', '10', ('Плейстейшан', 'Количка с дистанци[253 chars]м'))]
First differing element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ [(3.14,
+ 'Иван Иванов',
+ '10',
- [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^^^^^^^^^^^^^^^^^^^^^^^^^^
+ ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
+ (1.94, 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',)),
? + +
(99.9534412345,
'На мама сладкото ангелче',
- 3,
+ '3',
? + +
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
======================================================================
FAIL: test_modified_example (test.TestRegex)
A test similar to the example (just a lil' bit different).
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 101, in test_modified_example
self.assertEqual(converted, parse_wishlist(modified_example))
AssertionError: Lists differ: [('3.14', 'Иван Иванов', '10', ('Плейстейша[110 chars]',))] != [(3.14, 'Иван Иванов', '10', ('Плейстейшан'[106 chars]',))]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))
- [('3.14',
? - -
+ [(3.14,
'Иван Иванов',
'10',
('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('1.94', 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? - -
+ (1.94, 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/tmp/test.py", line 103, in test_modified_example
self.assertEqual(expected_example, parse_wishlist(modified_example))
AssertionError: Lists differ: [(3.1[13 chars]ов', 10, ('Плейстейшан', 'Количка с дистанцион[81 chars]',))] != [(3.1[13 chars]ов', '10', ('Плейстейшан', 'Количка с дистанци[85 chars]',))]
First differing element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ [(3.14,
+ 'Иван Иванов',
+ '10',
- [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^^^^^^^^^^^^^^^^^^^^^^^^^^
+ ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
? ^
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))]
+ (1.94, 'Georgi "Jorkata" Georgiev', '43', ('Vancheto ot tretiq etaj',))]
? + +
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (failures=2, errors=1)
Виктор Бечев
27.12.2023 11:54Липсва ти само една конверсия на годините към число, затова ти фейлват 2/3 теста (третият просто е по-кофти). Бонус точка.
|
f | 1 | import re | f | 1 | import re |
2 | import copy | 2 | import copy | ||
3 | 3 | ||||
4 | first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]' | 4 | first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]' | ||
5 | 5 | ||||
6 | 6 | ||||
7 | def helper_function(text): | 7 | def helper_function(text): | ||
8 | lines = text.split('\n') | 8 | lines = text.split('\n') | ||
9 | matches = [] | 9 | matches = [] | ||
10 | current_wishes = [] | 10 | current_wishes = [] | ||
11 | 11 | ||||
12 | for i in range(1, len(lines)): | 12 | for i in range(1, len(lines)): | ||
13 | # using strip() to remove trailing spaces | 13 | # using strip() to remove trailing spaces | ||
14 | current_line = lines[i].strip() | 14 | current_line = lines[i].strip() | ||
15 | next_line = lines[i + 1].strip() if i + 1 < len(lines) else "" | 15 | next_line = lines[i + 1].strip() if i + 1 < len(lines) else "" | ||
16 | 16 | ||||
17 | current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE) | 17 | current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE) | ||
18 | 18 | ||||
19 | if current_match and current_match.group(1): | 19 | if current_match and current_match.group(1): | ||
20 | current_wishes.append(current_match.group(1)) | 20 | current_wishes.append(current_match.group(1)) | ||
21 | # if the next line is the start of the wishes of another kid | 21 | # if the next line is the start of the wishes of another kid | ||
22 | if re.match(first_row_regex, next_line): | 22 | if re.match(first_row_regex, next_line): | ||
23 | matches.append(copy.deepcopy(current_wishes)) | 23 | matches.append(copy.deepcopy(current_wishes)) | ||
24 | current_wishes.clear() | 24 | current_wishes.clear() | ||
25 | 25 | ||||
26 | matches.append(copy.deepcopy(current_wishes)) | 26 | matches.append(copy.deepcopy(current_wishes)) | ||
27 | return matches | 27 | return matches | ||
28 | 28 | ||||
29 | 29 | ||||
30 | def parse_wishlist(santas_list): | 30 | def parse_wishlist(santas_list): | ||
31 | niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE) | 31 | niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE) | ||
32 | names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE) | 32 | names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE) | ||
t | 33 | ages = re.findall(r'\[(\d+)\D*]', santas_list, re.MULTILINE) | t | 33 | ages = re.findall(r'\[\D*(\d+)\D*]', santas_list, re.MULTILINE) |
34 | wishes = helper_function(santas_list) | 34 | wishes = helper_function(santas_list) | ||
35 | 35 | ||||
36 | return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in | 36 | return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in | ||
37 | range(len(niceness_factors))] | 37 | range(len(niceness_factors))] |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|