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

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

10 точки общо

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

 1def function_that_says_ni(*args,**kwargs):
 2
 3    bushes = []
 4    bushes_names = []
 5    for arg in args:
 6        if type(arg) is dict:
 7            if "name" not in arg:
 8                continue
 9            if type(arg["name"]) is not str:
10                continue
11            bush_name = arg["name"].lower()
12            if bush_name not in ["shrub", "храст", "bush"]:
13                continue
14            bushes.append(arg)
15
16    for key,value in kwargs.items():
17        if type(value) is dict:
18            if "name" not in value:
19                continue
20            if type(value["name"]) is not str:
21                continue
22            bush_name = value["name"].lower()
23            if bush_name not in ["shrub", "храст", "bush"]:
24                continue
25            bushes.append(value)
26            bushes_names.append(key)
27
28    letters = [letter for name in bushes_names for letter in name]
29    letters = set(letters)
30    letters_count = len(letters)
31
32    bushes_cost = 0
33    for bush in bushes:
34        if "cost" in bush:
35            if (type(bush["cost"]) is int) or (type(bush["cost"]) is float):
36                bushes_cost += bush["cost"]
37
38    bushes_cost = round(bushes_cost, 2)
39
40    if int(bushes_cost) == 0:
41        return "Ni!"
42    if letters_count % int(bushes_cost) != 0:
43        return "Ni!"
44    if bushes_cost > 42:
45        return "Ni!"
46
47    return f"{bushes_cost:.2f}лв"

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

OK

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

f1def function_that_says_ni(*args,**kwargs):f1def function_that_says_ni(*args,**kwargs):
22
3    bushes = []3    bushes = []
n4    bushesNames = []n4    bushes_names = []
5    for arg in args:5    for arg in args:
6        if type(arg) is dict:6        if type(arg) is dict:
7            if "name" not in arg:7            if "name" not in arg:
8                continue8                continue
9            if type(arg["name"]) is not str:9            if type(arg["name"]) is not str:
10                continue10                continue
n11            bushName = arg["name"].lower()n11            bush_name = arg["name"].lower()
12            if bushName not in ["shrub","храст","bush"]:12            if bush_name not in ["shrub", "храст", "bush"]:
13                continue13                continue
14            bushes.append(arg)14            bushes.append(arg)
1515
16    for key,value in kwargs.items():16    for key,value in kwargs.items():
17        if type(value) is dict:17        if type(value) is dict:
18            if "name" not in value:18            if "name" not in value:
19                continue19                continue
20            if type(value["name"]) is not str:20            if type(value["name"]) is not str:
21                continue21                continue
n22            bushName = value["name"].lower()n22            bush_name = value["name"].lower()
23            if bushName not in ["shrub","храст","bush"]:23            if bush_name not in ["shrub", "храст", "bush"]:
24                continue24                continue
25            bushes.append(value)25            bushes.append(value)
n26            bushesNames.append(key)n26            bushes_names.append(key)
2727
n28    letters = [letter for name in bushesNames for letter in name]n28    letters = [letter for name in bushes_names for letter in name]
29    letters = set(letters)29    letters = set(letters)
30    letters_count = len(letters)30    letters_count = len(letters)
3131
32    bushes_cost = 032    bushes_cost = 0
33    for bush in bushes:33    for bush in bushes:
34        if "cost" in bush:34        if "cost" in bush:
35            if (type(bush["cost"]) is int) or (type(bush["cost"]) is float):35            if (type(bush["cost"]) is int) or (type(bush["cost"]) is float):
t36                bushes_cost+=bush["cost"]t36                bushes_cost += bush["cost"]
3737
38    bushes_cost = round(bushes_cost, 2)38    bushes_cost = round(bushes_cost, 2)
3939
40    if int(bushes_cost) == 0:40    if int(bushes_cost) == 0:
41        return "Ni!"41        return "Ni!"
42    if letters_count % int(bushes_cost) != 0:42    if letters_count % int(bushes_cost) != 0:
43        return "Ni!"43        return "Ni!"
44    if bushes_cost > 42:44    if bushes_cost > 42:
45        return "Ni!"45        return "Ni!"
4646
47    return f"{bushes_cost:.2f}лв"47    return f"{bushes_cost:.2f}лв"
4848
4949
5050
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op