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

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код
Скрий всички коментари

 1def get_price(shrubbery):
 2    total_cost = 0
 3
 4    for item in shrubbery['positional']:
 5        if 'cost' in item:
 6            total_cost += item['cost']
 7
 8    for value in shrubbery['named'].values():
 9        if 'cost' in value:
10            total_cost += value['cost']
11
12    return total_cost
13
14
15def get_all_valid_shrubs(*args, **kwargs):
16    valid_bush_names = ("храст", "shrub", "bush")
17    valid_shrubs = {'positional': [], 'named': {}}
18
19    for arg in args:
20        if (isinstance(arg, dict)
21                and 'name' in arg
22                and arg.get('name').lower() in valid_bush_names):
23            valid_shrubs['positional'].append(arg)
24
25    for key, value in kwargs.items():
26        if (isinstance(value, dict)
27                and 'name' in value
28                and value.get('name').lower() in valid_bush_names):
29            valid_shrubs['named'][key] = value
30
31    return valid_shrubs
32
33
34def function_that_says_ni(*args, **kwargs):
35    shrubbery_max_price = 42
36    shrubbery = get_all_valid_shrubs(*args,**kwargs)
37    shrubbery_price = get_price(shrubbery)
38
39    if shrubbery_price > shrubbery_max_price:
40        return 'Ni!'
41
42    unique_symbols = set(''.join(str(key) for key in shrubbery['named']))
43
44    if shrubbery_price == 0 or len(unique_symbols) % int(shrubbery_price) != 0:
45        return 'Ni!'
46
47    return f"{shrubbery_price:.2f}лв"

..E.......
======================================================================
ERROR: test_cost_whole_part_zero (test.TestNi.test_cost_whole_part_zero)
Test with a total cost part equal to zero.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 43, in test_cost_whole_part_zero
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 44, in function_that_says_ni
if shrubbery_price == 0 or len(unique_symbols) % int(shrubbery_price) != 0:
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

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

FAILED (errors=1)

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

Отвъд двете дребни забележки - хубаво решение.
История
Това решение има само една версия.