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

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

11 точки общо

10 успешни теста
0 неуспешни теста
Код

 1def function_that_says_ni(*args, **kwargs):
 2    total_cost = 0
 3    shrub_names = set()
 4    valid_shrub_names = {"храст", "shrub", "bush"}
 5
 6    for arg in args:
 7        if isinstance(arg, dict):
 8            name = arg.get("name", "").lower()
 9            cost = arg.get("cost", 0)
10            if name in valid_shrub_names:
11                total_cost += float(cost)
12
13    for key, value in kwargs.items():
14        if isinstance(value, dict):
15            name = value.get("name", "").lower()
16            cost = value.get("cost", 0)
17            if name in valid_shrub_names:
18                total_cost += float(cost)
19                shrub_names.update(set(key))
20
21    if total_cost > 42.00:
22        return "Ni!"
23
24    whole_part_of_cost = int(total_cost)
25
26    if whole_part_of_cost == 0 or len(shrub_names) % whole_part_of_cost != 0:
27        return "Ni!"
28
29    return f"{total_cost:.2f}лв"

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

OK

Дискусия
Димитър Илиев
21.10.2024 10:21

Благодаря за обратната връзка! Мисля да не рискувам и да видя твоето решението.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
n2    total_cost = 0.0n2    total_cost = 0
3    shrub_names = set()3    shrub_names = set()
4    valid_shrub_names = {"храст", "shrub", "bush"}4    valid_shrub_names = {"храст", "shrub", "bush"}
55
6    for arg in args:6    for arg in args:
7        if isinstance(arg, dict):7        if isinstance(arg, dict):
8            name = arg.get("name", "").lower()8            name = arg.get("name", "").lower()
9            cost = arg.get("cost", 0)9            cost = arg.get("cost", 0)
10            if name in valid_shrub_names:10            if name in valid_shrub_names:
11                total_cost += float(cost)11                total_cost += float(cost)
1212
13    for key, value in kwargs.items():13    for key, value in kwargs.items():
14        if isinstance(value, dict):14        if isinstance(value, dict):
15            name = value.get("name", "").lower()15            name = value.get("name", "").lower()
16            cost = value.get("cost", 0)16            cost = value.get("cost", 0)
17            if name in valid_shrub_names:17            if name in valid_shrub_names:
18                total_cost += float(cost)18                total_cost += float(cost)
19                shrub_names.update(set(key))19                shrub_names.update(set(key))
2020
21    if total_cost > 42.00:21    if total_cost > 42.00:
22        return "Ni!"22        return "Ni!"
2323
n24    unique_letters_count = len(shrub_names)n
25    whole_part_of_cost = int(total_cost)24    whole_part_of_cost = int(total_cost)
2625
t27    if whole_part_of_cost == 0 or unique_letters_count % whole_part_of_cost != 0:t26    if whole_part_of_cost == 0 or len(shrub_names) % whole_part_of_cost != 0:
28        return "Ni!"27        return "Ni!"
2928
30    return f"{total_cost:.2f}лв"29    return f"{total_cost:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op