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

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

9 точки общо

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

 1def is_a_valid_bush_dict (bush):
 2    if "name" in bush and type(bush["name"]) == str:
 3        name = bush["name"].lower()
 4        #make sure that all are lowercase
 5        if name in {"храст", "shrub", "bush"}:
 6            cost = bush.get("cost", 0) #default cost set to zero
 7            if type(cost) == float or type(cost) == int:
 8                return True
 9
10    return False
11
12def unique_letters_count(keys):
13    unique_letters = set()
14    for key in keys:
15        unique_letters.update(set(key)) #saw this function online
16    return len(unique_letters)
17
18def function_that_says_ni (*args, **kwargs):
19    total_cost = 0.0
20    shrub_names = []
21
22    for element in args: #iterate to get the valid shrubs
23        if type(element) == dict and is_a_valid_bush_dict(element):
24            total_cost += element.get("cost", 0.0)
25
26    for key, value in kwargs.items():
27        if type(value) == dict and is_a_valid_bush_dict(value):
28            total_cost += value.get("cost", 0.0)
29            shrub_names.append(key)
30
31    if total_cost > 42.00:
32        return "Ni!"
33
34    unique_letters = unique_letters_count(shrub_names)
35    integer_part_of_cost = int(total_cost)
36
37    if total_cost == 0 or unique_letters % integer_part_of_cost != 0:
38        return "Ni!"
39
40
41    return f"{total_cost:.2f}лв" #saw this on the web because i wasnt sure how to do it

..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 37, in function_that_says_ni
if total_cost == 0 or unique_letters % integer_part_of_cost != 0:
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

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

FAILED (errors=1)

Дискусия
История
Това решение има само една версия.