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

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

1 точки общо

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

 1import re
 2import copy
 3
 4first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]'
 5
 6
 7def helper_function(text):
 8    lines = text.split('\n')
 9    matches = []
10    current_wishes = []
11
12    for i in range(1, len(lines)):
13        # using strip() to remove trailing spaces
14        current_line = lines[i].strip()
15        next_line = lines[i + 1].strip() if i + 1 < len(lines) else ""
16
17        current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE)
18
19        if current_match and current_match.group(1):
20            current_wishes.append(current_match.group(1))
21        # if the next line is the start of the wishes of another kid
22        if re.match(first_row_regex, next_line):
23            matches.append(copy.deepcopy(current_wishes))
24            current_wishes.clear()
25
26    matches.append(copy.deepcopy(current_wishes))
27    return matches
28
29
30def parse_wishlist(santas_list):
31    niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE)
32    names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE)
33    ages = re.findall(r'\[\D*(\d+)\D*]', santas_list, re.MULTILINE)
34    wishes = helper_function(santas_list)
35
36    return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
37            range(len(niceness_factors))]

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]',))]

First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))

- [('3.14',
? - -

+ [(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 36, in parse_wishlist
return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
File "/tmp/solution.py", line 36, in <listcomp>
return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
IndexError: list index out of range

======================================================================
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]',))]

First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))

- [('3.14',
? - -

+ [(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[13 chars]ов', 10, ('Плейстейшан', 'Количка с дистанцион[247 chars]м'))] != [(3.1[13 chars]ов', '10', ('Плейстейшан', 'Количка с дистанци[253 chars]м'))]

First differing element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))

+ [(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',)),
? + +

(99.9534412345,
'На мама сладкото ангелче',
- 3,
+ '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]',))]

First differing element 0:
('3.14', 'Иван Иванов', '10', ('Плейстейша[34 chars]че'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан'[32 chars]че'))

- [('3.14',
? - -

+ [(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[13 chars]ов', 10, ('Плейстейшан', 'Количка с дистанцион[81 chars]',))] != [(3.1[13 chars]ов', '10', ('Плейстейшан', 'Количка с дистанци[85 chars]',))]

First differing element 0:
(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче'))
(3.14, 'Иван Иванов', '10', ('Плейстейшан', 'Количка с дистанционо', 'Братче'))

+ [(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',))]
? + +

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

FAILED (failures=2, errors=1)

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

Липсва ти само една конверсия на годините към число, затова ти фейлват 2/3 теста (третият просто е по-кофти). Бонус точка.
История

f1import ref1import re
2import copy2import copy
33
4first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]'4first_row_regex = r'\(.*?\)[\w\s"\'-]+\[.*?\]'
55
66
7def helper_function(text):7def helper_function(text):
8    lines = text.split('\n')8    lines = text.split('\n')
9    matches = []9    matches = []
10    current_wishes = []10    current_wishes = []
1111
12    for i in range(1, len(lines)):12    for i in range(1, len(lines)):
13        # using strip() to remove trailing spaces13        # using strip() to remove trailing spaces
14        current_line = lines[i].strip()14        current_line = lines[i].strip()
15        next_line = lines[i + 1].strip() if i + 1 < len(lines) else ""15        next_line = lines[i + 1].strip() if i + 1 < len(lines) else ""
1616
17        current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE)17        current_match = re.search(r'^[-*]\s*(.*)', current_line, re.MULTILINE)
1818
19        if current_match and current_match.group(1):19        if current_match and current_match.group(1):
20            current_wishes.append(current_match.group(1))20            current_wishes.append(current_match.group(1))
21        # if the next line is the start of the wishes of another kid21        # if the next line is the start of the wishes of another kid
22        if re.match(first_row_regex, next_line):22        if re.match(first_row_regex, next_line):
23            matches.append(copy.deepcopy(current_wishes))23            matches.append(copy.deepcopy(current_wishes))
24            current_wishes.clear()24            current_wishes.clear()
2525
26    matches.append(copy.deepcopy(current_wishes))26    matches.append(copy.deepcopy(current_wishes))
27    return matches27    return matches
2828
2929
30def parse_wishlist(santas_list):30def parse_wishlist(santas_list):
31    niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE)31    niceness_factors = re.findall(r'^\((.*?)\)', santas_list, re.MULTILINE)
32    names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE)32    names = re.findall(r'\)([\w\s\"\'-]+)\[', santas_list, re.MULTILINE)
t33    ages = re.findall(r'\[(\d+)\D*]', santas_list, re.MULTILINE)t33    ages = re.findall(r'\[\D*(\d+)\D*]', santas_list, re.MULTILINE)
34    wishes = helper_function(santas_list)34    wishes = helper_function(santas_list)
3535
36    return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in36    return [(float(niceness_factors[i]), names[i].strip(), ages[i], tuple(wishes[i])) for i in
37            range(len(niceness_factors))]37            range(len(niceness_factors))]
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op