1def is_valid_name(name):
2 return name in ('храст' ,'shrub' ,'bush')
3
4def is_valid_dict(item):
5 return 'name' in item and is_valid_name(item['name'].lower())
6
7def calculate_price(*args, **kwargs):
8 price = 0
9 for item in args:
10 if type(item) is dict and 'name' in item and is_valid_name(item['name'].lower()):
11 price += item.get('cost',0)
12 for name, item in kwargs.items():
13 if 'name' in item and is_valid_name(item['name'].lower()):
14 price += item.get('cost',0)
15 return price
16
17def function_that_says_ni(*args, **kwargs):
18 sum_of_names = ''
19 for name, item in kwargs.items():
20 if is_valid_dict(item):
21 sum_of_names += name.lower()
22 number_of_unique_symbols = len(set(sum_of_names))
23 price = calculate_price(*args , **kwargs)
24 if price == 0:
25 return 'Ni!'
26 if price <= 42 and number_of_unique_symbols % int(price) == 0:
27 return str("{:.2f}".format(price)) + 'лв'
28 else:
29 return 'Ni!'
..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 26, in function_that_says_ni
if price <= 42 and number_of_unique_symbols % int(price) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (errors=1)
Георги Кунчев
21.10.2024 15:32Коментарите ми за кавичките на стринговете афектират тестовете ти, така че силно препоръчвам да ги оправиш.
Останалото са коментари, които просто можеш да вземеш предвид за следващия път.
|
Димитар Григоров
21.10.2024 15:15Да го оправя или да оставя това решение?
|
f | 1 | def is_valid_name(name): | f | 1 | def is_valid_name(name): |
n | 2 | return name == 'храст' or name == 'shrub' or name == 'bush' | n | 2 | return name in ('храст' ,'shrub' ,'bush') |
3 | |||||
4 | def is_valid_dict(item): | ||||
5 | return 'name' in item and is_valid_name(item['name'].lower()) | ||||
3 | 6 | ||||
4 | def calculate_price(*args, **kwargs): | 7 | def calculate_price(*args, **kwargs): | ||
5 | price = 0 | 8 | price = 0 | ||
6 | for item in args: | 9 | for item in args: | ||
n | 7 | if type(item) == type(kwargs) and 'name' in item and is_valid_name(item['name'].lower()): | n | 10 | if type(item) is dict and 'name' in item and is_valid_name(item['name'].lower()): |
8 | price += item.get('cost',0) | 11 | price += item.get('cost',0) | ||
n | 9 | for name,item in kwargs.items(): | n | 12 | for name, item in kwargs.items(): |
10 | if 'name' in item and is_valid_name(item['name'].lower()): | 13 | if 'name' in item and is_valid_name(item['name'].lower()): | ||
11 | price += item.get('cost',0) | 14 | price += item.get('cost',0) | ||
12 | return price | 15 | return price | ||
13 | 16 | ||||
14 | def function_that_says_ni(*args, **kwargs): | 17 | def function_that_says_ni(*args, **kwargs): | ||
15 | sum_of_names = '' | 18 | sum_of_names = '' | ||
n | 16 | for name,item in kwargs.items(): | n | 19 | for name, item in kwargs.items(): |
20 | if is_valid_dict(item): | ||||
17 | sum_of_names += name.lower() | 21 | sum_of_names += name.lower() | ||
18 | number_of_unique_symbols = len(set(sum_of_names)) | 22 | number_of_unique_symbols = len(set(sum_of_names)) | ||
n | 19 | price = calculate_price(*args, **kwargs) | n | 23 | price = calculate_price(*args , **kwargs) |
20 | if price == 0: return '"Ni!"' | 24 | if price == 0: | ||
25 | return 'Ni!' | ||||
21 | if price <= 42 and number_of_unique_symbols % int(price) == 0: | 26 | if price <= 42 and number_of_unique_symbols % int(price) == 0: | ||
n | 22 | return str('"' + "{:.2f}".format(price)) + 'лв"' | n | 27 | return str("{:.2f}".format(price)) + 'лв' |
23 | else: | 28 | else: | ||
t | 24 | return '"Ni"!' | t | 29 | return 'Ni!' |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|