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

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

5 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    approved_names = ('храст', 'shrub', 'bush')
 3    max_cost = 42
 4    total_cost = 0
 5
 6    for bush in args + tuple(kwargs.values()):
 7        if not isinstance(bush, type(dict())) or not bush.get('name'):
 8            continue
 9
10        total_cost += bush.get('cost', 0)
11        if bush['name'].lower() not in approved_names or total_cost > max_cost:
12            return 'Ni!'
13
14    keys = ''.join(kwargs.keys())
15    if len(set(keys)) % int(total_cost) != 0:
16        return 'Ni!'
17
18    return f'{total_cost:.2f}лв'
19
20# Example usage
21# print(function_that_says_ni({"name": "храст", "cost": 120}))
22# print(function_that_says_ni({"name": "храст", "cost": 1}))
23# print(function_that_says_ni(aabcc={"name": "bUsh", "cost": 3.20}))

..EEE..E.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 15, in function_that_says_ni
if len(set(keys)) % int(total_cost) != 0:
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

======================================================================
ERROR: test_empty (test.TestNi.test_empty)
Test with empty input.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 13, in test_empty
self.assertEqual(function_that_says_ni(), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 15, in function_that_says_ni
if len(set(keys)) % 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 67, in test_invalid_strings
self.assertEqual(function_that_says_ni({'no_name': 'bush', 'cost': 1}), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 15, in function_that_says_ni
if len(set(keys)) % int(total_cost) != 0:
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

======================================================================
ERROR: test_other_than_dicts (test.TestNi.test_other_than_dicts)
Test with inputs other than dicts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 74, in test_other_than_dicts
self.assertEqual(function_that_says_ni(1, 3.14, ['some_list'], some_arg={1, 2, 3}), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 15, in function_that_says_ni
if len(set(keys)) % 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 15, in function_that_says_ni
if len(set(keys)) % int(total_cost) != 0:
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

----------------------------------------------------------------------
Ran 10 tests in 0.002s

FAILED (errors=5)

Дискусия
Виктор Бечев
17.10.2024 12:28

Хубави имена на променливите и четим код, това, което търпи подобрение е премахването на повторението на няколко реда идентичен код.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
n2    approved_names = ("храст""shrub""bush")n2    approved_names = ('храст''shrub''bush')
3    max_cost = 423    max_cost = 42
4    total_cost = 04    total_cost = 0
55
n6    for bush in args:n6    for bush in args + tuple(kwargs.values()):
7        if not isinstance(bush, type({})):7        if not isinstance(bush, type(dict())) or not bush.get('name'):
8            continue8            continue
99
n10        total_cost += bush.get("cost", 0)n10        total_cost += bush.get('cost', 0)
11        if bush.get("name").lower() not in approved_names or total_cost > max_cost:11        if bush['name'].lower() not in approved_names or total_cost > max_cost:
12            return "Ni!"12            return 'Ni!'
1313
n14    keys = ""n14    keys = ''.join(kwargs.keys())
15    for key, bush in kwargs.items():15    if len(set(keys)) % int(total_cost) != 0:
16        if not isinstance(bush, type({})):16        return 'Ni!'
17            continue
1817
t19        total_cost += bush.get("cost", 0)t
20        if bush.get("name").lower() not in approved_names or total_cost > max_cost:
21            return "Ni!"
22        keys +=key
23 
24    if len(set(keys)) % int(total_cost) != 0:
25        return "Ni!"
26 
27    return f"{total_cost:.2f}лв"18    return f'{total_cost:.2f}лв'
2819
29# Example usage20# Example usage
30# print(function_that_says_ni({"name": "храст", "cost": 120}))21# print(function_that_says_ni({"name": "храст", "cost": 120}))
31# print(function_that_says_ni({"name": "храст", "cost": 1}))22# print(function_that_says_ni({"name": "храст", "cost": 1}))
32# print(function_that_says_ni(aabcc={"name": "bUsh", "cost": 3.20}))23# print(function_that_says_ni(aabcc={"name": "bUsh", "cost": 3.20}))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op