1import re
2
3def parse_wishlist(wishlists_str):
4 wishlists = []
5
6 lines = wishlists_str.strip().split('\n')
7
8 i = 0
9 while i < len(lines):
10 line = lines[i].strip()
11 if line.startswith('('):
12 coefficient, name, age = parse_header(line)
13
14 # Extract wishes
15 i += 1
16 wishes = []
17 while i < len(lines) and not lines[i].strip().startswith('('):
18 wish_line = lines[i].strip()
19 if wish_line.startswith('-') or wish_line.startswith('*'):
20 wish = wish_line[1:].strip()
21 if wish: # Exclude empty wishes
22 wishes.append(wish)
23 i += 1
24
25 wishlists.append((coefficient, name, age, tuple(wishes)))
26 else:
27 i += 1
28
29 return wishlists
30
31def parse_header(header):
32 # Extract coefficient
33 coefficient_match = re.match(r'\((\d+\.\d+)\)', header)
34 coefficient = float(coefficient_match.group(1)) if coefficient_match else None
35
36 # Extract name and age using a simpler approach
37 parts = header[header.find(')')+1:].split('[')
38 name = parts[0].strip()
39 age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None
40
41 return coefficient, name, age
F..
======================================================================
FAIL: 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, ('Плейстейшан', [102 chars]',))]
First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', 10, ('Плейстейшан', [30 chars]че'))
- [('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',))]
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))
AssertionError: Lists differ: [(3.1[142 chars])), (6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[336 chars]м'))] != [(3.1[142 chars])), (None, 'Mr. D-r', 999, ('[Doctor stuff] Ск[340 chars]м'))]
First differing element 2:
(6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[72 chars]пе'))
(None, 'Mr. D-r', 999, ('[Doctor stuff] Ск[73 chars]пе'))
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
- (6.9,
+ (None,
'Mr. D-r',
999,
('[Doctor stuff] Скалпел',
'[Doctor stuff] Мехлем за дупе',
'[Non-doctor stuff] Мехлем за дупе')),
- (1,
+ (None,
'Gospodin (he/him) Mitko',
12,
('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test')),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
----------------------------------------------------------------------
Ran 3 tests in 0.003s
FAILED (failures=1)
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
t | 3 | def parse_wishlists(wishlists_str): | t | 3 | def parse_wishlist(wishlists_str): |
4 | wishlists = [] | 4 | wishlists = [] | ||
5 | 5 | ||||
6 | lines = wishlists_str.strip().split('\n') | 6 | lines = wishlists_str.strip().split('\n') | ||
7 | 7 | ||||
8 | i = 0 | 8 | i = 0 | ||
9 | while i < len(lines): | 9 | while i < len(lines): | ||
10 | line = lines[i].strip() | 10 | line = lines[i].strip() | ||
11 | if line.startswith('('): | 11 | if line.startswith('('): | ||
12 | coefficient, name, age = parse_header(line) | 12 | coefficient, name, age = parse_header(line) | ||
13 | 13 | ||||
14 | # Extract wishes | 14 | # Extract wishes | ||
15 | i += 1 | 15 | i += 1 | ||
16 | wishes = [] | 16 | wishes = [] | ||
17 | while i < len(lines) and not lines[i].strip().startswith('('): | 17 | while i < len(lines) and not lines[i].strip().startswith('('): | ||
18 | wish_line = lines[i].strip() | 18 | wish_line = lines[i].strip() | ||
19 | if wish_line.startswith('-') or wish_line.startswith('*'): | 19 | if wish_line.startswith('-') or wish_line.startswith('*'): | ||
20 | wish = wish_line[1:].strip() | 20 | wish = wish_line[1:].strip() | ||
21 | if wish: # Exclude empty wishes | 21 | if wish: # Exclude empty wishes | ||
22 | wishes.append(wish) | 22 | wishes.append(wish) | ||
23 | i += 1 | 23 | i += 1 | ||
24 | 24 | ||||
25 | wishlists.append((coefficient, name, age, tuple(wishes))) | 25 | wishlists.append((coefficient, name, age, tuple(wishes))) | ||
26 | else: | 26 | else: | ||
27 | i += 1 | 27 | i += 1 | ||
28 | 28 | ||||
29 | return wishlists | 29 | return wishlists | ||
30 | 30 | ||||
31 | def parse_header(header): | 31 | def parse_header(header): | ||
32 | # Extract coefficient | 32 | # Extract coefficient | ||
33 | coefficient_match = re.match(r'\((\d+\.\d+)\)', header) | 33 | coefficient_match = re.match(r'\((\d+\.\d+)\)', header) | ||
34 | coefficient = float(coefficient_match.group(1)) if coefficient_match else None | 34 | coefficient = float(coefficient_match.group(1)) if coefficient_match else None | ||
35 | 35 | ||||
36 | # Extract name and age using a simpler approach | 36 | # Extract name and age using a simpler approach | ||
37 | parts = header[header.find(')')+1:].split('[') | 37 | parts = header[header.find(')')+1:].split('[') | ||
38 | name = parts[0].strip() | 38 | name = parts[0].strip() | ||
39 | age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None | 39 | age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None | ||
40 | 40 | ||||
41 | return coefficient, name, age | 41 | return coefficient, name, age |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|