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

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

10 точки общо

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

 1def is_bush(potential_bush, valid_names):
 2    name = potential_bush.get("name", "").lower()
 3    return name in valid_names
 4
 5
 6def is_not_too_expensive(sum_of_costs):
 7    return sum_of_costs <= 42.00
 8
 9
10def looks_nice(sum_of_costs, unique_symbols):
11    if int(sum_of_costs) != 0:
12        return len(unique_symbols) % int(sum_of_costs) == 0
13    else:
14        return False
15
16
17def function_that_says_ni(*args, **kwargs):
18    valid_names = ["храст", "shrub", "bush"]
19    sum_of_costs = 0
20    unique_symbols = set()
21
22    for arg in args:
23        if isinstance(arg, dict) and is_bush(arg, valid_names):
24            sum_of_costs += arg.get("cost", 0)
25
26    for key, potential_bush in kwargs.items():
27        if isinstance(potential_bush, dict) and is_bush(potential_bush, valid_names):
28            sum_of_costs += potential_bush.get("cost", 0)
29            unique_symbols.update(set(key))
30
31    if is_not_too_expensive(sum_of_costs) and looks_nice(sum_of_costs, unique_symbols):
32        return "{:.2f}лв".format(sum_of_costs)
33    else:
34        return "Ni!"

..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

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