1def function_that_says_ni(*args, **kwargs):
2 valid_names = {"храст", "shrub", "bush"}
3 total_cost = 0
4 unique_letters = set()
5
6 for arg in args:
7 if isinstance(arg, dict) and "name" in arg:
8 name = arg["name"].lower()
9 if name in valid_names:
10 cost = arg.get("cost", 0)
11 total_cost += cost
12 for key, data in kwargs.items():
13 if isinstance(data, dict) and "name" in data:
14 name = data["name"].lower()
15 if name in valid_names:
16 cost = data.get("cost", 0)
17 total_cost += cost
18 unique_letters.update(ch for ch in key)
19
20 if total_cost > 42:
21 return "Ni!"
22 whole_part = int(total_cost)
23 if whole_part == 0:
24 return "Ni!"
25 if len(unique_letters) % whole_part != 0:
26 return "Ni!"
27 return f"{total_cost:.2f}лв"
..........
----------------------------------------------------------------------
Ran 10 tests in 0.001s
OK
Георги Кунчев
20.10.2024 11:47https://peps.python.org/pep-0008/#blank-lines
Препоръчително е да намалиш празните редове в тялото на функцията си. Наблягам на думата "препоръчително". Не е задължително.
|
f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
n | 2 | valid_names = ["храст", "shrub", "bush"] | n | 2 | valid_names = {"храст", "shrub", "bush"} |
3 | total_cost = 0 | 3 | total_cost = 0 | ||
4 | unique_letters = set() | 4 | unique_letters = set() | ||
5 | 5 | ||||
6 | for arg in args: | 6 | for arg in args: | ||
7 | if isinstance(arg, dict) and "name" in arg: | 7 | if isinstance(arg, dict) and "name" in arg: | ||
8 | name = arg["name"].lower() | 8 | name = arg["name"].lower() | ||
n | 9 | n | |||
10 | if name in valid_names: | 9 | if name in valid_names: | ||
n | 11 | cost = arg["cost"] if "cost" in arg else 0 | n | 10 | cost = arg.get("cost", 0) |
12 | total_cost += cost | 11 | total_cost += cost | ||
n | 13 | n | |||
14 | for key, data in kwargs.items(): | 12 | for key, data in kwargs.items(): | ||
15 | if isinstance(data, dict) and "name" in data: | 13 | if isinstance(data, dict) and "name" in data: | ||
16 | name = data["name"].lower() | 14 | name = data["name"].lower() | ||
n | 17 | n | |||
18 | if name in valid_names: | 15 | if name in valid_names: | ||
n | 19 | cost = data["cost"] if "cost" in data else 0 | n | 16 | cost = data.get("cost", 0) |
20 | total_cost += cost | 17 | total_cost += cost | ||
n | 21 | n | 18 | unique_letters.update(ch for ch in key) | |
22 | unique_letters.update(ch for ch in key if ch == "_" or (ch.islower() and ch.isalpha())) | ||||
23 | 19 | ||||
24 | if total_cost > 42: | 20 | if total_cost > 42: | ||
25 | return "Ni!" | 21 | return "Ni!" | ||
n | 26 | n | |||
27 | whole_part = int(total_cost) | 22 | whole_part = int(total_cost) | ||
n | 28 | n | |||
29 | if whole_part == 0: | 23 | if whole_part == 0: | ||
30 | return "Ni!" | 24 | return "Ni!" | ||
n | 31 | n | |||
32 | if len(unique_letters) % whole_part != 0: | 25 | if len(unique_letters) % whole_part != 0: | ||
33 | return "Ni!" | 26 | return "Ni!" | ||
t | 34 | t | |||
35 | return f"{total_cost:.2f}лв" | 27 | return f"{total_cost:.2f}лв" |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|