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

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

2 точки общо

2 успешни теста
1 неуспешни теста
Код

1import re
2def parse_wishlist(wishlists):
3    pattern_per_person = r'\s*\(\s*(.*[\d\.]+\s*)\)\s+(.+)\[\s*(\d+).*\]\s*\n+((?:\s*[-|\*]\s+.+[\n|\s]*)*)'
4    patter_wishes = r'\s*[\*|-](.*)\n*'
5
6    return [(float(match[0]), match[1].strip(), int(match[2]), 
7             tuple(wish.strip() for wish in re.findall(patter_wishes, match[3]) if wish.strip() != "")) 
8             for match in re.findall(pattern_per_person, 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[264 chars]), (1, 'Gospodin (he/him) Mitko', 12, ('1', '2[214 chars]м'))] != [(3.1[264 chars]), (1.0, 'Gospodin (he/him) Mitko', 12, ('1',)[163 chars]м'))]

First differing element 3:
(1, 'Gospodin (he/him) Mitko', 12, ('1', '2[47 chars]st'))
(1.0, 'Gospodin (he/him) Mitko', 12, ('1',))

[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
(6.9,
'Mr. D-r',
999,
('[Doctor stuff] Скалпел',
'[Doctor stuff] Мехлем за дупе',
'[Non-doctor stuff] Мехлем за дупе')),
- (1,
- 'Gospodin (he/him) Mitko',
+ (1.0, 'Gospodin (he/him) Mitko', 12, ('1',)),
? +++++ +++++++++++++

- 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)

Дискусия
История

f1import ref1import re
2def parse_wishlist(wishlists):2def parse_wishlist(wishlists):
t3    pattern_per_person = r'\(([\d.]+)\)\s+(.+)\[(\d+).*\]\s*\n+((?:\s*[-|\*]\s+.+\n*)*)'t3    pattern_per_person = r'\s*\(\s*(.*[\d\.]+\s*)\)\s+(.+)\[\s*(\d+).*\]\s*\n+((?:\s*[-|\*]\s+.+[\n|\s]*)*)'
4    patter_wishes = r'\s*[\*|-](.*)\n*'4    patter_wishes = r'\s*[\*|-](.*)\n*'
55
6    return [(float(match[0]), match[1].strip(), int(match[2]), 6    return [(float(match[0]), match[1].strip(), int(match[2]), 
7             tuple(wish.strip() for wish in re.findall(patter_wishes, match[3]) if wish.strip() != "")) 7             tuple(wish.strip() for wish in re.findall(patter_wishes, match[3]) if wish.strip() != "")) 
8             for match in re.findall(pattern_per_person, wishlists)]8             for match in re.findall(pattern_per_person, wishlists)]
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op