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

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

1 точки общо

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

 1import re
 2
 3
 4def parse_wishlist(christams_wishes):
 5    list_of_wishes = []
 6    splitted_wishes = re.split('^\(',christams_wishes,0,re.MULTILINE)
 7    for wish in splitted_wishes:
 8       all_wishes =[]
 9       if wish:
10            number = float( re.search(r"[-+]?\d*\.?\d+|\d+", wish).group(0))
11            name = re.search(r"^.*\)(.*)\[.*$",wish, re.MULTILINE).group(1).strip()
12            age = int(re.search(r"\[(\d{1,3})(.*)\]",wish, re.MULTILINE).group(1).strip())
13            wishes = re.split(r"- | \*  |\n", wish)
14
15            for index in range(1,len(wishes)):
16                wishes[index]=wishes[index].replace("\n", "").strip()
17                wishes[index]=wishes[index].replace("*", "").strip()
18
19                if wishes[index]:
20                    all_wishes.append(wishes[index])
21
22            all_wishes = tuple(all_wishes)
23            list_of_wishes.append((number, name, age, all_wishes))
24
25    return list_of_wishes

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

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 12, in parse_wishlist
age = int(re.search(r"\[(\d{1,3})(.*)\]",wish, re.MULTILINE).group(1).strip())
AttributeError: 'NoneType' object has no attribute 'group'

======================================================================
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, ('Плейстейшан', [106 chars]j'))]

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[112 chars]3, ('Vancheto ot tretiq etaj',)), (99.95344123[148 chars]м'))] != [(3.1[112 chars]3, ('-', 'Vancheto ot tretiq etaj')), (99.9534[152 chars]м'))]

First differing element 1:
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('-', 'Vancheto ot tretiq etaj'))

[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
? -

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

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 103, in test_modified_example
self.assertEqual(expected_example, parse_wishlist(modified_example))
AssertionError: Lists differ: [(3.1[86 chars]rgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))] != [(3.1[86 chars]rgi "Jorkata" Georgiev', 43, ('-', 'Vancheto ot tretiq etaj'))]

First differing element 1:
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('-', 'Vancheto ot tretiq etaj'))

[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
- (1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',))]
? -

+ (1.94, 'Georgi "Jorkata" Georgiev', 43, ('-', 'Vancheto ot tretiq etaj'))]
? +++++

----------------------------------------------------------------------
Ran 3 tests in 0.004s

FAILED (failures=2, errors=1)

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

Тъй-тъй сме почнали с подаръците - разликите на по-малките тестове са ти false positive match-ове на празните тирета - `(1.94, 'Georgi "Jorkata" Georgiev', 43, ('-', 'Vancheto ot tretiq etaj')),`.
История
Това решение има само една версия.