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

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

11 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    total_cost = 0.0
 3    unique_letters = set()
 4    variable_type = (int, float)
 5    types_of_shrubs = {"храст", "shrub", "bush"}
 6
 7    def check_item(element):
 8        if type(element.get("name")) is not str or element["name"].lower() not in types_of_shrubs:
 9            return False
10        if "cost" in element and (type(element["cost"]) not in variable_type or element["cost"] < 0):
11            return False
12        return True
13
14    def add_cost(element):
15        return round(element.get("cost", 0), 2)
16
17    for arg in args:
18        if type(arg) is dict and check_item(arg):
19            total_cost += add_cost(arg)
20
21    for key, value in kwargs.items():
22        if type(value) is dict and check_item(value):
23            unique_letters.update(letter for letter in key if letter.islower() or letter == '_')
24            total_cost += add_cost(value)
25
26    if total_cost > 42.00:
27        return "Ni!"
28
29    integer_cost = int(total_cost)
30    if integer_cost == 0 or len(unique_letters) % integer_cost != 0:
31        return "Ni!"
32
33    return f"{total_cost:.2f}лв"

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

OK

Дискусия
Виктор Бечев
17.10.2024 12:10

Отвъд дребните оптимизационни забележки по-горе - чисто решение, хубави имена, четим код. Браво. П.П. Всъщност единственото нещо, което е място за подобрение е фактът, че имаш повторение на код.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    total_cost = 0.02    total_cost = 0.0
3    unique_letters = set()3    unique_letters = set()
4    variable_type = (int, float)4    variable_type = (int, float)
5    types_of_shrubs = {"храст", "shrub", "bush"}5    types_of_shrubs = {"храст", "shrub", "bush"}
66
7    def check_item(element):7    def check_item(element):
n8        if "name" not in element or type(element["name"]) is not str:n8        if type(element.get("name")) is not str or element["name"].lower() not in types_of_shrubs:
9            return False9            return False
n10        name_value = element["name"].lower()n10        if "cost" in element and (type(element["cost"]) not in variable_type or element["cost"] < 0):
11        if name_value not in types_of_shrubs:
12            return False11            return False
n13        if "cost" in element:n
14            cost = element["cost"]
15            if type(cost) not in variable_type or cost < 0:
16                return False
17        return True12        return True
1813
19    def add_cost(element):14    def add_cost(element):
n20        if "cost" in element:n
21            return round(element["cost"], 2)15        return round(element.get("cost", 0), 2)
22        return 0.0
2316
24    for arg in args:17    for arg in args:
25        if type(arg) is dict and check_item(arg):18        if type(arg) is dict and check_item(arg):
26            total_cost += add_cost(arg)19            total_cost += add_cost(arg)
n27        else:n
28            return "Ni!"
2920
30    for key, value in kwargs.items():21    for key, value in kwargs.items():
n31        if type(key) is not str:n
32            return "Ni!"
33        if type(value) is dict and check_item(value):22        if type(value) is dict and check_item(value):
34            unique_letters.update(letter for letter in key if letter.islower() or letter == '_')23            unique_letters.update(letter for letter in key if letter.islower() or letter == '_')
35            total_cost += add_cost(value)24            total_cost += add_cost(value)
n36        else:n
37            return "Ni!"
3825
39    if total_cost > 42.00:26    if total_cost > 42.00:
40        return "Ni!"27        return "Ni!"
4128
42    integer_cost = int(total_cost)29    integer_cost = int(total_cost)
43    if integer_cost == 0 or len(unique_letters) % integer_cost != 0:30    if integer_cost == 0 or len(unique_letters) % integer_cost != 0:
44        return "Ni!"31        return "Ni!"
4532
46    return f"{total_cost:.2f}лв"33    return f"{total_cost:.2f}лв"
tt34 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    total_cost = 0.02    total_cost = 0.0
n3    unique_letters  = set()n3    unique_letters = set()
4    variable_type =  (int, float)4    variable_type = (int, float)
5    types_of_shrubs = {"храст", "shrub", "bush"}5    types_of_shrubs = {"храст", "shrub", "bush"}
66
nn7    def check_item(element):
8        if "name" not in element or type(element["name"]) is not str:
9            return False
10        name_value = element["name"].lower()
11        if name_value not in types_of_shrubs:
12            return False
13        if "cost" in element:
14            cost = element["cost"]
15            if type(cost) not in variable_type or cost < 0:
16                return False
17        return True
18 
19    def add_cost(element):
20        if "cost" in element:
21            return round(element["cost"], 2)
22        return 0.0
23 
7    for arg in args:24    for arg in args:
n8        if type(arg) is dict and "name" in arg:n25        if type(arg) is dict and check_item(arg):
9            name_value = arg["name"].lower()26            total_cost += add_cost(arg)
10            if name_value not in types_of_shrubs:27        else:
11                return "Ni!"28            return "Ni!"
12            if "cost" in arg:
13                cost = arg["cost"]
14                if type(cost) in variable_type and cost >= 0:
15                    total_cost += round(cost, 2)
16                else:
17                    return "Ni!"
18            else:
19                continue
2029
21    for key, value in kwargs.items():30    for key, value in kwargs.items():
t22        if type(value) is dict and "name" in value:t31        if type(key) is not str:
23            name_value = value["name"].lower()
24            if name_value in types_of_shrubs:
25                for letter in key:
26                    unique_letters.add(letter)
27            else:
28                return "Ni!"32            return "Ni!"
29            if "cost" in value:33        if type(value) is dict and check_item(value):
30                cost = value["cost"]34            unique_letters.update(letter for letter in key if letter.islower() or letter == '_')
31                if type(cost) in variable_type and cost >= 0:35            total_cost += add_cost(value)
32                    total_cost += round(cost, 2)
33                else:
34                    return "Ni!"
35            else:36        else:
36                continue37            return "Ni!"
3738
38    if total_cost > 42.00:39    if total_cost > 42.00:
39        return "Ni!"40        return "Ni!"
4041
41    integer_cost = int(total_cost)42    integer_cost = int(total_cost)
42    if integer_cost == 0 or len(unique_letters) % integer_cost != 0:43    if integer_cost == 0 or len(unique_letters) % integer_cost != 0:
43        return "Ni!"44        return "Ni!"
4445
45    return f"{total_cost:.2f}лв"46    return f"{total_cost:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op