1def calculate_price(price_list):
2 total_price = 0.00
3 integer_price = 0
4
5 for price in price_list:
6 total_price += price
7 integer_price += int(price)
8
9 return (total_price, integer_price)
10
11def check_names(name_list, price):
12 if price == 0:
13 return False
14
15 letters = set()
16 for name in name_list:
17 for ch in name:
18 letters.add(ch)
19
20 return len(letters) % price == 0
21
22def function_that_says_ni(*args, **kwargs):
23 prices = []
24 names = []
25 bush_names = ('храст', 'shrub', 'bush')
26
27 for arg in args:
28 if type(arg) is not dict:
29 continue
30 if 'name' not in arg:
31 continue
32 if str(arg['name']).lower() not in bush_names:
33 continue
34 if 'cost' in arg:
35 prices.append(arg.get('cost', 0))
36
37
38 for arg_name, arg in kwargs.items():
39 if type(arg) is not dict:
40 continue
41 if 'name' not in arg:
42 continue
43 if str(arg['name']).lower() not in bush_names:
44 continue
45 if 'cost' in arg:
46 prices.append(arg.get('cost', 0))
47 names.append(arg_name)
48
49 price, integer_price = calculate_price(prices)
50 if price > 42:
51 return 'Ni!'
52
53 if check_names(names, integer_price):
54 return f'{format(price, '.2f')}лв'
55 else:
56 return 'Ni!'
......F...
======================================================================
FAIL: test_named_arguments (test.TestNi.test_named_arguments)
Test with named arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 50, in test_named_arguments
self.assertEqual(function_that_says_ni(_abcde={'name': 'shrub', 'cost': 2.82},
AssertionError: 'Ni!' != '7.33лв'
- Ni!
+ 7.33лв
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (failures=1)
Виктор Бечев
21.10.2024 19:27Отвъд дребните коментари по-горе - решението ти е чисто и структурирано.
|
f | 1 | def calculate_price(price_list): | f | 1 | def calculate_price(price_list): |
2 | total_price = 0.00 | 2 | total_price = 0.00 | ||
3 | integer_price = 0 | 3 | integer_price = 0 | ||
4 | 4 | ||||
5 | for price in price_list: | 5 | for price in price_list: | ||
6 | total_price += price | 6 | total_price += price | ||
7 | integer_price += int(price) | 7 | integer_price += int(price) | ||
8 | 8 | ||||
9 | return (total_price, integer_price) | 9 | return (total_price, integer_price) | ||
10 | 10 | ||||
11 | def check_names(name_list, price): | 11 | def check_names(name_list, price): | ||
12 | if price == 0: | 12 | if price == 0: | ||
13 | return False | 13 | return False | ||
14 | 14 | ||||
15 | letters = set() | 15 | letters = set() | ||
16 | for name in name_list: | 16 | for name in name_list: | ||
17 | for ch in name: | 17 | for ch in name: | ||
n | 18 | if (ch >= 'a' and ch <= 'z') or (ch == '_'): | n | ||
19 | letters.add(ch) | 18 | letters.add(ch) | ||
20 | 19 | ||||
21 | return len(letters) % price == 0 | 20 | return len(letters) % price == 0 | ||
22 | 21 | ||||
23 | def function_that_says_ni(*args, **kwargs): | 22 | def function_that_says_ni(*args, **kwargs): | ||
24 | prices = [] | 23 | prices = [] | ||
25 | names = [] | 24 | names = [] | ||
26 | bush_names = ('храст', 'shrub', 'bush') | 25 | bush_names = ('храст', 'shrub', 'bush') | ||
27 | 26 | ||||
28 | for arg in args: | 27 | for arg in args: | ||
29 | if type(arg) is not dict: | 28 | if type(arg) is not dict: | ||
30 | continue | 29 | continue | ||
31 | if 'name' not in arg: | 30 | if 'name' not in arg: | ||
32 | continue | 31 | continue | ||
33 | if str(arg['name']).lower() not in bush_names: | 32 | if str(arg['name']).lower() not in bush_names: | ||
34 | continue | 33 | continue | ||
35 | if 'cost' in arg: | 34 | if 'cost' in arg: | ||
n | 36 | prices.append(arg['cost']) | n | 35 | prices.append(arg.get('cost', 0)) |
37 | 36 | ||||
38 | 37 | ||||
39 | for arg_name, arg in kwargs.items(): | 38 | for arg_name, arg in kwargs.items(): | ||
40 | if type(arg) is not dict: | 39 | if type(arg) is not dict: | ||
41 | continue | 40 | continue | ||
42 | if 'name' not in arg: | 41 | if 'name' not in arg: | ||
43 | continue | 42 | continue | ||
44 | if str(arg['name']).lower() not in bush_names: | 43 | if str(arg['name']).lower() not in bush_names: | ||
45 | continue | 44 | continue | ||
46 | if 'cost' in arg: | 45 | if 'cost' in arg: | ||
n | 47 | prices.append(arg['cost']) | n | 46 | prices.append(arg.get('cost', 0)) |
48 | names.append(arg_name) | 47 | names.append(arg_name) | ||
49 | 48 | ||||
50 | price, integer_price = calculate_price(prices) | 49 | price, integer_price = calculate_price(prices) | ||
n | 51 | if price > 42.00: | n | 50 | if price > 42: |
52 | return 'Ni!' | 51 | return 'Ni!' | ||
53 | 52 | ||||
54 | if check_names(names, integer_price): | 53 | if check_names(names, integer_price): | ||
n | 55 | formatted_price = format(price, '.2f') | n | ||
56 | return f'{formatted_price}лв' | 54 | return f'{format(price, '.2f')}лв' | ||
57 | else: | 55 | else: | ||
58 | return 'Ni!' | 56 | return 'Ni!' | ||
t | 59 | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|