1def is_good_named_shrub(shrub):
2 if 'name' in map(str.lower, shrub.keys()):
3 name_value = shrub.get('name')
4 if name_value.lower() in ['bush', 'shrub', 'храст']:
5 return True
6 return False
7 return False
8
9
10def function_that_says_ni(*args, **kwargs):
11 costs = 0
12 unique_letters = set()
13 have_bush = False
14
15 for key, value in kwargs.items():
16 if type(value) == dict and is_good_named_shrub(value):
17 unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_'))
18 have_bush = True
19 if 'cost' in value:
20 costs += value.get('cost')
21
22 unique_letters_count = len(unique_letters)
23
24 for element in args:
25 if type(element) == dict and is_good_named_shrub(element):
26 have_bush = True
27 if 'cost' in element:
28 costs += element.get('cost')
29
30 is_one_that_looks_nice = 0
31 if int(costs) != 0:
32 is_one_that_looks_nice = unique_letters_count % int(costs) == 0
33
34 if have_bush and costs < 42.00 and is_one_that_looks_nice:
35 return f"{costs:.2f}лв"
36 else:
37 return 'Ni!'
.....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 34, in test_multiple_shrubs_sumс
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.00},
AssertionError: 'Ni!' != '42.00лв'
- Ni!
+ 42.00лв
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (failures=1)
f | 1 | def is_good_named_shrub(shrub): | f | 1 | def is_good_named_shrub(shrub): |
2 | if 'name' in map(str.lower, shrub.keys()): | 2 | if 'name' in map(str.lower, shrub.keys()): | ||
3 | name_value = shrub.get('name') | 3 | name_value = shrub.get('name') | ||
4 | if name_value.lower() in ['bush', 'shrub', 'храст']: | 4 | if name_value.lower() in ['bush', 'shrub', 'храст']: | ||
5 | return True | 5 | return True | ||
6 | return False | 6 | return False | ||
7 | return False | 7 | return False | ||
8 | 8 | ||||
9 | 9 | ||||
10 | def function_that_says_ni(*args, **kwargs): | 10 | def function_that_says_ni(*args, **kwargs): | ||
11 | costs = 0 | 11 | costs = 0 | ||
12 | unique_letters = set() | 12 | unique_letters = set() | ||
13 | have_bush = False | 13 | have_bush = False | ||
14 | 14 | ||||
15 | for key, value in kwargs.items(): | 15 | for key, value in kwargs.items(): | ||
16 | if type(value) == dict and is_good_named_shrub(value): | 16 | if type(value) == dict and is_good_named_shrub(value): | ||
17 | unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_')) | 17 | unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_')) | ||
18 | have_bush = True | 18 | have_bush = True | ||
19 | if 'cost' in value: | 19 | if 'cost' in value: | ||
20 | costs += value.get('cost') | 20 | costs += value.get('cost') | ||
21 | 21 | ||||
22 | unique_letters_count = len(unique_letters) | 22 | unique_letters_count = len(unique_letters) | ||
23 | 23 | ||||
24 | for element in args: | 24 | for element in args: | ||
25 | if type(element) == dict and is_good_named_shrub(element): | 25 | if type(element) == dict and is_good_named_shrub(element): | ||
26 | have_bush = True | 26 | have_bush = True | ||
27 | if 'cost' in element: | 27 | if 'cost' in element: | ||
28 | costs += element.get('cost') | 28 | costs += element.get('cost') | ||
29 | 29 | ||||
30 | is_one_that_looks_nice = 0 | 30 | is_one_that_looks_nice = 0 | ||
31 | if int(costs) != 0: | 31 | if int(costs) != 0: | ||
32 | is_one_that_looks_nice = unique_letters_count % int(costs) == 0 | 32 | is_one_that_looks_nice = unique_letters_count % int(costs) == 0 | ||
33 | 33 | ||||
34 | if have_bush and costs < 42.00 and is_one_that_looks_nice: | 34 | if have_bush and costs < 42.00 and is_one_that_looks_nice: | ||
n | 35 | return f"{costs: .2f} лв" | n | 35 | return f"{costs:.2f}лв" |
36 | else: | 36 | else: | ||
37 | return 'Ni!' | 37 | return 'Ni!' | ||
t | 38 | t | |||
39 | |||||
40 | print(function_that_says_ni({"name": "Храст", "cost": 11.0}, ad={"name": "дърво", "cost": 1.80}, c = "pypy")) | ||||
41 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
21.10.2024 13:48
21.10.2024 14:05
21.10.2024 13:54
21.10.2024 13:57