1def handle_shrub_data(arg, valid_shrubs):
2 if(type(arg) == dict):
3 if arg.get("name"):
4 if arg["name"].lower() in valid_shrubs:
5 return True
6 return False
7
8
9def function_that_says_ni(*args, **kwargs):
10 valid_shrubs = ["храст", "shrub", "bush"]
11 unique_symbols = set()
12 total_sum = 0.0
13 unique_symbols_count = 0
14 current_sum = 0.0
15
16 for arg in args:
17 if handle_shrub_data(arg, valid_shrubs) == True:
18 current_sum = arg.get("cost", 0)
19 total_sum += current_sum
20
21 for name, arg in kwargs.items():
22 if handle_shrub_data(arg, valid_shrubs) == True:
23 current_sum = arg.get("cost", 0)
24 total_sum += current_sum
25 unique_symbols.update(set(name))
26
27 if total_sum > 42.00:
28 return "Ni!"
29
30 unique_symbols_count = len(unique_symbols)
31
32 if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
33 return "Ni!"
34
35 return f"{total_sum:.2f}лв"
..E.......
======================================================================
ERROR: test_cost_whole_part_zero (test.TestNi.test_cost_whole_part_zero)
Test with a total cost part equal to zero.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 43, in test_cost_whole_part_zero
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 33, in function_that_says_ni
if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (errors=1)
Виктор Бечев
21.10.2024 18:58Отвъд забележката за излишната проверка - единственото друго, което търпи подобрение е премахването на повтарящият се код във `for`-овете. Но пък така рискуваш решението си, така че не е необходимо да го правиш, ще имаш възможност да видиш различни подходи когато решенията станат публични.
С други думи - чисто решение, браво.
|
n | n | 1 | |||
2 | def handle_shrub_data(arg, valid_shrubs): | ||||
3 | if(type(arg) == dict): | ||||
4 | if arg.get("name"): | ||||
5 | if arg["name"].lower() in valid_shrubs: | ||||
6 | return True | ||||
7 | return False | ||||
8 | |||||
9 | |||||
1 | def function_that_says_ni(*args, **kwargs): | 10 | def function_that_says_ni(*args, **kwargs): | ||
2 | valid_shrubs = ["храст", "shrub", "bush"] | 11 | valid_shrubs = ["храст", "shrub", "bush"] | ||
3 | unique_symbols = set() | 12 | unique_symbols = set() | ||
4 | total_sum = 0.0 | 13 | total_sum = 0.0 | ||
5 | unique_symbols_count = 0 | 14 | unique_symbols_count = 0 | ||
6 | current_sum = 0.0 | 15 | current_sum = 0.0 | ||
7 | 16 | ||||
8 | for arg in args: | 17 | for arg in args: | ||
n | 9 | if type(arg) == dict: | n | 18 | if handle_shrub_data(arg, valid_shrubs) == True: |
10 | if arg.get("name"): | ||||
11 | if arg["name"].lower() in valid_shrubs: | ||||
12 | current_sum = arg.get("cost", 0) | 19 | current_sum = arg.get("cost", 0) | ||
13 | if type(current_sum) == float or type(current_sum) == int: | ||||
14 | total_sum += current_sum | 20 | total_sum += current_sum | ||
15 | 21 | ||||
16 | for name, arg in kwargs.items(): | 22 | for name, arg in kwargs.items(): | ||
n | 17 | if type(arg) == dict: | n | 23 | if handle_shrub_data(arg, valid_shrubs) == True: |
18 | if arg.get("name"): | ||||
19 | if arg["name"].lower() in valid_shrubs: | ||||
20 | current_sum = arg.get("cost", 0) | 24 | current_sum = arg.get("cost", 0) | ||
21 | if type(current_sum) == float or type(current_sum) == int: | ||||
22 | total_sum += current_sum | 25 | total_sum += current_sum | ||
23 | unique_symbols.update(set(name)) | 26 | unique_symbols.update(set(name)) | ||
24 | 27 | ||||
25 | if total_sum > 42.00: | 28 | if total_sum > 42.00: | ||
26 | return "Ni!" | 29 | return "Ni!" | ||
27 | 30 | ||||
28 | unique_symbols_count = len(unique_symbols) | 31 | unique_symbols_count = len(unique_symbols) | ||
29 | 32 | ||||
30 | if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0): | 33 | if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0): | ||
31 | return "Ni!" | 34 | return "Ni!" | ||
32 | 35 | ||||
33 | return f"{total_sum:.2f}лв" | 36 | return f"{total_sum:.2f}лв" | ||
t | 34 | t | |||
35 | |||||
36 | |||||
37 | |||||
38 | |||||
39 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
2 | valid_shrubs = ["храст", "shrub", "bush"] | 2 | valid_shrubs = ["храст", "shrub", "bush"] | ||
3 | unique_symbols = set() | 3 | unique_symbols = set() | ||
4 | total_sum = 0.0 | 4 | total_sum = 0.0 | ||
5 | unique_symbols_count = 0 | 5 | unique_symbols_count = 0 | ||
6 | current_sum = 0.0 | 6 | current_sum = 0.0 | ||
7 | 7 | ||||
8 | for arg in args: | 8 | for arg in args: | ||
9 | if type(arg) == dict: | 9 | if type(arg) == dict: | ||
10 | if arg.get("name"): | 10 | if arg.get("name"): | ||
11 | if arg["name"].lower() in valid_shrubs: | 11 | if arg["name"].lower() in valid_shrubs: | ||
12 | current_sum = arg.get("cost", 0) | 12 | current_sum = arg.get("cost", 0) | ||
13 | if type(current_sum) == float or type(current_sum) == int: | 13 | if type(current_sum) == float or type(current_sum) == int: | ||
14 | total_sum += current_sum | 14 | total_sum += current_sum | ||
15 | 15 | ||||
16 | for name, arg in kwargs.items(): | 16 | for name, arg in kwargs.items(): | ||
17 | if type(arg) == dict: | 17 | if type(arg) == dict: | ||
18 | if arg.get("name"): | 18 | if arg.get("name"): | ||
19 | if arg["name"].lower() in valid_shrubs: | 19 | if arg["name"].lower() in valid_shrubs: | ||
20 | current_sum = arg.get("cost", 0) | 20 | current_sum = arg.get("cost", 0) | ||
21 | if type(current_sum) == float or type(current_sum) == int: | 21 | if type(current_sum) == float or type(current_sum) == int: | ||
22 | total_sum += current_sum | 22 | total_sum += current_sum | ||
23 | unique_symbols.update(set(name)) | 23 | unique_symbols.update(set(name)) | ||
24 | 24 | ||||
25 | if total_sum > 42.00: | 25 | if total_sum > 42.00: | ||
26 | return "Ni!" | 26 | return "Ni!" | ||
27 | 27 | ||||
28 | unique_symbols_count = len(unique_symbols) | 28 | unique_symbols_count = len(unique_symbols) | ||
29 | 29 | ||||
30 | if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0): | 30 | if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0): | ||
31 | return "Ni!" | 31 | return "Ni!" | ||
32 | 32 | ||||
33 | return f"{total_sum:.2f}лв" | 33 | return f"{total_sum:.2f}лв" | ||
t | 34 | t | 34 | ||
35 | #print(function_that_says_ni({"name": "храст", "cost": 120})) | ||||
36 | #print(function_that_says_ni({"name": "храст", "cost": 1})) | ||||
37 | #print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20})) | ||||
38 | print(function_that_says_ni(ab={"name": "енисел", "cost": 1.80}, bc={"name": "храст", "cost": 2.00})) | ||||
39 | 35 | ||||
40 | 36 | ||||
41 | 37 | ||||
42 | 38 | ||||
43 | 39 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
22.10.2024 15:53
22.10.2024 14:20