1def put_all_keywords_into_str(named_bushes):
2 all_symbols = ""
3 for bush in named_bushes:
4 all_symbols += bush["key"]
5 return all_symbols
6
7def get_sum_of_elements_at_index(elements, index):
8 total_sum = 0
9 for elem in elements:
10 total_sum += elem[index]
11 return total_sum
12
13def function_that_says_ni(*args, **kwargs):
14 valid_shrub_names = {"храст", "shrub", "bush"}
15 named_bushes = []
16 no_name_bushes = []
17 for arg in args:
18 if isinstance(arg, dict) and "name" in arg and isinstance(arg["name"], str):
19 name = arg.get("name").lower()
20 if name not in valid_shrub_names:
21 continue
22 cost = arg.get("cost", 0)
23 bush = {
24 "name": name,
25 "cost": cost
26 }
27 no_name_bushes.append(bush)
28 for key, value in kwargs.items():
29 if isinstance(value, dict) and "name" in value and isinstance(value["name"], str):
30 name = value.get("name").lower()
31 if name not in valid_shrub_names:
32 continue
33 cost = value.get("cost", 0)
34 bush = {
35 "key": key,
36 "name": name,
37 "cost": cost
38 }
39 named_bushes.append(bush)
40
41 costs_sum = get_sum_of_elements_at_index(no_name_bushes, "cost") + get_sum_of_elements_at_index(named_bushes, "cost")
42 if costs_sum < 1 or costs_sum > 42.00:
43 return "Ni!"
44
45 all_symbols = put_all_keywords_into_str(named_bushes)
46 unique_symbols_count = len(set(all_symbols))
47
48 if unique_symbols_count % int(costs_sum) == 0:
49 return f"{round(costs_sum, 2):.2f}лв"
50 else:
51 return "Ni!"
..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s
OK
Георги Кунчев
20.10.2024 11:26Общо казано имаш добро и чисте решение. Има нещо генерално, което не ми харесва. Това е начина, по който пазиш информация за храсти в tuple-и, които после индексираш.
Индексите се следят трудно и човек забравя кое на коя позиция е и защо вункциите ти се извикват с магическите 1 или 2.
Има и по-добри методи за това. Една опция, която мога да препоръчам, базирано на знанията, които сме преподали, е да ползваш речници. Тогава няма да е нужно да използваш индекси, а ще реферираш полета с конкретно име. Например, вместо:
`bush = (name, cost)`
правиш това:
```
bush = {
'name': name,
'cost': cost
}
```
Това също не е опция, която бих използвал, но е по-добре от прости индекси и не изиксва голяма промяна в кода ти.
|
n | 1 | def count_unique_symbols_in_str(name): | n | ||
2 | return len(set(name)) | ||||
3 | |||||
4 | def put_all_keywords_into_str(named_bushes): | 1 | def put_all_keywords_into_str(named_bushes): | ||
5 | all_symbols = "" | 2 | all_symbols = "" | ||
6 | for bush in named_bushes: | 3 | for bush in named_bushes: | ||
n | 7 | all_symbols += bush[0] | n | 4 | all_symbols += bush["key"] |
8 | return all_symbols | 5 | return all_symbols | ||
9 | 6 | ||||
10 | def get_sum_of_elements_at_index(elements, index): | 7 | def get_sum_of_elements_at_index(elements, index): | ||
11 | total_sum = 0 | 8 | total_sum = 0 | ||
12 | for elem in elements: | 9 | for elem in elements: | ||
13 | total_sum += elem[index] | 10 | total_sum += elem[index] | ||
14 | return total_sum | 11 | return total_sum | ||
15 | 12 | ||||
16 | def function_that_says_ni(*args, **kwargs): | 13 | def function_that_says_ni(*args, **kwargs): | ||
17 | valid_shrub_names = {"храст", "shrub", "bush"} | 14 | valid_shrub_names = {"храст", "shrub", "bush"} | ||
18 | named_bushes = [] | 15 | named_bushes = [] | ||
19 | no_name_bushes = [] | 16 | no_name_bushes = [] | ||
20 | for arg in args: | 17 | for arg in args: | ||
21 | if isinstance(arg, dict) and "name" in arg and isinstance(arg["name"], str): | 18 | if isinstance(arg, dict) and "name" in arg and isinstance(arg["name"], str): | ||
22 | name = arg.get("name").lower() | 19 | name = arg.get("name").lower() | ||
23 | if name not in valid_shrub_names: | 20 | if name not in valid_shrub_names: | ||
24 | continue | 21 | continue | ||
n | 25 | cost = 0 | n | ||
26 | if "cost" in arg: | ||||
27 | cost = arg.get("cost") | 22 | cost = arg.get("cost", 0) | ||
28 | bush = (name, cost) | 23 | bush = { | ||
24 | "name": name, | ||||
25 | "cost": cost | ||||
26 | } | ||||
29 | no_name_bushes.append(bush) | 27 | no_name_bushes.append(bush) | ||
30 | for key, value in kwargs.items(): | 28 | for key, value in kwargs.items(): | ||
31 | if isinstance(value, dict) and "name" in value and isinstance(value["name"], str): | 29 | if isinstance(value, dict) and "name" in value and isinstance(value["name"], str): | ||
32 | name = value.get("name").lower() | 30 | name = value.get("name").lower() | ||
33 | if name not in valid_shrub_names: | 31 | if name not in valid_shrub_names: | ||
34 | continue | 32 | continue | ||
n | 35 | cost = 0 | n | ||
36 | if "cost" in value: | ||||
37 | cost = value.get("cost") | 33 | cost = value.get("cost", 0) | ||
38 | bush = (key, name, cost) | 34 | bush = { | ||
35 | "key": key, | ||||
36 | "name": name, | ||||
37 | "cost": cost | ||||
38 | } | ||||
39 | named_bushes.append(bush) | 39 | named_bushes.append(bush) | ||
40 | 40 | ||||
n | 41 | costs_sum = get_sum_of_elements_at_index(no_name_bushes, 1) + get_sum_of_elements_at_index(named_bushes, 2) | n | 41 | costs_sum = get_sum_of_elements_at_index(no_name_bushes, "cost") + get_sum_of_elements_at_index(named_bushes, "cost") |
42 | if costs_sum < 1 or costs_sum > 42.00: | 42 | if costs_sum < 1 or costs_sum > 42.00: | ||
43 | return "Ni!" | 43 | return "Ni!" | ||
44 | 44 | ||||
45 | all_symbols = put_all_keywords_into_str(named_bushes) | 45 | all_symbols = put_all_keywords_into_str(named_bushes) | ||
n | 46 | unique_symbols_count = count_unique_symbols_in_str(all_symbols) | n | 46 | unique_symbols_count = len(set(all_symbols)) |
47 | 47 | ||||
48 | if unique_symbols_count % int(costs_sum) == 0: | 48 | if unique_symbols_count % int(costs_sum) == 0: | ||
49 | return f"{round(costs_sum, 2):.2f}лв" | 49 | return f"{round(costs_sum, 2):.2f}лв" | ||
50 | else: | 50 | else: | ||
51 | return "Ni!" | 51 | return "Ni!" | ||
t | 52 | t | |||
53 | |||||
54 | |||||
55 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|