Предизвикателства > Регекс за Дядо Коледа > Решения > Решението на Веселина Велкова

Резултати
0 точки от тестове
1 точки от учител

1 точки общо

0 успешни теста
3 неуспешни теста
Код

 1import re
 2
 3
 4class Person:
 5    def __init__(self, index, name, age, wishes):
 6        self.name = name
 7        self.age = age
 8        self.index = index
 9        self.wishes = wishes
10
11def parse_wishlist(wishes):
12    wishes_lines = wishes.split('\n')
13    pattern = r'(^|\n)\((?P<index>\d+\.{0,1}\d*)\)[ ]+(?P<name>.+\b)[ ]+\[(?P<age>\d+).*\]\s*'
14    wishes_pattern = r'(^|\n)\s*(\*|\-)\s*(?P<wish>.+\b)'
15    compiled_pattern = re.compile(pattern)
16    compiled_wishes = re.compile(wishes_pattern)
17    poslushkovci = []
18    wishes = []
19    for line in wishes_lines:
20        match_person = compiled_pattern.match(line)
21        match_wish = compiled_wishes.match(line)
22        if match_person:
23            if wishes:
24                poslushkovci.append((poslushko.index, poslushko.name, poslushko.age, tuple(poslushko.wishes)))
25            wishes = []
26            poslushko = Person(float(match_person['index']), match_person['name'], int(match_person['age']), wishes)
27        elif match_wish and match_wish != '':
28            poslushko.wishes.append(match_wish['wish'])
29    poslushkovci.append((poslushko.index, poslushko.name, poslushko.age, tuple(poslushko.wishes)))

FFF
======================================================================
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: [('3.14', 'Иван Иванов', '10', ('Плейстей[112 chars]',))] != None

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: [(3.14, 'Иван Иванов', 10, ('Плейстейшан'[488 chars]м'))] != None

======================================================================
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: [('3.14', 'Иван Иванов', '10', ('Плейстей[112 chars]',))] != None

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: [(3.14, 'Иван Иванов', 10, ('Плейстейшан'[270 chars]м'))] != None

======================================================================
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: [('3.14', 'Иван Иванов', '10', ('Плейстей[112 chars]',))] != None

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: [(3.14, 'Иван Иванов', 10, ('Плейстейшан'[104 chars]',))] != None

----------------------------------------------------------------------
Ran 3 tests in 0.001s

FAILED (failures=3)

Дискусия
Виктор Бечев
27.12.2023 12:58

Then again, иначе е close enough _(режеш едни скоби)_. Ай, добре.
Виктор Бечев
27.12.2023 12:29

Липсва ти ретърн, Уви, дори и с него - пак не минават, иначе щях да ти дам поощрителна бонус точка.
История
Това решение има само една версия.