1def valid_bush_name(bush):
2 if not isinstance(bush, dict):
3 return False
4 current_key_name = bush.get("name")
5 valid_bush_names = ("храст", "shrub", "bush")
6 if not current_key_name:
7 return False
8 return current_key_name.lower() in valid_bush_names
9
10
11def function_that_says_ni(*args, **kwargs):
12 potential_bushes = list(filter(valid_bush_name, [*args, *kwargs.values()]))
13 # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())
14
15 max_price = 42.0
16 generated_bush_price = 0.0
17
18 for bush in potential_bushes:
19 if bush.get("cost"):
20 generated_bush_price += bush["cost"]
21 if generated_bush_price > max_price:
22 return "Ni!"
23
24 unique_letters = len({letter for (string, val) in kwargs.items() for letter in string if valid_bush_name(val)})
25
26 if int(generated_bush_price) == 0:
27 return "Ni!"
28 if unique_letters % int(generated_bush_price) == 0:
29 return f"{generated_bush_price:.2f}лв"
30 return "Ni!"
..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s
OK
Виктор Бечев
17.10.2024 12:24Чисто решение, нямам забележки.
|
f | 1 | def valid_bush_name(bush): | f | 1 | def valid_bush_name(bush): |
n | n | 2 | if not isinstance(bush, dict): | ||
3 | return False | ||||
2 | current_key_name = bush.get("name") | 4 | current_key_name = bush.get("name") | ||
3 | valid_bush_names = ("храст", "shrub", "bush") | 5 | valid_bush_names = ("храст", "shrub", "bush") | ||
4 | if not current_key_name: | 6 | if not current_key_name: | ||
5 | return False | 7 | return False | ||
6 | return current_key_name.lower() in valid_bush_names | 8 | return current_key_name.lower() in valid_bush_names | ||
7 | 9 | ||||
n | n | 10 | |||
8 | def function_that_says_ni(*args, **kwargs): | 11 | def function_that_says_ni(*args, **kwargs): | ||
n | 9 | potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()])) | n | 12 | potential_bushes = list(filter(valid_bush_name, [*args, *kwargs.values()])) |
10 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | 13 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | ||
11 | 14 | ||||
12 | max_price = 42.0 | 15 | max_price = 42.0 | ||
13 | generated_bush_price = 0.0 | 16 | generated_bush_price = 0.0 | ||
14 | 17 | ||||
15 | for bush in potential_bushes: | 18 | for bush in potential_bushes: | ||
n | 16 | if not valid_bush_name(bush): | n | ||
17 | return "Ni!" | ||||
18 | if bush.get("cost"): | 19 | if bush.get("cost"): | ||
19 | generated_bush_price += bush["cost"] | 20 | generated_bush_price += bush["cost"] | ||
20 | if generated_bush_price > max_price: | 21 | if generated_bush_price > max_price: | ||
21 | return "Ni!" | 22 | return "Ni!" | ||
22 | 23 | ||||
n | 23 | unique_letters = len({letter for string in kwargs.keys() for letter in string}) | n | 24 | unique_letters = len({letter for (string, val) in kwargs.items() for letter in string if valid_bush_name(val)}) |
24 | 25 | ||||
25 | if int(generated_bush_price) == 0: | 26 | if int(generated_bush_price) == 0: | ||
26 | return "Ni!" | 27 | return "Ni!" | ||
27 | if unique_letters % int(generated_bush_price) == 0: | 28 | if unique_letters % int(generated_bush_price) == 0: | ||
28 | return f"{generated_bush_price:.2f}лв" | 29 | return f"{generated_bush_price:.2f}лв" | ||
29 | return "Ni!" | 30 | return "Ni!" | ||
t | 30 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def valid_bush_name(bush): | f | 1 | def valid_bush_name(bush): |
2 | current_key_name = bush.get("name") | 2 | current_key_name = bush.get("name") | ||
3 | valid_bush_names = ("храст", "shrub", "bush") | 3 | valid_bush_names = ("храст", "shrub", "bush") | ||
4 | if not current_key_name: | 4 | if not current_key_name: | ||
5 | return False | 5 | return False | ||
6 | return current_key_name.lower() in valid_bush_names | 6 | return current_key_name.lower() in valid_bush_names | ||
n | 7 | n | |||
8 | |||||
9 | def get_unique_letters_count(data): | ||||
10 | letter_set = set() | ||||
11 | for string in data: | ||||
12 | for letter in string: | ||||
13 | letter_set.add(letter) | ||||
14 | return len(letter_set) | ||||
15 | |||||
16 | 7 | ||||
17 | def function_that_says_ni(*args, **kwargs): | 8 | def function_that_says_ni(*args, **kwargs): | ||
18 | potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()])) | 9 | potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()])) | ||
19 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | 10 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | ||
20 | 11 | ||||
21 | max_price = 42.0 | 12 | max_price = 42.0 | ||
22 | generated_bush_price = 0.0 | 13 | generated_bush_price = 0.0 | ||
23 | 14 | ||||
24 | for bush in potential_bushes: | 15 | for bush in potential_bushes: | ||
25 | if not valid_bush_name(bush): | 16 | if not valid_bush_name(bush): | ||
26 | return "Ni!" | 17 | return "Ni!" | ||
27 | if bush.get("cost"): | 18 | if bush.get("cost"): | ||
28 | generated_bush_price += bush["cost"] | 19 | generated_bush_price += bush["cost"] | ||
29 | if generated_bush_price > max_price: | 20 | if generated_bush_price > max_price: | ||
30 | return "Ni!" | 21 | return "Ni!" | ||
31 | 22 | ||||
t | 32 | unique_letters = get_unique_letters_count(kwargs.keys()) | t | 23 | unique_letters = len({letter for string in kwargs.keys() for letter in string}) |
33 | 24 | ||||
34 | if int(generated_bush_price) == 0: | 25 | if int(generated_bush_price) == 0: | ||
35 | return "Ni!" | 26 | return "Ni!" | ||
36 | if unique_letters % int(generated_bush_price) == 0: | 27 | if unique_letters % int(generated_bush_price) == 0: | ||
37 | return f"{generated_bush_price:.2f}лв" | 28 | return f"{generated_bush_price:.2f}лв" | ||
38 | return "Ni!" | 29 | return "Ni!" | ||
39 | 30 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def valid_bush_name(bush): | f | 1 | def valid_bush_name(bush): |
2 | current_key_name = bush.get("name") | 2 | current_key_name = bush.get("name") | ||
3 | valid_bush_names = ("храст", "shrub", "bush") | 3 | valid_bush_names = ("храст", "shrub", "bush") | ||
4 | if not current_key_name: | 4 | if not current_key_name: | ||
5 | return False | 5 | return False | ||
6 | return current_key_name.lower() in valid_bush_names | 6 | return current_key_name.lower() in valid_bush_names | ||
7 | 7 | ||||
8 | 8 | ||||
9 | def get_unique_letters_count(data): | 9 | def get_unique_letters_count(data): | ||
10 | letter_set = set() | 10 | letter_set = set() | ||
11 | for string in data: | 11 | for string in data: | ||
12 | for letter in string: | 12 | for letter in string: | ||
13 | letter_set.add(letter) | 13 | letter_set.add(letter) | ||
14 | return len(letter_set) | 14 | return len(letter_set) | ||
15 | 15 | ||||
16 | 16 | ||||
17 | def function_that_says_ni(*args, **kwargs): | 17 | def function_that_says_ni(*args, **kwargs): | ||
18 | potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()])) | 18 | potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()])) | ||
19 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | 19 | # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict()) | ||
20 | 20 | ||||
21 | max_price = 42.0 | 21 | max_price = 42.0 | ||
22 | generated_bush_price = 0.0 | 22 | generated_bush_price = 0.0 | ||
23 | 23 | ||||
24 | for bush in potential_bushes: | 24 | for bush in potential_bushes: | ||
25 | if not valid_bush_name(bush): | 25 | if not valid_bush_name(bush): | ||
n | 26 | return "Ni" | n | 26 | return "Ni!" |
27 | if bush.get("cost"): | 27 | if bush.get("cost"): | ||
28 | generated_bush_price += bush["cost"] | 28 | generated_bush_price += bush["cost"] | ||
29 | if generated_bush_price > max_price: | 29 | if generated_bush_price > max_price: | ||
n | 30 | return "Ni" | n | 30 | return "Ni!" |
31 | 31 | ||||
32 | unique_letters = get_unique_letters_count(kwargs.keys()) | 32 | unique_letters = get_unique_letters_count(kwargs.keys()) | ||
n | n | 33 | |||
34 | if int(generated_bush_price) == 0: | ||||
35 | return "Ni!" | ||||
33 | if unique_letters % int(generated_bush_price) == 0: | 36 | if unique_letters % int(generated_bush_price) == 0: | ||
n | 34 | return f"{generated_bush_price:.2f}лв" # дори да го няма в тестовете се затраховам | n | 37 | return f"{generated_bush_price:.2f}лв" |
35 | return "Ni" | 38 | return "Ni!" | ||
36 | 39 | ||||
t | 37 | t | |||
38 | print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20})) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|