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

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

9 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    correct_bush_names = ["храст", "shrub", "bush"]
 3    total_sum = 0
 4
 5    for arg in list(args) + list(kwargs.values()):
 6        if type(arg) is not dict:
 7            continue
 8        
 9        if "name" not in arg:
10            continue
11        
12        if arg["name"].lower() not in correct_bush_names:
13            continue
14
15        total_sum += arg["cost"] if "cost" in arg else 0
16
17    unique_word_count = len(set([
18        c for word in [
19            key for key, value in kwargs.items() if "name" in value and value["name"].lower() in correct_bush_names
20        ] for c in word])
21    )
22
23    if total_sum == 0 or total_sum > 42 or unique_word_count % int(total_sum) != 0:
24        return "Ni!"
25    
26    return f"{total_sum:.2f}лв"

..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 23, in function_that_says_ni
if total_sum == 0 or total_sum > 42 or unique_word_count % int(total_sum) != 0:
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

----------------------------------------------------------------------
Ran 10 tests in 0.001s

FAILED (errors=1)

Дискусия
Асен Красимиров
17.10.2024 15:06

Извинявам се. От условиете разбрах, че се иска функцията да отпечата вярната стойност, не да я върне. Иначе да, разбирам, че по този начин функцията връща None.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    correct_bush_names = ["храст", "shrub", "bush"]2    correct_bush_names = ["храст", "shrub", "bush"]
3    total_sum = 03    total_sum = 0
44
5    for arg in list(args) + list(kwargs.values()):5    for arg in list(args) + list(kwargs.values()):
6        if type(arg) is not dict:6        if type(arg) is not dict:
7            continue7            continue
8        8        
9        if "name" not in arg:9        if "name" not in arg:
10            continue10            continue
11        11        
12        if arg["name"].lower() not in correct_bush_names:12        if arg["name"].lower() not in correct_bush_names:
13            continue13            continue
1414
15        total_sum += arg["cost"] if "cost" in arg else 015        total_sum += arg["cost"] if "cost" in arg else 0
1616
n17    unique_word_count = len(n17    unique_word_count = len(set([
18        set(
19            [
20                c for word in [18        c for word in [
21                    key for key, value in kwargs.items() if "name" in value and value["name"].lower() in correct_bush_names19            key for key, value in kwargs.items() if "name" in value and value["name"].lower() in correct_bush_names
22                ]
23                for c in word20        ] for c in word])
24            ]
25        )
26    )21    )
2722
28    if total_sum == 0 or total_sum > 42 or unique_word_count % int(total_sum) != 0:23    if total_sum == 0 or total_sum > 42 or unique_word_count % int(total_sum) != 0:
n29        print("Ni!")n
30        return24        return "Ni!"
31    25    
t32    print(f"{total_sum:.2f}лв")t26    return f"{total_sum:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op