1from functools import reduce
2
3NI = "Ni!"
4
5def append_currrency(func):
6 def format(*args, **kwargs):
7 result = func(*args, **kwargs)
8 if isinstance(result, str) and result != NI:
9 return f"{result}лв"
10 return result
11 return format
12
13
14@append_currrency
15def function_that_says_ni(*args, **kwargs):
16 keywords = set(["храст", "shrub", "bush"])
17
18 important_args = list(filter(lambda arg: isinstance(arg, dict) and arg.get("name", "").lower() in keywords, args))
19 important_kwargs = list(filter(lambda key_value: isinstance(key_value[1],dict) and key_value[1].get("name","").lower() in keywords, kwargs.items()))
20
21 bushes = important_args + [key_value[1] for key_value in important_kwargs]
22
23 total_cost = round(sum(map(lambda bush: bush.get('cost', 0), bushes)), 2)
24
25 unique_characters = set()
26 for key_value in important_kwargs:
27 unique_characters.update(key_value[0])
28 if int(total_cost) == 0:
29 return NI
30 elif total_cost <= 42.00 and total_cost >= 0.00 and len(unique_characters) % int(total_cost) == 0:
31 return f"{total_cost:.2f}"
32 return NI
..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s
OK
Рада Димитрова
22.10.2024 13:43За списъка с позволени стойности си позволих да използвам лист точно защото бяха малко на брой стойности, но да - с по-големи данни би било неоптимално.
А иначе за ламбдите - много ми се искаше да практикувам синтаксиса тук и затова ги предпочетох пред класическото решение.
|
f | 1 | from functools import reduce | f | 1 | from functools import reduce |
n | n | 2 | |||
3 | NI = "Ni!" | ||||
2 | 4 | ||||
3 | def append_currrency(func): | 5 | def append_currrency(func): | ||
4 | def format(*args, **kwargs): | 6 | def format(*args, **kwargs): | ||
5 | result = func(*args, **kwargs) | 7 | result = func(*args, **kwargs) | ||
n | 6 | if isinstance(result, str) and result != "Ni!": | n | 8 | if isinstance(result, str) and result != NI: |
7 | return f"{result}лв" | 9 | return f"{result}лв" | ||
8 | return result | 10 | return result | ||
9 | return format | 11 | return format | ||
10 | 12 | ||||
11 | 13 | ||||
12 | @append_currrency | 14 | @append_currrency | ||
13 | def function_that_says_ni(*args, **kwargs): | 15 | def function_that_says_ni(*args, **kwargs): | ||
n | 14 | keywords = ["храст", "shrub", "bush"] | n | 16 | keywords = set(["храст", "shrub", "bush"]) |
15 | 17 | ||||
n | 16 | important_args = list(filter(lambda arg: isinstance(arg,dict) and arg.get("name","").lower() in keywords, args)) | n | 18 | important_args = list(filter(lambda arg: isinstance(arg, dict) and arg.get("name", "").lower() in keywords, args)) |
17 | important_kwargs = list(filter(lambda key_value: isinstance(key_value[1],dict) and key_value[1].get("name","").lower() in keywords, kwargs.items())) | 19 | important_kwargs = list(filter(lambda key_value: isinstance(key_value[1],dict) and key_value[1].get("name","").lower() in keywords, kwargs.items())) | ||
18 | 20 | ||||
19 | bushes = important_args + [key_value[1] for key_value in important_kwargs] | 21 | bushes = important_args + [key_value[1] for key_value in important_kwargs] | ||
20 | 22 | ||||
n | 21 | total_cost = round(reduce(lambda acc, bush: acc + bush.get("cost", 0), bushes, 0.00),2) | n | 23 | total_cost = round(sum(map(lambda bush: bush.get('cost', 0), bushes)), 2) |
22 | 24 | ||||
23 | unique_characters = set() | 25 | unique_characters = set() | ||
24 | for key_value in important_kwargs: | 26 | for key_value in important_kwargs: | ||
25 | unique_characters.update(key_value[0]) | 27 | unique_characters.update(key_value[0]) | ||
26 | if int(total_cost) == 0: | 28 | if int(total_cost) == 0: | ||
n | 27 | return "Ni!" | n | 29 | return NI |
28 | elif total_cost <= 42.00 and total_cost >= 0.00 and len(unique_characters) % int(total_cost) == 0: | 30 | elif total_cost <= 42.00 and total_cost >= 0.00 and len(unique_characters) % int(total_cost) == 0: | ||
29 | return f"{total_cost:.2f}" | 31 | return f"{total_cost:.2f}" | ||
t | 30 | return "Ni!" | t | 32 | return NI |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from functools import reduce | f | 1 | from functools import reduce |
2 | 2 | ||||
3 | def append_currrency(func): | 3 | def append_currrency(func): | ||
4 | def format(*args, **kwargs): | 4 | def format(*args, **kwargs): | ||
5 | result = func(*args, **kwargs) | 5 | result = func(*args, **kwargs) | ||
6 | if isinstance(result, str) and result != "Ni!": | 6 | if isinstance(result, str) and result != "Ni!": | ||
7 | return f"{result}лв" | 7 | return f"{result}лв" | ||
8 | return result | 8 | return result | ||
9 | return format | 9 | return format | ||
10 | 10 | ||||
11 | 11 | ||||
12 | @append_currrency | 12 | @append_currrency | ||
13 | def function_that_says_ni(*args, **kwargs): | 13 | def function_that_says_ni(*args, **kwargs): | ||
14 | keywords = ["храст", "shrub", "bush"] | 14 | keywords = ["храст", "shrub", "bush"] | ||
15 | 15 | ||||
16 | important_args = list(filter(lambda arg: isinstance(arg,dict) and arg.get("name","").lower() in keywords, args)) | 16 | important_args = list(filter(lambda arg: isinstance(arg,dict) and arg.get("name","").lower() in keywords, args)) | ||
17 | important_kwargs = list(filter(lambda key_value: isinstance(key_value[1],dict) and key_value[1].get("name","").lower() in keywords, kwargs.items())) | 17 | important_kwargs = list(filter(lambda key_value: isinstance(key_value[1],dict) and key_value[1].get("name","").lower() in keywords, kwargs.items())) | ||
18 | 18 | ||||
19 | bushes = important_args + [key_value[1] for key_value in important_kwargs] | 19 | bushes = important_args + [key_value[1] for key_value in important_kwargs] | ||
20 | 20 | ||||
21 | total_cost = round(reduce(lambda acc, bush: acc + bush.get("cost", 0), bushes, 0.00),2) | 21 | total_cost = round(reduce(lambda acc, bush: acc + bush.get("cost", 0), bushes, 0.00),2) | ||
22 | 22 | ||||
23 | unique_characters = set() | 23 | unique_characters = set() | ||
24 | for key_value in important_kwargs: | 24 | for key_value in important_kwargs: | ||
25 | unique_characters.update(key_value[0]) | 25 | unique_characters.update(key_value[0]) | ||
26 | if int(total_cost) == 0: | 26 | if int(total_cost) == 0: | ||
27 | return "Ni!" | 27 | return "Ni!" | ||
t | 28 | if total_cost <= 42.00 and total_cost >= 0.00 and len(unique_characters) % int(total_cost) == 0: | t | 28 | elif total_cost <= 42.00 and total_cost >= 0.00 and len(unique_characters) % int(total_cost) == 0: |
29 | return f"{total_cost:.2f}" | 29 | return f"{total_cost:.2f}" | ||
30 | return "Ni!" | 30 | return "Ni!" |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|