1def is_shrub(element) -> bool:
2 valid_shrub_names = frozenset(("храст", "shrub", "bush"))
3
4 if type(element) is not dict:
5 return False
6
7 if element.get("name").lower() not in valid_shrub_names:
8 return False
9
10 return True
11
12
13def is_too_expensive(overall_cost) -> bool:
14
15 return overall_cost > 42
16
17
18def is_looking_nice(keywords_unique_char_count, overall_cost) -> bool:
19 overall_cost_integer_part = int(overall_cost)
20
21 if overall_cost_integer_part == 0:
22 return False
23
24 return keywords_unique_char_count % overall_cost_integer_part == 0
25
26
27def function_that_says_ni(*args, **kwargs):
28 overall_cost = 0
29 keywords_unique_char_set = set()
30
31 for argument in args:
32 if not is_shrub(argument):
33 continue
34
35 overall_cost += argument.get("cost", 0)
36
37 for keyword in kwargs:
38 value = kwargs.get(keyword)
39
40 if not is_shrub(value):
41 continue
42
43 for char in keyword:
44 keywords_unique_char_set.add(char)
45
46 overall_cost += value.get("cost", 0)
47
48 if (is_looking_nice(len(keywords_unique_char_set), overall_cost) and
49 not is_too_expensive(overall_cost)):
50
51 return f"{overall_cost:.2f}лв"
52 else:
53 return "Ni!"
....E.....
======================================================================
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 67, in test_invalid_strings
self.assertEqual(function_that_says_ni({'no_name': 'bush', 'cost': 1}), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 32, in function_that_says_ni
if not is_shrub(argument):
^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 7, in is_shrub
if element.get("name").lower() not in valid_shrub_names:
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'lower'
----------------------------------------------------------------------
Ran 10 tests in 0.001s
FAILED (errors=1)
Димитър Танков
18.10.2024 00:02Ок, съгласен съм. Относно празните редове, според мен изглежда по-нагледно нз.
|
Виктор Бечев
17.10.2024 14:44Жорката би бил малко скептичен към прекалената употреба на празни редове из кода ти (говорим за из функциите, не между тях, там си spot on). Аз съм една идея по-толерантен, но все пак на места и на мен ми идват излишни.
|
n | 1 | n | |||
2 | def is_shrub(element) -> bool: | 1 | def is_shrub(element) -> bool: | ||
3 | valid_shrub_names = frozenset(("храст", "shrub", "bush")) | 2 | valid_shrub_names = frozenset(("храст", "shrub", "bush")) | ||
4 | 3 | ||||
5 | if type(element) is not dict: | 4 | if type(element) is not dict: | ||
6 | return False | 5 | return False | ||
7 | 6 | ||||
8 | if element.get("name").lower() not in valid_shrub_names: | 7 | if element.get("name").lower() not in valid_shrub_names: | ||
9 | return False | 8 | return False | ||
10 | 9 | ||||
11 | return True | 10 | return True | ||
12 | 11 | ||||
13 | 12 | ||||
14 | def is_too_expensive(overall_cost) -> bool: | 13 | def is_too_expensive(overall_cost) -> bool: | ||
n | 15 | upper_limit = 42 | n | ||
16 | 14 | ||||
n | 17 | return overall_cost > upper_limit | n | 15 | return overall_cost > 42 |
18 | 16 | ||||
19 | 17 | ||||
20 | def is_looking_nice(keywords_unique_char_count, overall_cost) -> bool: | 18 | def is_looking_nice(keywords_unique_char_count, overall_cost) -> bool: | ||
21 | overall_cost_integer_part = int(overall_cost) | 19 | overall_cost_integer_part = int(overall_cost) | ||
22 | 20 | ||||
23 | if overall_cost_integer_part == 0: | 21 | if overall_cost_integer_part == 0: | ||
24 | return False | 22 | return False | ||
25 | 23 | ||||
26 | return keywords_unique_char_count % overall_cost_integer_part == 0 | 24 | return keywords_unique_char_count % overall_cost_integer_part == 0 | ||
27 | 25 | ||||
28 | 26 | ||||
29 | def function_that_says_ni(*args, **kwargs): | 27 | def function_that_says_ni(*args, **kwargs): | ||
30 | overall_cost = 0 | 28 | overall_cost = 0 | ||
31 | keywords_unique_char_set = set() | 29 | keywords_unique_char_set = set() | ||
32 | 30 | ||||
33 | for argument in args: | 31 | for argument in args: | ||
34 | if not is_shrub(argument): | 32 | if not is_shrub(argument): | ||
35 | continue | 33 | continue | ||
36 | 34 | ||||
n | 37 | if argument.get("cost") is not None: | n | ||
38 | overall_cost += argument.get("cost") | 35 | overall_cost += argument.get("cost", 0) | ||
39 | 36 | ||||
40 | for keyword in kwargs: | 37 | for keyword in kwargs: | ||
41 | value = kwargs.get(keyword) | 38 | value = kwargs.get(keyword) | ||
42 | 39 | ||||
43 | if not is_shrub(value): | 40 | if not is_shrub(value): | ||
44 | continue | 41 | continue | ||
45 | 42 | ||||
46 | for char in keyword: | 43 | for char in keyword: | ||
47 | keywords_unique_char_set.add(char) | 44 | keywords_unique_char_set.add(char) | ||
48 | 45 | ||||
n | 49 | if value.get("cost") is not None: | n | ||
50 | overall_cost += value.get("cost") | 46 | overall_cost += value.get("cost", 0) | ||
51 | 47 | ||||
52 | if (is_looking_nice(len(keywords_unique_char_set), overall_cost) and | 48 | if (is_looking_nice(len(keywords_unique_char_set), overall_cost) and | ||
t | 53 | not is_too_expensive(overall_cost)): | t | 49 | not is_too_expensive(overall_cost)): |
54 | 50 | ||||
55 | return f"{overall_cost:.2f}лв" | 51 | return f"{overall_cost:.2f}лв" | ||
56 | else: | 52 | else: | ||
57 | return "Ni!" | 53 | return "Ni!" |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|