1def get_unique_letters_count(used_keywords):
2 unique_letters = set()
3 for keyword in used_keywords:
4 unique_letters.update(keyword)
5
6 return len(unique_letters)
7
8def get_only_dicts(all_args):
9 dicts = []
10 for arg in all_args:
11 if isinstance(arg, dict):
12 dicts.append(arg)
13
14 return dicts
15
16def get_only_valid_brushes(all_args):
17 valid_bushes = []
18
19 dicts = get_only_dicts(all_args)
20 valid_names = ("храст", "shrub", "bush")
21
22 for d in dicts:
23 if "name" in d and isinstance(d["name"], str) and d["name"].lower() in valid_names:
24 valid_bushes.append(d)
25
26 return valid_bushes
27
28def get_total_cost(bushes):
29 result = 0
30
31 for bush in bushes:
32 if "cost" in bush and isinstance(bush["cost"], (float, int)):
33 result += bush["cost"]
34
35 return result
36
37def function_that_says_ni(*args, **kwargs):
38 used_keywords = list(kwargs.keys())
39 count_of_unique_letters = get_unique_letters_count(used_keywords)
40
41 all_args = list(args)
42 all_args.extend(kwargs.values())
43
44 bushes = get_only_valid_brushes(all_args)
45
46 if not bushes:
47 return "Ni!"
48
49 total_cost = get_total_cost(bushes)
50
51 if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
52 return f"{total_cost:.2f}лв"
53
54 return "Ni!"
..E.EF...E
======================================================================
ERROR: test_cost_whole_part_zero (test.TestNi.test_cost_whole_part_zero)
Test with a total cost part equal to zero.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 43, in test_cost_whole_part_zero
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero
======================================================================
ERROR: 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 69, in test_invalid_strings
self.assertEqual(function_that_says_ni({'name': 'shrub', ' cost': 1}), self.NI) # Space before cost
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero
======================================================================
ERROR: test_with_no_cost (test.TestNi.test_with_no_cost)
Test with a shrub without defined cost.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 30, in test_with_no_cost
self.assertEqual(function_that_says_ni({'name': 'shrub'}), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero
======================================================================
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.002s
FAILED (failures=1, errors=3)
Николай Радилов
17.10.2024 15:21Мерси
|
Виктор Бечев
17.10.2024 15:06Можеш да качваш колкото решиш версии на домашните - независимо дали е заради корекции спрямо наш фийдбек или ти си се сетил за нещо, което можеш да направиш по-добре (или си си намерил проблем в решението и т.н.).
|
Николай Радилов
17.10.2024 14:49След като съм си оправил грешките от коментарите, мога ли да го кача наново?
|
f | 1 | def get_unique_letters_count(used_keywords): | f | 1 | def get_unique_letters_count(used_keywords): |
2 | unique_letters = set() | 2 | unique_letters = set() | ||
3 | for keyword in used_keywords: | 3 | for keyword in used_keywords: | ||
n | 4 | for letter in keyword: | n | 4 | unique_letters.update(keyword) |
5 | unique_letters.add(letter) | ||||
6 | 5 | ||||
7 | return len(unique_letters) | 6 | return len(unique_letters) | ||
8 | 7 | ||||
9 | def get_only_dicts(all_args): | 8 | def get_only_dicts(all_args): | ||
10 | dicts = [] | 9 | dicts = [] | ||
11 | for arg in all_args: | 10 | for arg in all_args: | ||
n | 12 | if type(arg) is dict: | n | 11 | if isinstance(arg, dict): |
13 | dicts.append(arg) | 12 | dicts.append(arg) | ||
14 | 13 | ||||
15 | return dicts | 14 | return dicts | ||
16 | 15 | ||||
17 | def get_only_valid_brushes(all_args): | 16 | def get_only_valid_brushes(all_args): | ||
18 | valid_bushes = [] | 17 | valid_bushes = [] | ||
19 | 18 | ||||
20 | dicts = get_only_dicts(all_args) | 19 | dicts = get_only_dicts(all_args) | ||
21 | valid_names = ("храст", "shrub", "bush") | 20 | valid_names = ("храст", "shrub", "bush") | ||
22 | 21 | ||||
n | 23 | for dict in dicts: | n | 22 | for d in dicts: |
24 | if "name" in dict and type(dict["name"]) is str and dict["name"].lower() in valid_names: | 23 | if "name" in d and isinstance(d["name"], str) and d["name"].lower() in valid_names: | ||
25 | valid_bushes.append(dict) | 24 | valid_bushes.append(d) | ||
26 | 25 | ||||
27 | return valid_bushes | 26 | return valid_bushes | ||
28 | 27 | ||||
29 | def get_total_cost(bushes): | 28 | def get_total_cost(bushes): | ||
30 | result = 0 | 29 | result = 0 | ||
31 | 30 | ||||
32 | for bush in bushes: | 31 | for bush in bushes: | ||
t | 33 | if "cost" in bush and (type(bush["cost"]) is int or type(bush["cost"]) is float): | t | 32 | if "cost" in bush and isinstance(bush["cost"], (float, int)): |
34 | result += bush["cost"] | 33 | result += bush["cost"] | ||
35 | 34 | ||||
36 | return result | 35 | return result | ||
37 | 36 | ||||
38 | def function_that_says_ni(*args, **kwargs): | 37 | def function_that_says_ni(*args, **kwargs): | ||
39 | used_keywords = list(kwargs.keys()) | 38 | used_keywords = list(kwargs.keys()) | ||
40 | count_of_unique_letters = get_unique_letters_count(used_keywords) | 39 | count_of_unique_letters = get_unique_letters_count(used_keywords) | ||
41 | 40 | ||||
42 | all_args = list(args) | 41 | all_args = list(args) | ||
43 | all_args.extend(kwargs.values()) | 42 | all_args.extend(kwargs.values()) | ||
44 | 43 | ||||
45 | bushes = get_only_valid_brushes(all_args) | 44 | bushes = get_only_valid_brushes(all_args) | ||
46 | 45 | ||||
47 | if not bushes: | 46 | if not bushes: | ||
48 | return "Ni!" | 47 | return "Ni!" | ||
49 | 48 | ||||
50 | total_cost = get_total_cost(bushes) | 49 | total_cost = get_total_cost(bushes) | ||
51 | 50 | ||||
52 | if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0: | 51 | if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0: | ||
53 | return f"{total_cost:.2f}лв" | 52 | return f"{total_cost:.2f}лв" | ||
54 | 53 | ||||
55 | return "Ni!" | 54 | return "Ni!" |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|