1#checks if the shrub is valid
2def is_valid(shrub):
3 if "name" in shrub:
4 name_val = shrub["name"].lower()
5 if name_val in ["храст", "shrub", "bush"]:
6 return True
7 return False
8
9#checks if the total cost is bigger than 42
10def total_cost_check(total_cost):
11 return total_cost > 42
12
13#checks if the bush is nice
14def nice(unique_letters_count, total_cost):
15 if unique_letters_count == 0:
16 return False
17 return unique_letters_count % int(total_cost) == 0
18
19#checks if the total cost is zero
20def is_zero(total_cost):
21 return int(total_cost) == 0
22
23
24def function_that_says_ni(*args, **kwargs):
25 # empty list to store valid shrubs
26 shrubs = []
27
28 # empty set to keep track of unique letters
29 unique_letters = set()
30
31 # total cost of shrubs
32 total_cost = 0.0
33
34 # check if it is valid shrub
35
36
37 for arg in args:
38 if isinstance(arg, dict):
39 if is_valid(arg):
40 shrubs.append(arg)
41 total_cost += arg.get("cost", 0)
42
43 for key, value in kwargs.items():
44 if isinstance(value, dict):
45 if is_valid(value):
46 shrubs.append(value)
47 total_cost += value.get("cost", 0)
48 unique_letters.add(key)
49
50 if total_cost_check(total_cost):
51 return "Ni!"
52
53 unique_letters_count = len(unique_letters)
54
55 if is_zero(total_cost):
56 return "Ni!"
57
58 if nice(unique_letters_count, total_cost):
59 return "Ni!"
60
61 f_cost = f"{total_cost:.2f}лв"
62 return f_cost
.F....F...
======================================================================
FAIL: test_combination_of_arguments (test.TestNi.test_combination_of_arguments)
Test with combination of named and positional arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 60, in test_combination_of_arguments
self.assertEqual(function_that_says_ni({'name': 'bush', 'cost': 1},
AssertionError: 'Ni!' != '2.00лв'
- Ni!
+ 2.00лв
======================================================================
FAIL: test_named_arguments (test.TestNi.test_named_arguments)
Test with named arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 55, in test_named_arguments
self.assertEqual(function_that_says_ni(a={'name': 'shrub', 'cost': 1},
AssertionError: '3.00лв' != 'Ni!'
- 3.00лв
+ Ni!
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (failures=2)
f | 1 | #checks if the shrub is valid | f | 1 | #checks if the shrub is valid |
2 | def is_valid(shrub): | 2 | def is_valid(shrub): | ||
3 | if "name" in shrub: | 3 | if "name" in shrub: | ||
4 | name_val = shrub["name"].lower() | 4 | name_val = shrub["name"].lower() | ||
5 | if name_val in ["храст", "shrub", "bush"]: | 5 | if name_val in ["храст", "shrub", "bush"]: | ||
6 | return True | 6 | return True | ||
7 | return False | 7 | return False | ||
8 | 8 | ||||
9 | #checks if the total cost is bigger than 42 | 9 | #checks if the total cost is bigger than 42 | ||
10 | def total_cost_check(total_cost): | 10 | def total_cost_check(total_cost): | ||
11 | return total_cost > 42 | 11 | return total_cost > 42 | ||
12 | 12 | ||||
13 | #checks if the bush is nice | 13 | #checks if the bush is nice | ||
14 | def nice(unique_letters_count, total_cost): | 14 | def nice(unique_letters_count, total_cost): | ||
15 | if unique_letters_count == 0: | 15 | if unique_letters_count == 0: | ||
16 | return False | 16 | return False | ||
17 | return unique_letters_count % int(total_cost) == 0 | 17 | return unique_letters_count % int(total_cost) == 0 | ||
18 | 18 | ||||
19 | #checks if the total cost is zero | 19 | #checks if the total cost is zero | ||
20 | def is_zero(total_cost): | 20 | def is_zero(total_cost): | ||
21 | return int(total_cost) == 0 | 21 | return int(total_cost) == 0 | ||
22 | 22 | ||||
23 | 23 | ||||
24 | def function_that_says_ni(*args, **kwargs): | 24 | def function_that_says_ni(*args, **kwargs): | ||
25 | # empty list to store valid shrubs | 25 | # empty list to store valid shrubs | ||
26 | shrubs = [] | 26 | shrubs = [] | ||
27 | 27 | ||||
28 | # empty set to keep track of unique letters | 28 | # empty set to keep track of unique letters | ||
29 | unique_letters = set() | 29 | unique_letters = set() | ||
30 | 30 | ||||
31 | # total cost of shrubs | 31 | # total cost of shrubs | ||
32 | total_cost = 0.0 | 32 | total_cost = 0.0 | ||
33 | 33 | ||||
34 | # check if it is valid shrub | 34 | # check if it is valid shrub | ||
35 | 35 | ||||
36 | 36 | ||||
37 | for arg in args: | 37 | for arg in args: | ||
38 | if isinstance(arg, dict): | 38 | if isinstance(arg, dict): | ||
39 | if is_valid(arg): | 39 | if is_valid(arg): | ||
40 | shrubs.append(arg) | 40 | shrubs.append(arg) | ||
41 | total_cost += arg.get("cost", 0) | 41 | total_cost += arg.get("cost", 0) | ||
42 | 42 | ||||
43 | for key, value in kwargs.items(): | 43 | for key, value in kwargs.items(): | ||
44 | if isinstance(value, dict): | 44 | if isinstance(value, dict): | ||
45 | if is_valid(value): | 45 | if is_valid(value): | ||
46 | shrubs.append(value) | 46 | shrubs.append(value) | ||
47 | total_cost += value.get("cost", 0) | 47 | total_cost += value.get("cost", 0) | ||
48 | unique_letters.add(key) | 48 | unique_letters.add(key) | ||
49 | 49 | ||||
50 | if total_cost_check(total_cost): | 50 | if total_cost_check(total_cost): | ||
51 | return "Ni!" | 51 | return "Ni!" | ||
52 | 52 | ||||
53 | unique_letters_count = len(unique_letters) | 53 | unique_letters_count = len(unique_letters) | ||
54 | 54 | ||||
55 | if is_zero(total_cost): | 55 | if is_zero(total_cost): | ||
56 | return "Ni!" | 56 | return "Ni!" | ||
57 | 57 | ||||
58 | if nice(unique_letters_count, total_cost): | 58 | if nice(unique_letters_count, total_cost): | ||
59 | return "Ni!" | 59 | return "Ni!" | ||
60 | 60 | ||||
61 | f_cost = f"{total_cost:.2f}лв" | 61 | f_cost = f"{total_cost:.2f}лв" | ||
62 | return f_cost | 62 | return f_cost | ||
63 | 63 | ||||
64 | 64 | ||||
t | 65 | print(function_that_says_ni(c={"name": "ffff", "cost": 100}, a = {"name": "sHrub", "cost": 3}, b={"name": "buSH", "cost": 4.50})) | t |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
22.10.2024 18:42
22.10.2024 18:43
22.10.2024 18:43
22.10.2024 18:44
22.10.2024 18:44
22.10.2024 18:45