1def function_that_says_ni(*args, **kwargs):
2 costs = 0
3 unique_letters = set()
4
5 for el in args:
6 if type(el) == dict:
7 for key, value in el.items():
8 if key == 'name':
9 if value.lower() not in ('храст', 'shrub', 'bush'):
10 break
11 if key == 'cost':
12 costs += round(value, 2)
13
14 for key, value in kwargs.items():
15 if type(value) == dict:
16 for inner_key, inner_value in value.items():
17 if inner_key == 'name':
18 if inner_value.lower() not in ('храст', 'shrub', 'bush'):
19 break
20 if inner_key == 'cost':
21 costs += round(inner_value, 2)
22 for letter in key:
23 if letter not in unique_letters:
24 unique_letters.add(letter)
25
26 letters_count = len(unique_letters)
27
28 if costs <= 42.00 and not int(costs) == 0:
29 if not letters_count % int(costs):
30 return '{:.2f}'.format(costs) + 'лв'
31
32 return 'Ni!'
....F.....
======================================================================
FAIL: test_invalid_strings (test.TestNi.test_invalid_strings)
Test with invalid strings that might be misinterpreted.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 67, in test_invalid_strings
self.assertEqual(function_that_says_ni({'no_name': 'bush', 'cost': 1}), self.NI)
AssertionError: '1.00лв' != 'Ni!'
- 1.00лв
+ Ni!
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (failures=1)
n | 1 | def function_that_says_ni(*args, **kwargs): | n | 1 | def function_that_says_ni(*args, **kwargs): |
2 | sum = 0.00 | 2 | costs = 0 | ||
3 | unique_letters = set() | 3 | unique_letters = set() | ||
4 | 4 | ||||
5 | for el in args: | 5 | for el in args: | ||
6 | if type(el) == dict: | 6 | if type(el) == dict: | ||
7 | for key, value in el.items(): | 7 | for key, value in el.items(): | ||
8 | if key == 'name': | 8 | if key == 'name': | ||
n | 9 | if value.lower() == 'храст' or value.lower() == 'shrub' or value.lower() =='bush': | n | 9 | if value.lower() not in ('храст', 'shrub', 'bush'): |
10 | continue | ||||
11 | else: | ||||
12 | break | 10 | break | ||
13 | if key == 'cost': | 11 | if key == 'cost': | ||
n | 14 | sum += round(value,2) | n | 12 | costs += round(value, 2) |
15 | 13 | ||||
16 | for key, value in kwargs.items(): | 14 | for key, value in kwargs.items(): | ||
17 | if type(value) == dict: | 15 | if type(value) == dict: | ||
18 | for inner_key, inner_value in value.items(): | 16 | for inner_key, inner_value in value.items(): | ||
19 | if inner_key == 'name': | 17 | if inner_key == 'name': | ||
n | 20 | if inner_value.lower() == 'храст' or inner_value.lower() == 'shrub' or inner_value.lower() =='bush': | n | 18 | if inner_value.lower() not in ('храст', 'shrub', 'bush'): |
21 | continue | ||||
22 | else: | ||||
23 | break | 19 | break | ||
24 | if inner_key == 'cost': | 20 | if inner_key == 'cost': | ||
n | 25 | sum += round(inner_value,2) | n | 21 | costs += round(inner_value, 2) |
26 | for letter in key: | 22 | for letter in key: | ||
27 | if letter not in unique_letters: | 23 | if letter not in unique_letters: | ||
28 | unique_letters.add(letter) | 24 | unique_letters.add(letter) | ||
29 | 25 | ||||
n | 30 | int_sum = int(sum) | n | ||
31 | letters_count = len(unique_letters) | 26 | letters_count = len(unique_letters) | ||
32 | 27 | ||||
n | 33 | if sum <= 42.00 and not int_sum == 0: | n | 28 | if costs <= 42.00 and not int(costs) == 0: |
34 | if not letters_count % int_sum: | 29 | if not letters_count % int(costs): | ||
35 | return str(round(sum,2)) + 'лв' | 30 | return '{:.2f}'.format(costs) + 'лв' | ||
36 | 31 | ||||
37 | return 'Ni!' | 32 | return 'Ni!' | ||
t | 38 | t | |||
39 | |||||
40 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|