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

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

10 точки общо

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

 1valid_shrubs = ["храст",  "shrub", "bush"]
 2
 3def is_valid_shrub(shrub):
 4     return isinstance(shrub, dict) and "name" in shrub and shrub["name"].lower() in valid_shrubs
 5
 6def calculate_total_cost(shrubs):
 7     total_cost = 0
 8     for shrub in shrubs:
 9          if is_valid_shrub(shrub):
10               current_cost = shrub.get("cost", 0)
11               total_cost += current_cost
12     return total_cost
13
14def calculate_unique_letters(shrubs):
15     unique_letters = set()
16     for key, shrub in shrubs.items():
17          if is_valid_shrub(shrub):
18               for letter in key:
19                    unique_letters.add(letter)
20     return len(unique_letters)
21         
22def function_that_says_ni(*args, **kwargs):
23    shrubs = [*args, *kwargs.values()]
24    total_cost = calculate_total_cost(shrubs)
25    if kwargs:
26         unique_letters = calculate_unique_letters(kwargs)
27         if int(total_cost) != 0 and unique_letters % int(total_cost) != 0:
28             return "Ni!"
29
30    if total_cost > 42:
31         return "Ni!"
32    
33    if int(total_cost) == 0:
34         return "Ni!"    
35    
36    return f"{total_cost:.2f}лв"

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

OK

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