1def is_valid_bush_name(element):
2 if type(element) == dict and 'name' in element:
3 name = element['name'].lower()
4 if name in ('храст', 'bush', 'shrub'):
5 return True
6 return False
7
8def filter_kwargs(kwargs):
9 res = {}
10 for name_of_element, element in kwargs.items():
11 if is_valid_bush_name(element):
12 res[name_of_element] = element
13 return res
14
15def ch_count(kwargs):
16 res = set()
17 for name in kwargs:
18 for ch in name:
19 res.add(ch)
20 return len(res)
21
22def find_price(args):
23 price = 0
24 for element in args:
25 price += element.get('cost', 0)
26 return price
27
28def is_it_good(price, ch_count):
29 if int(price) == 0 or int(price) > 42 or ch_count % int(price) != 0:
30 return "Ni!"
31 return f'{price:.2f}лв'
32
33def function_that_says_ni(*args, **kwargs):
34 args_filtered = list(filter(is_valid_bush_name, args))
35 kwargs_filtered = filter_kwargs(kwargs)
36
37 distinct_ch_count = ch_count(kwargs_filtered)
38
39 args_filtered.extend(kwargs_filtered.values())
40
41 price = find_price(args_filtered)
42 return is_it_good(price, distinct_ch_count)
.....F....
======================================================================
FAIL: test_multiple_shrubs_sumс (test.TestNi.test_multiple_shrubs_sumс)
Test with a multiple shrubs and cornercase costs.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 38, in test_multiple_shrubs_sumс
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.01},
AssertionError: '42.01лв' != 'Ni!'
- 42.01лв
+ Ni!
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (failures=1)
f | 1 | def is_valid_bush_name(element): | f | 1 | def is_valid_bush_name(element): |
2 | if type(element) == dict and 'name' in element: | 2 | if type(element) == dict and 'name' in element: | ||
3 | name = element['name'].lower() | 3 | name = element['name'].lower() | ||
n | 4 | if name == 'храст' or name == 'bush' or name == 'shrub': | n | 4 | if name in ('храст', 'bush', 'shrub'): |
5 | return True | 5 | return True | ||
6 | return False | 6 | return False | ||
7 | 7 | ||||
8 | def filter_kwargs(kwargs): | 8 | def filter_kwargs(kwargs): | ||
9 | res = {} | 9 | res = {} | ||
10 | for name_of_element, element in kwargs.items(): | 10 | for name_of_element, element in kwargs.items(): | ||
11 | if is_valid_bush_name(element): | 11 | if is_valid_bush_name(element): | ||
n | 12 | res[name_of_element] = element | n | 12 | res[name_of_element] = element |
13 | return res | 13 | return res | ||
14 | 14 | ||||
15 | def ch_count(kwargs): | 15 | def ch_count(kwargs): | ||
16 | res = set() | 16 | res = set() | ||
17 | for name in kwargs: | 17 | for name in kwargs: | ||
n | 18 | for ch in name : | n | 18 | for ch in name: |
19 | if ch >= 'a' and ch <= 'z' or ch == '_': | ||||
20 | res.add(ch) | 19 | res.add(ch) | ||
21 | return len(res) | 20 | return len(res) | ||
n | 22 | n | |||
23 | def fill(args, kwargs): | ||||
24 | for name, element in kwargs.items(): | ||||
25 | args.append(element) | ||||
26 | 21 | ||||
27 | def find_price(args): | 22 | def find_price(args): | ||
n | 28 | price = 0.00 | n | 23 | price = 0 |
29 | for element in args: | 24 | for element in args: | ||
n | 30 | if 'cost' in element: | n | ||
31 | price += element['cost'] | 25 | price += element.get('cost', 0) | ||
32 | return price | 26 | return price | ||
33 | 27 | ||||
34 | def is_it_good(price, ch_count): | 28 | def is_it_good(price, ch_count): | ||
35 | if int(price) == 0 or int(price) > 42 or ch_count % int(price) != 0: | 29 | if int(price) == 0 or int(price) > 42 or ch_count % int(price) != 0: | ||
36 | return "Ni!" | 30 | return "Ni!" | ||
n | 37 | round(price, 2) | n | 31 | return f'{price:.2f}лв' |
38 | return str(format(price, '.2f')) + 'лв' | ||||
39 | 32 | ||||
40 | def function_that_says_ni(*args, **kwargs): | 33 | def function_that_says_ni(*args, **kwargs): | ||
n | 41 | args_filtered = [] | n | ||
42 | args_filtered.extend(filter(is_valid_bush_name, args)) | 34 | args_filtered = list(filter(is_valid_bush_name, args)) | ||
43 | kwargs_filtered = filter_kwargs(kwargs) | 35 | kwargs_filtered = filter_kwargs(kwargs) | ||
44 | 36 | ||||
45 | distinct_ch_count = ch_count(kwargs_filtered) | 37 | distinct_ch_count = ch_count(kwargs_filtered) | ||
46 | 38 | ||||
t | 47 | fill(args_filtered, kwargs_filtered) | t | 39 | args_filtered.extend(kwargs_filtered.values()) |
48 | 40 | ||||
49 | price = find_price(args_filtered) | 41 | price = find_price(args_filtered) | ||
50 | return is_it_good(price, distinct_ch_count) | 42 | return is_it_good(price, distinct_ch_count) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | def is_valid_bush_name(element): | f | 1 | def is_valid_bush_name(element): |
2 | if type(element) == dict and 'name' in element: | 2 | if type(element) == dict and 'name' in element: | ||
3 | name = element['name'].lower() | 3 | name = element['name'].lower() | ||
4 | if name == 'храст' or name == 'bush' or name == 'shrub': | 4 | if name == 'храст' or name == 'bush' or name == 'shrub': | ||
5 | return True | 5 | return True | ||
6 | return False | 6 | return False | ||
7 | 7 | ||||
8 | def filter_kwargs(kwargs): | 8 | def filter_kwargs(kwargs): | ||
9 | res = {} | 9 | res = {} | ||
10 | for name_of_element, element in kwargs.items(): | 10 | for name_of_element, element in kwargs.items(): | ||
11 | if is_valid_bush_name(element): | 11 | if is_valid_bush_name(element): | ||
12 | res[name_of_element] = element | 12 | res[name_of_element] = element | ||
13 | return res | 13 | return res | ||
14 | 14 | ||||
15 | def ch_count(kwargs): | 15 | def ch_count(kwargs): | ||
16 | res = set() | 16 | res = set() | ||
17 | for name in kwargs: | 17 | for name in kwargs: | ||
18 | for ch in name : | 18 | for ch in name : | ||
19 | if ch >= 'a' and ch <= 'z' or ch == '_': | 19 | if ch >= 'a' and ch <= 'z' or ch == '_': | ||
20 | res.add(ch) | 20 | res.add(ch) | ||
21 | return len(res) | 21 | return len(res) | ||
22 | 22 | ||||
23 | def fill(args, kwargs): | 23 | def fill(args, kwargs): | ||
24 | for name, element in kwargs.items(): | 24 | for name, element in kwargs.items(): | ||
25 | args.append(element) | 25 | args.append(element) | ||
26 | 26 | ||||
27 | def find_price(args): | 27 | def find_price(args): | ||
28 | price = 0.00 | 28 | price = 0.00 | ||
29 | for element in args: | 29 | for element in args: | ||
30 | if 'cost' in element: | 30 | if 'cost' in element: | ||
31 | price += element['cost'] | 31 | price += element['cost'] | ||
32 | return price | 32 | return price | ||
33 | 33 | ||||
34 | def is_it_good(price, ch_count): | 34 | def is_it_good(price, ch_count): | ||
35 | if int(price) == 0 or int(price) > 42 or ch_count % int(price) != 0: | 35 | if int(price) == 0 or int(price) > 42 or ch_count % int(price) != 0: | ||
36 | return "Ni!" | 36 | return "Ni!" | ||
37 | round(price, 2) | 37 | round(price, 2) | ||
38 | return str(format(price, '.2f')) + 'лв' | 38 | return str(format(price, '.2f')) + 'лв' | ||
39 | 39 | ||||
40 | def function_that_says_ni(*args, **kwargs): | 40 | def function_that_says_ni(*args, **kwargs): | ||
41 | args_filtered = [] | 41 | args_filtered = [] | ||
42 | args_filtered.extend(filter(is_valid_bush_name, args)) | 42 | args_filtered.extend(filter(is_valid_bush_name, args)) | ||
43 | kwargs_filtered = filter_kwargs(kwargs) | 43 | kwargs_filtered = filter_kwargs(kwargs) | ||
44 | 44 | ||||
t | 45 | print(kwargs_filtered) | t | ||
46 | distinct_ch_count = ch_count(kwargs_filtered) | 45 | distinct_ch_count = ch_count(kwargs_filtered) | ||
47 | 46 | ||||
48 | fill(args_filtered, kwargs_filtered) | 47 | fill(args_filtered, kwargs_filtered) | ||
49 | 48 | ||||
50 | price = find_price(args_filtered) | 49 | price = find_price(args_filtered) | ||
51 | return is_it_good(price, distinct_ch_count) | 50 | return is_it_good(price, distinct_ch_count) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|