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

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код (Подобрено решение)
Скрий всички коментари

 1def handle_shrub_data(arg, valid_shrubs):
 2    if(type(arg) == dict):
 3        if arg.get("name"):
 4            if arg["name"].lower() in valid_shrubs:
 5                return True
 6    return False
 7                
 8
 9def function_that_says_ni(*args, **kwargs):
10    valid_shrubs = ["храст", "shrub", "bush"]
11    unique_symbols = set()
12    total_sum = 0.0
13    unique_symbols_count = 0
14    current_sum = 0.0
15
16    for arg in args:
17        if handle_shrub_data(arg, valid_shrubs) == True:
18            current_sum = arg.get("cost", 0)
19            total_sum += current_sum
20
21    for name, arg in kwargs.items():
22        if handle_shrub_data(arg, valid_shrubs) == True:
23            current_sum = arg.get("cost", 0)
24            total_sum += current_sum
25            unique_symbols.update(set(name))
26
27    if total_sum > 42.00:
28        return "Ni!"
29    
30    unique_symbols_count = len(unique_symbols)
31    
32    if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
33        return "Ni!"
34    
35    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 33, in function_that_says_ni
if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

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

FAILED (errors=1)

Дискусия
Виктор Бечев
21.10.2024 18:58

Отвъд забележката за излишната проверка - единственото друго, което търпи подобрение е премахването на повтарящият се код във `for`-овете. Но пък така рискуваш решението си, така че не е необходимо да го правиш, ще имаш възможност да видиш различни подходи когато решенията станат публични. С други думи - чисто решение, браво.
История

nn1 
2def handle_shrub_data(arg, valid_shrubs):
3    if(type(arg) == dict):
4        if arg.get("name"):
5            if arg["name"].lower() in valid_shrubs:
6                return True
7    return False
8                
9 
1def function_that_says_ni(*args, **kwargs):10def function_that_says_ni(*args, **kwargs):
2    valid_shrubs = ["храст", "shrub", "bush"]11    valid_shrubs = ["храст", "shrub", "bush"]
3    unique_symbols = set()12    unique_symbols = set()
4    total_sum = 0.013    total_sum = 0.0
5    unique_symbols_count = 014    unique_symbols_count = 0
6    current_sum = 0.015    current_sum = 0.0
716
8    for arg in args:17    for arg in args:
n9        if type(arg) == dict:n18        if handle_shrub_data(arg, valid_shrubs) == True:
10            if arg.get("name"):
11                if arg["name"].lower() in valid_shrubs:
12                    current_sum = arg.get("cost", 0)19            current_sum = arg.get("cost", 0)
13                    if type(current_sum) == float or type(current_sum) == int:
14                        total_sum += current_sum20            total_sum += current_sum
1521
16    for name, arg in kwargs.items():22    for name, arg in kwargs.items():
n17        if type(arg) == dict:n23        if handle_shrub_data(arg, valid_shrubs) == True:
18            if arg.get("name"):
19                if arg["name"].lower() in valid_shrubs:
20                    current_sum = arg.get("cost", 0)24            current_sum = arg.get("cost", 0)
21                    if type(current_sum) == float or type(current_sum) == int:
22                        total_sum += current_sum25            total_sum += current_sum
23                    unique_symbols.update(set(name))26            unique_symbols.update(set(name))
2427
25    if total_sum > 42.00:28    if total_sum > 42.00:
26        return "Ni!"29        return "Ni!"
27    30    
28    unique_symbols_count = len(unique_symbols)31    unique_symbols_count = len(unique_symbols)
29    32    
30    if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):33    if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
31        return "Ni!"34        return "Ni!"
32    35    
33    return f"{total_sum:.2f}лв"36    return f"{total_sum:.2f}лв"
t34 t
35    
36 
37   
38 
39                  
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    valid_shrubs = ["храст", "shrub", "bush"]2    valid_shrubs = ["храст", "shrub", "bush"]
3    unique_symbols = set()3    unique_symbols = set()
4    total_sum = 0.04    total_sum = 0.0
5    unique_symbols_count = 05    unique_symbols_count = 0
6    current_sum = 0.06    current_sum = 0.0
77
8    for arg in args:8    for arg in args:
9        if type(arg) == dict:9        if type(arg) == dict:
10            if arg.get("name"):10            if arg.get("name"):
11                if arg["name"].lower() in valid_shrubs:11                if arg["name"].lower() in valid_shrubs:
12                    current_sum = arg.get("cost", 0)12                    current_sum = arg.get("cost", 0)
13                    if type(current_sum) == float or type(current_sum) == int:13                    if type(current_sum) == float or type(current_sum) == int:
14                        total_sum += current_sum14                        total_sum += current_sum
1515
16    for name, arg in kwargs.items():16    for name, arg in kwargs.items():
17        if type(arg) == dict:17        if type(arg) == dict:
18            if arg.get("name"):18            if arg.get("name"):
19                if arg["name"].lower() in valid_shrubs:19                if arg["name"].lower() in valid_shrubs:
20                    current_sum = arg.get("cost", 0)20                    current_sum = arg.get("cost", 0)
21                    if type(current_sum) == float or type(current_sum) == int:21                    if type(current_sum) == float or type(current_sum) == int:
22                        total_sum += current_sum22                        total_sum += current_sum
23                    unique_symbols.update(set(name))23                    unique_symbols.update(set(name))
2424
25    if total_sum > 42.00:25    if total_sum > 42.00:
26        return "Ni!"26        return "Ni!"
27    27    
28    unique_symbols_count = len(unique_symbols)28    unique_symbols_count = len(unique_symbols)
29    29    
30    if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):30    if(total_sum == 0 or unique_symbols_count % int(total_sum) != 0):
31        return "Ni!"31        return "Ni!"
32    32    
33    return f"{total_sum:.2f}лв"33    return f"{total_sum:.2f}лв"
t34 t34 
35#print(function_that_says_ni({"name": "храст", "cost": 120}))
36#print(function_that_says_ni({"name": "храст", "cost": 1}))
37#print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
38print(function_that_says_ni(ab={"name": "енисел", "cost": 1.80}, bc={"name": "храст", "cost": 2.00})) 
39    35    
4036
41   37   
4238
43                  39                  
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op