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

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

10 точки общо

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

 1def is_bush(potential_bush):
 2    valid_names = {"храст", "shrub", "bush"}
 3
 4    if "name" in potential_bush and type(potential_bush["name"]) == str:
 5        name_lower = potential_bush["name"].lower()
 6        return name_lower in valid_names
 7    else:
 8        return False
 9
10def function_that_says_ni(*args, **kwargs):
11
12    merged = list(args) + list(kwargs.values())
13    total_cost = 0
14
15    for element in merged :
16        if type(element) == dict and is_bush(element):
17            current_cost = element.get("cost", 0)
18            if type(current_cost) == int or type(current_cost) == float:
19                total_cost +=  current_cost
20
21    unique_letters = set()
22    for key, value in kwargs.items():
23        if type(value) == dict and is_bush(value):
24            unique_letters.update(key)
25        if '_' in key:
26            unique_letters.add('_')
27
28    num_unique_letters = len(unique_letters)
29
30
31    if (int(total_cost) == 0):
32        one_that_looks_nice = False
33    else:
34        one_that_looks_nice = num_unique_letters % int(total_cost) == 0
35
36
37    if total_cost <= 42 and one_that_looks_nice:
38        return f"{total_cost:.2f}лв"
39    else:
40        return "Ni!"
41
42#print(function_that_says_ni({"name": "храст", "cost": 120}))
43#print(function_that_says_ni({"name": "храст", "cost": 1}))
44#print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
45#print(function_that_says_ni({"name": "чегарак", "cost": 1.00}))
46#print(function_that_says_ni({"name": "храст", "cost": 2.00}))
47#print(function_that_says_ni({"name": "shrub", "cost": 3.00}))
48#print(function_that_says_ni({"name": "bush"}))

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

OK

Дискусия
История

t1def is_bush(potential_bush):t1def is_bush(potential_bush):
2    valid_names = {"храст", "shrub", "bush"}2    valid_names = {"храст", "shrub", "bush"}
33
4    if "name" in potential_bush and type(potential_bush["name"]) == str:4    if "name" in potential_bush and type(potential_bush["name"]) == str:
5        name_lower = potential_bush["name"].lower()5        name_lower = potential_bush["name"].lower()
6        return name_lower in valid_names6        return name_lower in valid_names
7    else:7    else:
8        return False8        return False
99
10def function_that_says_ni(*args, **kwargs):10def function_that_says_ni(*args, **kwargs):
1111
12    merged = list(args) + list(kwargs.values())12    merged = list(args) + list(kwargs.values())
13    total_cost = 013    total_cost = 0
1414
15    for element in merged :15    for element in merged :
16        if type(element) == dict and is_bush(element):16        if type(element) == dict and is_bush(element):
17            current_cost = element.get("cost", 0)17            current_cost = element.get("cost", 0)
18            if type(current_cost) == int or type(current_cost) == float:18            if type(current_cost) == int or type(current_cost) == float:
19                total_cost +=  current_cost19                total_cost +=  current_cost
2020
21    unique_letters = set()21    unique_letters = set()
22    for key, value in kwargs.items():22    for key, value in kwargs.items():
23        if type(value) == dict and is_bush(value):23        if type(value) == dict and is_bush(value):
24            unique_letters.update(key)24            unique_letters.update(key)
25        if '_' in key:25        if '_' in key:
26            unique_letters.add('_')26            unique_letters.add('_')
2727
28    num_unique_letters = len(unique_letters)28    num_unique_letters = len(unique_letters)
2929
3030
31    if (int(total_cost) == 0):31    if (int(total_cost) == 0):
32        one_that_looks_nice = False32        one_that_looks_nice = False
33    else:33    else:
34        one_that_looks_nice = num_unique_letters % int(total_cost) == 034        one_that_looks_nice = num_unique_letters % int(total_cost) == 0
3535
3636
37    if total_cost <= 42 and one_that_looks_nice:37    if total_cost <= 42 and one_that_looks_nice:
38        return f"{total_cost:.2f}лв"38        return f"{total_cost:.2f}лв"
39    else:39    else:
40        return "Ni!"40        return "Ni!"
4141
42#print(function_that_says_ni({"name": "храст", "cost": 120}))42#print(function_that_says_ni({"name": "храст", "cost": 120}))
43#print(function_that_says_ni({"name": "храст", "cost": 1}))43#print(function_that_says_ni({"name": "храст", "cost": 1}))
44#print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))44#print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
45#print(function_that_says_ni({"name": "чегарак", "cost": 1.00}))45#print(function_that_says_ni({"name": "чегарак", "cost": 1.00}))
46#print(function_that_says_ni({"name": "храст", "cost": 2.00}))46#print(function_that_says_ni({"name": "храст", "cost": 2.00}))
47#print(function_that_says_ni({"name": "shrub", "cost": 3.00}))47#print(function_that_says_ni({"name": "shrub", "cost": 3.00}))
48#print(function_that_says_ni({"name": "bush"}))48#print(function_that_says_ni({"name": "bush"}))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op