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

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

1 точки общо

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

 1import re
 2
 3def parse_wishlist(wishlists_str):
 4    wishlists = []
 5
 6    lines = wishlists_str.strip().split('\n')
 7
 8    i = 0
 9    while i < len(lines):
10        line = lines[i].strip()
11        if line.startswith('('):
12            coefficient, name, age = parse_header(line)
13
14            # Extract wishes
15            i += 1
16            wishes = []
17            while i < len(lines) and not lines[i].strip().startswith('('):
18                wish_line = lines[i].strip()
19                if wish_line.startswith('-') or wish_line.startswith('*'):
20                    wish = wish_line[1:].strip()
21                    if wish:  # Exclude empty wishes
22                        wishes.append(wish)
23                i += 1
24
25            wishlists.append((coefficient, name, age, tuple(wishes)))
26        else:
27            i += 1
28
29    return wishlists
30
31def parse_header(header):
32    # Extract coefficient
33    coefficient_match = re.match(r'\((\d+\.\d+)\)', header)
34    coefficient = float(coefficient_match.group(1)) if coefficient_match else None
35
36    # Extract name and age using a simpler approach
37    parts = header[header.find(')')+1:].split('[')
38    name = parts[0].strip()
39    age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None
40
41    return coefficient, name, age

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[142 chars])), (6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[336 chars]м'))] != [(3.1[142 chars])), (None, 'Mr. D-r', 999, ('[Doctor stuff] Ск[340 chars]м'))]

First differing element 2:
(6.9, 'Mr. D-r', 999, ('[Doctor stuff] Ска[72 chars]пе'))
(None, 'Mr. D-r', 999, ('[Doctor stuff] Ск[73 chars]пе'))

[(3.14, 'Иван Иванов', 10, ('Плейстейшан', 'Количка с дистанционо', 'Братче')),
(1.94, 'Georgi "Jorkata" Georgiev', 43, ('Vancheto ot tretiq etaj',)),
- (6.9,
+ (None,
'Mr. D-r',
999,
('[Doctor stuff] Скалпел',
'[Doctor stuff] Мехлем за дупе',
'[Non-doctor stuff] Мехлем за дупе')),
- (1,
+ (None,
'Gospodin (he/him) Mitko',
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
22
t3def parse_wishlists(wishlists_str):t3def parse_wishlist(wishlists_str):
4    wishlists = []4    wishlists = []
55
6    lines = wishlists_str.strip().split('\n')6    lines = wishlists_str.strip().split('\n')
77
8    i = 08    i = 0
9    while i < len(lines):9    while i < len(lines):
10        line = lines[i].strip()10        line = lines[i].strip()
11        if line.startswith('('):11        if line.startswith('('):
12            coefficient, name, age = parse_header(line)12            coefficient, name, age = parse_header(line)
1313
14            # Extract wishes14            # Extract wishes
15            i += 115            i += 1
16            wishes = []16            wishes = []
17            while i < len(lines) and not lines[i].strip().startswith('('):17            while i < len(lines) and not lines[i].strip().startswith('('):
18                wish_line = lines[i].strip()18                wish_line = lines[i].strip()
19                if wish_line.startswith('-') or wish_line.startswith('*'):19                if wish_line.startswith('-') or wish_line.startswith('*'):
20                    wish = wish_line[1:].strip()20                    wish = wish_line[1:].strip()
21                    if wish:  # Exclude empty wishes21                    if wish:  # Exclude empty wishes
22                        wishes.append(wish)22                        wishes.append(wish)
23                i += 123                i += 1
2424
25            wishlists.append((coefficient, name, age, tuple(wishes)))25            wishlists.append((coefficient, name, age, tuple(wishes)))
26        else:26        else:
27            i += 127            i += 1
2828
29    return wishlists29    return wishlists
3030
31def parse_header(header):31def parse_header(header):
32    # Extract coefficient32    # Extract coefficient
33    coefficient_match = re.match(r'\((\d+\.\d+)\)', header)33    coefficient_match = re.match(r'\((\d+\.\d+)\)', header)
34    coefficient = float(coefficient_match.group(1)) if coefficient_match else None34    coefficient = float(coefficient_match.group(1)) if coefficient_match else None
3535
36    # Extract name and age using a simpler approach36    # Extract name and age using a simpler approach
37    parts = header[header.find(')')+1:].split('[')37    parts = header[header.find(')')+1:].split('[')
38    name = parts[0].strip()38    name = parts[0].strip()
39    age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None39    age = int(re.search(r'(\d+)', parts[1]).group()) if len(parts) > 1 and re.search(r'(\d+)', parts[1]) else None
4040
41    return coefficient, name, age41    return coefficient, name, age
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op