Домашни > Функцията, която казва "Ni!" > Решения > Решението на Димитър Танков

Резултати
9 точки от тестове
0 точки от учител

9 точки общо

9 успешни теста
1 неуспешни теста
Код

 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). Аз съм една идея по-толерантен, но все пак на места и на мен ми идват излишни.
История

n1 n
2def is_shrub(element) -> bool:1def is_shrub(element) -> bool:
3    valid_shrub_names = frozenset(("храст", "shrub", "bush"))2    valid_shrub_names = frozenset(("храст", "shrub", "bush"))
43
5    if type(element) is not dict:4    if type(element) is not dict:
6        return False5        return False
76
8    if element.get("name").lower() not in valid_shrub_names:7    if element.get("name").lower() not in valid_shrub_names:
9        return False8        return False
109
11    return True10    return True
1211
1312
14def is_too_expensive(overall_cost) -> bool:13def is_too_expensive(overall_cost) -> bool:
n15    upper_limit = 42n
1614
n17    return overall_cost > upper_limitn15    return overall_cost > 42
1816
1917
20def is_looking_nice(keywords_unique_char_count, overall_cost) -> bool:18def 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)
2220
23    if overall_cost_integer_part == 0:21    if overall_cost_integer_part == 0:
24        return False22        return False
2523
26    return keywords_unique_char_count % overall_cost_integer_part == 024    return keywords_unique_char_count % overall_cost_integer_part == 0
2725
2826
29def function_that_says_ni(*args, **kwargs):27def function_that_says_ni(*args, **kwargs):
30    overall_cost = 028    overall_cost = 0
31    keywords_unique_char_set = set()29    keywords_unique_char_set = set()
3230
33    for argument in args:31    for argument in args:
34        if not is_shrub(argument):32        if not is_shrub(argument):
35            continue33            continue
3634
n37        if argument.get("cost") is not None:n
38            overall_cost += argument.get("cost")35        overall_cost += argument.get("cost", 0)
3936
40    for keyword in kwargs:37    for keyword in kwargs:
41        value = kwargs.get(keyword)38        value = kwargs.get(keyword)
4239
43        if not is_shrub(value):40        if not is_shrub(value):
44            continue41            continue
4542
46        for char in keyword:43        for char in keyword:
47            keywords_unique_char_set.add(char)44            keywords_unique_char_set.add(char)
4845
n49        if value.get("cost") is not None:n
50            overall_cost += value.get("cost")46        overall_cost += value.get("cost", 0)
5147
52    if (is_looking_nice(len(keywords_unique_char_set), overall_cost) and48    if (is_looking_nice(len(keywords_unique_char_set), overall_cost) and
t53       not is_too_expensive(overall_cost)):t49        not is_too_expensive(overall_cost)):
5450
55        return f"{overall_cost:.2f}лв"51        return f"{overall_cost:.2f}лв"
56    else:52    else:
57        return "Ni!"53        return "Ni!"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op