Домашни > Функцията, която казва "Ni!" > Решения > Решението на Силвана Николова

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код (Промених някои дребни работи според коментарите)

 1def calculate_price(price_list):
 2    total_price = 0.00
 3    integer_price = 0
 4
 5    for price in price_list:
 6        total_price += price
 7        integer_price += int(price)
 8    
 9    return (total_price, integer_price)
10
11def check_names(name_list, price):
12    if price == 0:
13        return False
14    
15    letters = set()
16    for name in name_list:
17        for ch in name:
18            letters.add(ch)
19
20    return len(letters) % price == 0
21
22def function_that_says_ni(*args, **kwargs):
23    prices = []
24    names = []
25    bush_names = ('храст', 'shrub', 'bush')
26
27    for arg in args:
28        if type(arg) is not dict:
29            continue
30        if 'name' not in arg:
31            continue
32        if str(arg['name']).lower() not in bush_names:
33            continue
34        if 'cost' in arg:
35            prices.append(arg.get('cost', 0))
36    
37
38    for arg_name, arg in kwargs.items():
39        if type(arg) is not dict:
40            continue
41        if 'name' not in arg:
42            continue
43        if str(arg['name']).lower() not in bush_names:
44            continue
45        if 'cost' in arg:
46            prices.append(arg.get('cost', 0))
47        names.append(arg_name)
48    
49    price, integer_price = calculate_price(prices)
50    if price > 42:
51        return 'Ni!'
52
53    if check_names(names, integer_price):
54        return f'{format(price, '.2f')}лв'
55    else:
56        return 'Ni!'

......F...
======================================================================
FAIL: test_named_arguments (test.TestNi.test_named_arguments)
Test with named arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 50, in test_named_arguments
self.assertEqual(function_that_says_ni(_abcde={'name': 'shrub', 'cost': 2.82},
AssertionError: 'Ni!' != '7.33лв'
- Ni!
+ 7.33лв

----------------------------------------------------------------------
Ran 10 tests in 0.001s

FAILED (failures=1)

Дискусия
Виктор Бечев
21.10.2024 19:27

Отвъд дребните коментари по-горе - решението ти е чисто и структурирано.
История

f1def calculate_price(price_list):f1def calculate_price(price_list):
2    total_price = 0.002    total_price = 0.00
3    integer_price = 03    integer_price = 0
44
5    for price in price_list:5    for price in price_list:
6        total_price += price6        total_price += price
7        integer_price += int(price)7        integer_price += int(price)
8    8    
9    return (total_price, integer_price)9    return (total_price, integer_price)
1010
11def check_names(name_list, price):11def check_names(name_list, price):
12    if price == 0:12    if price == 0:
13        return False13        return False
14    14    
15    letters = set()15    letters = set()
16    for name in name_list:16    for name in name_list:
17        for ch in name:17        for ch in name:
n18            if (ch >= 'a' and ch <= 'z') or (ch == '_'):n
19                letters.add(ch)18            letters.add(ch)
2019
21    return len(letters) % price == 020    return len(letters) % price == 0
2221
23def function_that_says_ni(*args, **kwargs):22def function_that_says_ni(*args, **kwargs):
24    prices = []23    prices = []
25    names = []24    names = []
26    bush_names = ('храст', 'shrub', 'bush')25    bush_names = ('храст', 'shrub', 'bush')
2726
28    for arg in args:27    for arg in args:
29        if type(arg) is not dict:28        if type(arg) is not dict:
30            continue29            continue
31        if 'name' not in arg:30        if 'name' not in arg:
32            continue31            continue
33        if str(arg['name']).lower() not in bush_names:32        if str(arg['name']).lower() not in bush_names:
34            continue33            continue
35        if 'cost' in arg:34        if 'cost' in arg:
n36            prices.append(arg['cost'])n35            prices.append(arg.get('cost', 0))
37    36    
3837
39    for arg_name, arg in kwargs.items():38    for arg_name, arg in kwargs.items():
40        if type(arg) is not dict:39        if type(arg) is not dict:
41            continue40            continue
42        if 'name' not in arg:41        if 'name' not in arg:
43            continue42            continue
44        if str(arg['name']).lower() not in bush_names:43        if str(arg['name']).lower() not in bush_names:
45            continue44            continue
46        if 'cost' in arg:45        if 'cost' in arg:
n47            prices.append(arg['cost'])n46            prices.append(arg.get('cost', 0))
48        names.append(arg_name)47        names.append(arg_name)
49    48    
50    price, integer_price = calculate_price(prices)49    price, integer_price = calculate_price(prices)
n51    if price > 42.00:n50    if price > 42:
52        return 'Ni!'51        return 'Ni!'
5352
54    if check_names(names, integer_price):53    if check_names(names, integer_price):
n55        formatted_price = format(price, '.2f')n
56        return f'{formatted_price}лв'54        return f'{format(price, '.2f')}лв'
57    else:55    else:
58        return 'Ni!'56        return 'Ni!'
t59 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op