1import re
2
3def parse_wishlist(input_string):
4 pattern = r'((.?))\s(.?)\s[(.?)]\s([\s\S]?)(?=([\d.])|$)'
5 matches = re.findall(pattern, input_string)
6
7 result = []
8 for match in matches:
9 coefficient = float(match[0])
10 name = match[1].strip()
11 age_match = re.search(r'(\d+)\s', match[2])
12 age = int(age_match.group(1)) if age_match else None
13
14 wishes = re.findall(r'[-][\s[^\n]]*(.+?)(?=\n|$)', match[3]) # absolutely no idea why [\s[^\n]]* works, but \s* does not
15 result.append((coefficient, name, age, tuple(wishes)))
16
17 return result
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', ('Плейстейш[111 chars]',))] != []
First list contains 2 additional elements.
First extra element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ []
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('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 9, in parse_wishlist
coefficient = float(match[0])
ValueError: could not convert string to float: 'j'
======================================================================
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', ('Плейстейш[111 chars]',))] != []
First list contains 2 additional elements.
First extra element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ []
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('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.14, 'Иван Иванов', 10, ('Плейстейшан',[269 chars]м'))] != []
First list contains 3 additional elements.
First extra element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ []
- [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
- (99.9534412345,
- 'На мама сладкото ангелче',
- 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', ('Плейстейш[111 chars]',))] != []
First list contains 2 additional elements.
First extra element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ []
- [('3.14',
- 'Иван Иванов',
- '10',
- ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- ('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.14, 'Иван Иванов', 10, ('Плейстейшан',[103 chars]',))] != []
First list contains 2 additional elements.
First extra element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
+ []
- [(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))]
----------------------------------------------------------------------
Ran 3 tests in 0.001s
FAILED (failures=2, errors=1)
Атанас Христов
27.12.2023 14:53А това дето викаш че не работи даже с примера е странно, помня че го тествах и работеше преди да го кача. 🤔
|
Атанас Христов
27.12.2023 14:46Хах ами дискретните не са ми сила ама изобщо и учих regex-и от вашата презентация за точно един цял ден. Та няма какво да се очаква XD. То и сега съм на ски и не мога да го видя да го оправя. Като се върна в софия ще го погледна защо не работи.
|
Виктор Бечев
27.12.2023 13:07Много ми се иска и на теб да дам бонус точка, но на другите с фейлващи тестове им липсва някоя скоба, някой интервал.
При теб прекарах 10-тина минути да фиксвам _(не да го пренаписвам, това убива смисъла в упражнението)_ и пак е трудно да се доближа до коректен match.
Некст гейм е некст гейм, дет' вика Ицата...
|
Виктор Бечев
27.12.2023 11:51То даже с примера не работи, буквално всеки път връща празен списък. ;(
|
Атанас Христов
23.12.2023 00:48Утре ще се опитам да го докарам до one liner :^)
|
| t | t | 1 | import re | ||
| 2 | |||||
| 1 | def parse_wishlist(wishlist): | 3 | def parse_wishlist(input_string): | ||
| 2 | wishlist += '\n\n' | 4 | pattern = r'((.?))\s(.?)\s[(.?)]\s([\s\S]?)(?=([\d.])|$)' | ||
| 3 | xd = [] | 5 | matches = re.findall(pattern, input_string) | ||
| 4 | coefBool = False | 6 | |||
| 5 | nameBool = False | 7 | result = [] | ||
| 6 | ageBool = False | 8 | for match in matches: | ||
| 7 | wishesBool = False | 9 | coefficient = float(match[0]) | ||
| 8 | foundWish = False | 10 | name = match[1].strip() | ||
| 9 | coef = '' | 11 | age_match = re.search(r'(\d+)\s', match[2]) | ||
| 10 | name = '' | 12 | age = int(age_match.group(1)) if age_match else None | ||
| 11 | age = '' | 13 | |||
| 12 | wish = '' | 14 | wishes = re.findall(r'[-][\s[^\n]]*(.+?)(?=\n|$)', match[3]) # absolutely no idea why [\s[^\n]]* works, but \s* does not | ||
| 13 | wishes = [] | 15 | result.append((coefficient, name, age, tuple(wishes))) | ||
| 14 | counter = 0 | 16 | |||
| 15 | char_count = len(wishlist) | 17 | return result | ||
| 16 | for char in wishlist: | ||||
| 17 | counter += 1 | ||||
| 18 | if (char == '(' and not foundWish) or counter == char_count: | ||||
| 19 | if coef != '': | ||||
| 20 | xd.append(tuple([float(coef), str(name.strip()), int(age), tuple(str(w.strip()) for w in wishes)])) | ||||
| 21 | wishesBool = False | ||||
| 22 | coefBool = True | ||||
| 23 | coef = '' | ||||
| 24 | elif wishesBool: | ||||
| 25 | if char == '\n': | ||||
| 26 | if wish and not wish.isspace(): | ||||
| 27 | wishes.append(str(wish)) | ||||
| 28 | wish = '' | ||||
| 29 | foundWish = False | ||||
| 30 | elif char in '-*' and not foundWish: | ||||
| 31 | foundWish = True | ||||
| 32 | wish = '' | ||||
| 33 | elif foundWish: | ||||
| 34 | wish += char | ||||
| 35 | elif char == ')': | ||||
| 36 | coefBool = False | ||||
| 37 | nameBool = True | ||||
| 38 | name = '' | ||||
| 39 | coef = float(coef) | ||||
| 40 | elif coefBool: | ||||
| 41 | coef += char | ||||
| 42 | elif char == '[': | ||||
| 43 | nameBool = False | ||||
| 44 | ageBool = True | ||||
| 45 | age = '' | ||||
| 46 | elif nameBool: | ||||
| 47 | name += char | ||||
| 48 | elif char == ']': | ||||
| 49 | ageBool = False | ||||
| 50 | pure_age = '' | ||||
| 51 | age = int(pure_age.join([x for x in age if x in '0123456789'])) | ||||
| 52 | elif ageBool: | ||||
| 53 | age += char | ||||
| 54 | elif char == '\n': | ||||
| 55 | wishesBool = True | ||||
| 56 | wishes = [] | ||||
| 57 | return list(xd) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| t | 1 | def parse_wishlists(wishlist): | t | 1 | def parse_wishlist(wishlist): |
| 2 | wishlist += '\n\n' | 2 | wishlist += '\n\n' | ||
| 3 | xd = [] | 3 | xd = [] | ||
| 4 | coefBool = False | 4 | coefBool = False | ||
| 5 | nameBool = False | 5 | nameBool = False | ||
| 6 | ageBool = False | 6 | ageBool = False | ||
| 7 | wishesBool = False | 7 | wishesBool = False | ||
| 8 | foundWish = False | 8 | foundWish = False | ||
| 9 | coef = '' | 9 | coef = '' | ||
| 10 | name = '' | 10 | name = '' | ||
| 11 | age = '' | 11 | age = '' | ||
| 12 | wish = '' | 12 | wish = '' | ||
| 13 | wishes = [] | 13 | wishes = [] | ||
| 14 | counter = 0 | 14 | counter = 0 | ||
| 15 | char_count = len(wishlist) | 15 | char_count = len(wishlist) | ||
| 16 | for char in wishlist: | 16 | for char in wishlist: | ||
| 17 | counter += 1 | 17 | counter += 1 | ||
| 18 | if (char == '(' and not foundWish) or counter == char_count: | 18 | if (char == '(' and not foundWish) or counter == char_count: | ||
| 19 | if coef != '': | 19 | if coef != '': | ||
| 20 | xd.append(tuple([float(coef), str(name.strip()), int(age), tuple(str(w.strip()) for w in wishes)])) | 20 | xd.append(tuple([float(coef), str(name.strip()), int(age), tuple(str(w.strip()) for w in wishes)])) | ||
| 21 | wishesBool = False | 21 | wishesBool = False | ||
| 22 | coefBool = True | 22 | coefBool = True | ||
| 23 | coef = '' | 23 | coef = '' | ||
| 24 | elif wishesBool: | 24 | elif wishesBool: | ||
| 25 | if char == '\n': | 25 | if char == '\n': | ||
| 26 | if wish and not wish.isspace(): | 26 | if wish and not wish.isspace(): | ||
| 27 | wishes.append(str(wish)) | 27 | wishes.append(str(wish)) | ||
| 28 | wish = '' | 28 | wish = '' | ||
| 29 | foundWish = False | 29 | foundWish = False | ||
| 30 | elif char in '-*' and not foundWish: | 30 | elif char in '-*' and not foundWish: | ||
| 31 | foundWish = True | 31 | foundWish = True | ||
| 32 | wish = '' | 32 | wish = '' | ||
| 33 | elif foundWish: | 33 | elif foundWish: | ||
| 34 | wish += char | 34 | wish += char | ||
| 35 | elif char == ')': | 35 | elif char == ')': | ||
| 36 | coefBool = False | 36 | coefBool = False | ||
| 37 | nameBool = True | 37 | nameBool = True | ||
| 38 | name = '' | 38 | name = '' | ||
| 39 | coef = float(coef) | 39 | coef = float(coef) | ||
| 40 | elif coefBool: | 40 | elif coefBool: | ||
| 41 | coef += char | 41 | coef += char | ||
| 42 | elif char == '[': | 42 | elif char == '[': | ||
| 43 | nameBool = False | 43 | nameBool = False | ||
| 44 | ageBool = True | 44 | ageBool = True | ||
| 45 | age = '' | 45 | age = '' | ||
| 46 | elif nameBool: | 46 | elif nameBool: | ||
| 47 | name += char | 47 | name += char | ||
| 48 | elif char == ']': | 48 | elif char == ']': | ||
| 49 | ageBool = False | 49 | ageBool = False | ||
| 50 | pure_age = '' | 50 | pure_age = '' | ||
| 51 | age = int(pure_age.join([x for x in age if x in '0123456789'])) | 51 | age = int(pure_age.join([x for x in age if x in '0123456789'])) | ||
| 52 | elif ageBool: | 52 | elif ageBool: | ||
| 53 | age += char | 53 | age += char | ||
| 54 | elif char == '\n': | 54 | elif char == '\n': | ||
| 55 | wishesBool = True | 55 | wishesBool = True | ||
| 56 | wishes = [] | 56 | wishes = [] | ||
| 57 | return list(xd) | 57 | return list(xd) |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||