1import re
2
3START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+(((\S)+(\ )*)+)\[([0-9]+).*\]'
4# START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+((.[^\[])+)'
5
6
7def parse_wishlist(wishlists_to_parse: str):
8 starter_lines_matches = re.findall(START_PATTERN, wishlists_to_parse)
9 remove_list = [item for tupl in starter_lines_matches for item in tupl]
10 wishes_strings = [item for item in re.split(START_PATTERN, wishlists_to_parse) if item not in remove_list and item != ""]
11
12 wishlists = []
13
14 for (match, wishes_str) in zip(starter_lines_matches, wishes_strings):
15 coef = float(match[0])
16 name = str(match[2]).strip()
17 age = int(match[6])
18 # print(f"{coef} {name} {age}")
19
20 wishes_list = []
21
22 for wish in wishes_str.splitlines():
23 # print(result[0] if len(result) > 0 else '')
24 if parsed_wish := re.findall(r'\s*[-*]\s*(.+)\n*', wish):
25 wishes_list.append(parsed_wish[0])
26
27 wishlists.append((coef, name, age, tuple(wishes_list)))
28
29 return wishlists
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[137 chars]taj',)), (6.9, 'Mr. D-r', 999, ('[Doctor stuff[341 chars]м'))] != [(3.1[137 chars]taj', 'r [ 999 ]', '[Doctor stuff] Скалпел', '[290 chars]м'))]
First differing element 1:
(1.94[57 chars]taj',))
(1.94[57 chars]taj', 'r [ 999 ]', '[Doctor stuff] Скалпел', '[123 chars]st'))
First list contains 2 additional elements.
First extra element 3:
(1, 'Gospodin (he/him) Mitko', 12, ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test'))
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
- (6.9,
? ^
+ (1.94,
? ^ +
- 'Mr. D-r',
- 999,
+ 'Georgi "Jorkata" Georgiev',
+ 43,
+ ('Vancheto ot tretiq etaj',
+ 'r [ 999 ]',
- ('[Doctor stuff] Скалпел',
? ^
+ '[Doctor stuff] Скалпел',
? ^
'[Doctor stuff] Мехлем за дупе',
- '[Non-doctor stuff] Мехлем за дупе')),
? --
+ '[Non-doctor stuff] Мехлем за дупе',
- (1,
- 'Gospodin (he/him) Mitko',
- 12,
+ '1',
+ '2',
+ '3',
+ '4',
- ('1', '2', '3', '4', 'Malko mi pisva da go pisha toq test')),
? --------------------
+ 'Malko mi pisva da go pisha toq test')),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
'Хлороформ'))]
----------------------------------------------------------------------
Ran 3 tests in 0.005s
FAILED (failures=1)
Виктор Бечев
27.12.2023 12:53Бонус точка за нелошо решение, в комбинация с това, че беше първи.
|
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+(((\S)+(\ )*)+)\[([0-9]+).*\]' | 3 | START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+(((\S)+(\ )*)+)\[([0-9]+).*\]' | ||
4 | # START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+((.[^\[])+)' | 4 | # START_PATTERN = r'\(([0-9]+(.[0-9]+))?\)\s+((.[^\[])+)' | ||
5 | 5 | ||||
6 | 6 | ||||
t | 7 | def parse_wishlists(wishlists_to_parse: str): | t | 7 | def parse_wishlist(wishlists_to_parse: str): |
8 | starter_lines_matches = re.findall(START_PATTERN, wishlists_to_parse) | 8 | starter_lines_matches = re.findall(START_PATTERN, wishlists_to_parse) | ||
9 | remove_list = [item for tupl in starter_lines_matches for item in tupl] | 9 | remove_list = [item for tupl in starter_lines_matches for item in tupl] | ||
10 | wishes_strings = [item for item in re.split(START_PATTERN, wishlists_to_parse) if item not in remove_list and item != ""] | 10 | wishes_strings = [item for item in re.split(START_PATTERN, wishlists_to_parse) if item not in remove_list and item != ""] | ||
11 | 11 | ||||
12 | wishlists = [] | 12 | wishlists = [] | ||
13 | 13 | ||||
14 | for (match, wishes_str) in zip(starter_lines_matches, wishes_strings): | 14 | for (match, wishes_str) in zip(starter_lines_matches, wishes_strings): | ||
15 | coef = float(match[0]) | 15 | coef = float(match[0]) | ||
16 | name = str(match[2]).strip() | 16 | name = str(match[2]).strip() | ||
17 | age = int(match[6]) | 17 | age = int(match[6]) | ||
18 | # print(f"{coef} {name} {age}") | 18 | # print(f"{coef} {name} {age}") | ||
19 | 19 | ||||
20 | wishes_list = [] | 20 | wishes_list = [] | ||
21 | 21 | ||||
22 | for wish in wishes_str.splitlines(): | 22 | for wish in wishes_str.splitlines(): | ||
23 | # print(result[0] if len(result) > 0 else '') | 23 | # print(result[0] if len(result) > 0 else '') | ||
24 | if parsed_wish := re.findall(r'\s*[-*]\s*(.+)\n*', wish): | 24 | if parsed_wish := re.findall(r'\s*[-*]\s*(.+)\n*', wish): | ||
25 | wishes_list.append(parsed_wish[0]) | 25 | wishes_list.append(parsed_wish[0]) | ||
26 | 26 | ||||
27 | wishlists.append((coef, name, age, tuple(wishes_list))) | 27 | wishlists.append((coef, name, age, tuple(wishes_list))) | ||
28 | 28 | ||||
29 | return wishlists | 29 | return wishlists |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|