1import re
2
3def get_wishes(cur_str):
4 wishes = []
5 while True:
6 try:
7 wish_start = re.search(r'\s*(-|\*)\s*-*\s*', cur_str).end()
8 cur_str = cur_str[wish_start:]
9 except AttributeError:
10 break
11 wish_end = re.search(r'\n', cur_str)
12 wish_end = len(cur_str) if not wish_end else wish_end.start()
13 wishes.append(cur_str[:wish_end])
14 cur_str = cur_str[wish_end:]
15
16 return tuple(wishes)
17
18def generate_tuple(start, end, string):
19 cur_str = string[start:end]
20
21 match_coef = re.search(r'(?<=\()\d*\.\d*(?=\))', cur_str)
22 coef = float(cur_str[match_coef.start():match_coef.end()])
23
24 cur_str = cur_str[match_coef.end() + 1:]
25 match_name = re.search(r' +.* +(?=\[)', cur_str)
26 name = cur_str[match_name.start():match_name.end()].strip()
27
28 cur_str = cur_str[match_name.end():]
29 match_age = re.search(r'(?<=\[)\d*', cur_str)
30 age = int(cur_str[match_age.start():match_age.end()])
31
32 cur_str = cur_str[match_age.end() + 1:]
33 wishes = get_wishes(cur_str)
34
35 return (coef, name, age, wishes)
36
37def parse_wishlist(string):
38 result = []
39 braces = re.finditer(r'^\(', string, re.MULTILINE)
40 try:
41 prev_brace = next(braces)
42 except StopIteration:
43 return result
44
45 for cur_brace in braces:
46 result.append(generate_tuple(prev_brace.start(), cur_brace.start(), string))
47 prev_brace = cur_brace
48
49 result.append(generate_tuple(prev_brace.start(), len(string), string))
50 return result
EF.
======================================================================
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, ('Плейстейшан', [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))
File "/tmp/solution.py", line 46, in parse_wishlist
result.append(generate_tuple(prev_brace.start(), cur_brace.start(), string))
File "/tmp/solution.py", line 22, in generate_tuple
coef = float(cur_str[match_coef.start():match_coef.end()])
AttributeError: 'NoneType' object has no attribute 'start'
======================================================================
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, ('Плейстейшан', [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 113, in test_half
self.assertEqual(expected_half, parse_wishlist(half))
AssertionError: Lists differ: [(3.1[252 chars]о лего (тамън отвориха лицензиран магазин)', 'Хлороформ'))] != [(3.1[252 chars]о лего (тамън отвориха лицензиран магазин)', 'Хлороформ', ''))]
First differing element 2:
(99.9[100 chars]то лего (тамън отвориха лицензиран магазин)', 'Хлороформ'))
(99.9[100 chars]то лего (тамън отвориха лицензиран магазин)', 'Хлороформ', ''))
[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
(99.9534412345,
'На мама сладкото ангелче',
3,
('Най-хубавото дървено конче (размер 1.5)',
'Най-страхотното лего (тамън отвориха лицензиран магазин)',
- 'Хлороформ'))]
? ^^^
+ 'Хлороформ',
? ^
+ ''))]
----------------------------------------------------------------------
Ran 3 tests in 0.003s
FAILED (failures=1, errors=1)
Виктор Бечев
27.12.2023 11:40Имаш само една разлика, за да ти мине теста - един празен match накрая на единия елемент. Бонус точка. :)
**Едит:** Нямаш бонус точка, защото просто направих тестовете по-либерални и си я имаш от резултатите така или иначе.
|
| f | 1 | import re | f | 1 | import re |
| 2 | 2 | ||||
| 3 | def get_wishes(cur_str): | 3 | def get_wishes(cur_str): | ||
| 4 | wishes = [] | 4 | wishes = [] | ||
| 5 | while True: | 5 | while True: | ||
| 6 | try: | 6 | try: | ||
| 7 | wish_start = re.search(r'\s*(-|\*)\s*-*\s*', cur_str).end() | 7 | wish_start = re.search(r'\s*(-|\*)\s*-*\s*', cur_str).end() | ||
| 8 | cur_str = cur_str[wish_start:] | 8 | cur_str = cur_str[wish_start:] | ||
| 9 | except AttributeError: | 9 | except AttributeError: | ||
| 10 | break | 10 | break | ||
| 11 | wish_end = re.search(r'\n', cur_str) | 11 | wish_end = re.search(r'\n', cur_str) | ||
| 12 | wish_end = len(cur_str) if not wish_end else wish_end.start() | 12 | wish_end = len(cur_str) if not wish_end else wish_end.start() | ||
| 13 | wishes.append(cur_str[:wish_end]) | 13 | wishes.append(cur_str[:wish_end]) | ||
| 14 | cur_str = cur_str[wish_end:] | 14 | cur_str = cur_str[wish_end:] | ||
| 15 | 15 | ||||
| 16 | return tuple(wishes) | 16 | return tuple(wishes) | ||
| 17 | 17 | ||||
| 18 | def generate_tuple(start, end, string): | 18 | def generate_tuple(start, end, string): | ||
| 19 | cur_str = string[start:end] | 19 | cur_str = string[start:end] | ||
| 20 | 20 | ||||
| 21 | match_coef = re.search(r'(?<=\()\d*\.\d*(?=\))', cur_str) | 21 | match_coef = re.search(r'(?<=\()\d*\.\d*(?=\))', cur_str) | ||
| 22 | coef = float(cur_str[match_coef.start():match_coef.end()]) | 22 | coef = float(cur_str[match_coef.start():match_coef.end()]) | ||
| 23 | 23 | ||||
| 24 | cur_str = cur_str[match_coef.end() + 1:] | 24 | cur_str = cur_str[match_coef.end() + 1:] | ||
| 25 | match_name = re.search(r' +.* +(?=\[)', cur_str) | 25 | match_name = re.search(r' +.* +(?=\[)', cur_str) | ||
| 26 | name = cur_str[match_name.start():match_name.end()].strip() | 26 | name = cur_str[match_name.start():match_name.end()].strip() | ||
| 27 | 27 | ||||
| 28 | cur_str = cur_str[match_name.end():] | 28 | cur_str = cur_str[match_name.end():] | ||
| 29 | match_age = re.search(r'(?<=\[)\d*', cur_str) | 29 | match_age = re.search(r'(?<=\[)\d*', cur_str) | ||
| 30 | age = int(cur_str[match_age.start():match_age.end()]) | 30 | age = int(cur_str[match_age.start():match_age.end()]) | ||
| 31 | 31 | ||||
| 32 | cur_str = cur_str[match_age.end() + 1:] | 32 | cur_str = cur_str[match_age.end() + 1:] | ||
| 33 | wishes = get_wishes(cur_str) | 33 | wishes = get_wishes(cur_str) | ||
| 34 | 34 | ||||
| 35 | return (coef, name, age, wishes) | 35 | return (coef, name, age, wishes) | ||
| 36 | 36 | ||||
| t | 37 | def parse_wishlists(string): | t | 37 | def parse_wishlist(string): |
| 38 | result = [] | 38 | result = [] | ||
| 39 | braces = re.finditer(r'^\(', string, re.MULTILINE) | 39 | braces = re.finditer(r'^\(', string, re.MULTILINE) | ||
| 40 | try: | 40 | try: | ||
| 41 | prev_brace = next(braces) | 41 | prev_brace = next(braces) | ||
| 42 | except StopIteration: | 42 | except StopIteration: | ||
| 43 | return result | 43 | return result | ||
| 44 | 44 | ||||
| 45 | for cur_brace in braces: | 45 | for cur_brace in braces: | ||
| 46 | result.append(generate_tuple(prev_brace.start(), cur_brace.start(), string)) | 46 | result.append(generate_tuple(prev_brace.start(), cur_brace.start(), string)) | ||
| 47 | prev_brace = cur_brace | 47 | prev_brace = cur_brace | ||
| 48 | 48 | ||||
| 49 | result.append(generate_tuple(prev_brace.start(), len(string), string)) | 49 | result.append(generate_tuple(prev_brace.start(), len(string), string)) | ||
| 50 | return result | 50 | return result |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||