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Отвъд дребните оптимизационни забележки по-горе - чисто решение, хубави имена, четим код. Браво.
П.П. Всъщност единственото нещо, което е място за подобрение е фактът, че имаш повторение на код.
|
| f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
| 2 | total_cost = 0.0 | 2 | 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"} | ||
| 6 | 6 | ||||
| 7 | def check_item(element): | 7 | def check_item(element): | ||
| n | 8 | if "name" not in element or type(element["name"]) is not str: | n | 8 | if type(element.get("name")) is not str or element["name"].lower() not in types_of_shrubs: |
| 9 | return False | 9 | return False | ||
| n | 10 | name_value = element["name"].lower() | n | 10 | 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 False | 11 | return False | ||
| n | 13 | 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 True | 12 | return True | ||
| 18 | 13 | ||||
| 19 | def add_cost(element): | 14 | def add_cost(element): | ||
| n | 20 | if "cost" in element: | n | ||
| 21 | return round(element["cost"], 2) | 15 | return round(element.get("cost", 0), 2) | ||
| 22 | return 0.0 | ||||
| 23 | 16 | ||||
| 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) | ||
| n | 27 | else: | n | ||
| 28 | return "Ni!" | ||||
| 29 | 20 | ||||
| 30 | for key, value in kwargs.items(): | 21 | for key, value in kwargs.items(): | ||
| n | 31 | 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) | ||
| n | 36 | else: | n | ||
| 37 | return "Ni!" | ||||
| 38 | 25 | ||||
| 39 | if total_cost > 42.00: | 26 | if total_cost > 42.00: | ||
| 40 | return "Ni!" | 27 | return "Ni!" | ||
| 41 | 28 | ||||
| 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!" | ||
| 45 | 32 | ||||
| 46 | return f"{total_cost:.2f}лв" | 33 | return f"{total_cost:.2f}лв" | ||
| t | t | 34 |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
| 2 | total_cost = 0.0 | 2 | total_cost = 0.0 | ||
| n | 3 | unique_letters = set() | n | 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"} | ||
| 6 | 6 | ||||
| n | n | 7 | 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: | ||
| n | 8 | if type(arg) is dict and "name" in arg: | n | 25 | 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 | ||||
| 20 | 29 | ||||
| 21 | for key, value in kwargs.items(): | 30 | for key, value in kwargs.items(): | ||
| t | 22 | if type(value) is dict and "name" in value: | t | 31 | 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 | continue | 37 | return "Ni!" | ||
| 37 | 38 | ||||
| 38 | if total_cost > 42.00: | 39 | if total_cost > 42.00: | ||
| 39 | return "Ni!" | 40 | return "Ni!" | ||
| 40 | 41 | ||||
| 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!" | ||
| 44 | 45 | ||||
| 45 | return f"{total_cost:.2f}лв" | 46 | return f"{total_cost:.2f}лв" |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||