Домашни > Функцията, която казва "Ni!" > Решения > Решението на Юлиана Иванова

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

8 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    def valid_shrub(input_dict):
 3        name = input_dict.get('name','').lower()
 4        if name in ('храст', 'shrub', 'bush'):
 5            return name in ('храст', 'shrub', 'bush')
 6        
 7    total_cost = 0
 8
 9    def shrub_cost(input_dict):
10        return input_dict.get('cost', 0)
11
12    for shrub in args:
13        if valid_shrub(shrub):
14            total_cost += shrub_cost(shrub)
15
16    unique_letters = set()
17
18    for name, shrub in kwargs.items():
19        if valid_shrub(shrub):
20            total_cost += shrub_cost(shrub)
21            unique_letters.update(set(name))
22
23    if total_cost >= 42.00:
24        return 'Ni!'
25    
26    if int(total_cost) == 0:
27        return "Ni!"
28    
29    if (len(unique_letters) % int(total_cost)) == 0:
30        return '{:.2f}лв'.format(total_cost)      
31    else:
32        return 'Ni!'

.....F.E..
======================================================================
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 13, in function_that_says_ni
if valid_shrub(shrub):
^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 3, in valid_shrub
name = input_dict.get('name','').lower()
^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'get'

======================================================================
FAIL: test_multiple_shrubs_sumс (test.TestNi.test_multiple_shrubs_sumс)
Test with a multiple shrubs and cornercase costs.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 34, in test_multiple_shrubs_sumс
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.00},
AssertionError: 'Ni!' != '42.00лв'
- Ni!
+ 42.00лв

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

FAILED (failures=1, errors=1)

Дискусия
Георги Кунчев
22.10.2024 11:22

За ";" в края на реда ще взимаме точки. Съветвам те да ги махнеш.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    def valid_shrub(input_dict):2    def valid_shrub(input_dict):
3        name = input_dict.get('name','').lower()3        name = input_dict.get('name','').lower()
4        if name in ('храст', 'shrub', 'bush'):4        if name in ('храст', 'shrub', 'bush'):
5            return name in ('храст', 'shrub', 'bush')5            return name in ('храст', 'shrub', 'bush')
6        6        
7    total_cost = 07    total_cost = 0
88
9    def shrub_cost(input_dict):9    def shrub_cost(input_dict):
10        return input_dict.get('cost', 0)10        return input_dict.get('cost', 0)
1111
12    for shrub in args:12    for shrub in args:
13        if valid_shrub(shrub):13        if valid_shrub(shrub):
14            total_cost += shrub_cost(shrub)14            total_cost += shrub_cost(shrub)
1515
16    unique_letters = set()16    unique_letters = set()
1717
18    for name, shrub in kwargs.items():18    for name, shrub in kwargs.items():
19        if valid_shrub(shrub):19        if valid_shrub(shrub):
20            total_cost += shrub_cost(shrub)20            total_cost += shrub_cost(shrub)
n21        unique_letters.update(set(name))n21            unique_letters.update(set(name))
2222
23    if total_cost >= 42.00:23    if total_cost >= 42.00:
24        return 'Ni!'24        return 'Ni!'
25    25    
nn26    if int(total_cost) == 0:
27        return "Ni!"
28    
26    if (len(unique_letters) % int(total_cost)) == 0:29    if (len(unique_letters) % int(total_cost)) == 0:
t27        return '{:.2f}лв.'.format(total_cost)      t30        return '{:.2f}лв'.format(total_cost)      
28    else:31    else:
29        return 'Ni!'32        return 'Ni!'
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    def valid_shrub(input_dict):2    def valid_shrub(input_dict):
3        name = input_dict.get('name','').lower()3        name = input_dict.get('name','').lower()
4        if name in ('храст', 'shrub', 'bush'):4        if name in ('храст', 'shrub', 'bush'):
5            return name in ('храст', 'shrub', 'bush')5            return name in ('храст', 'shrub', 'bush')
6        6        
n7 n
8    total_cost = 07    total_cost = 0
98
10    def shrub_cost(input_dict):9    def shrub_cost(input_dict):
11        return input_dict.get('cost', 0)10        return input_dict.get('cost', 0)
1211
13    for shrub in args:12    for shrub in args:
14        if valid_shrub(shrub):13        if valid_shrub(shrub):
15            total_cost += shrub_cost(shrub)14            total_cost += shrub_cost(shrub)
1615
17    unique_letters = set()16    unique_letters = set()
1817
19    for name, shrub in kwargs.items():18    for name, shrub in kwargs.items():
20        if valid_shrub(shrub):19        if valid_shrub(shrub):
21            total_cost += shrub_cost(shrub)20            total_cost += shrub_cost(shrub)
22        unique_letters.update(set(name))21        unique_letters.update(set(name))
2322
24    if total_cost >= 42.00:23    if total_cost >= 42.00:
25        return 'Ni!'24        return 'Ni!'
26    25    
27    if (len(unique_letters) % int(total_cost)) == 0:26    if (len(unique_letters) % int(total_cost)) == 0:
28        return '{:.2f}лв.'.format(total_cost)      27        return '{:.2f}лв.'.format(total_cost)      
29    else:28    else:
30        return 'Ni!'29        return 'Ni!'
t31    t
32print(function_that_says_ni({"name": "храст", "cost": 120}))
33print(function_that_says_ni({"name": "храст", "cost": 1}))
34print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
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):
n2    def valid_shrub(dict):n2    def valid_shrub(input_dict):
3        name = dict.get('name','').lower()3        name = input_dict.get('name','').lower()
4        if name == 'храст' or name == 'shrub' or name == 'bush':4        if name in ('храст', 'shrub', 'bush'):
5            return True5            return name in ('храст', 'shrub', 'bush')
6        return False;6        
77
n8    total_cost = 0.00;n8    total_cost = 0
99
n10    def shrub_cost(dict):n10    def shrub_cost(input_dict):
11        cost = dict.get('cost', 0.00)11        return input_dict.get('cost', 0)
12        return cost;
1312
14    for shrub in args:13    for shrub in args:
15        if valid_shrub(shrub):14        if valid_shrub(shrub):
16            total_cost += shrub_cost(shrub)15            total_cost += shrub_cost(shrub)
1716
18    unique_letters = set()17    unique_letters = set()
1918
20    for name, shrub in kwargs.items():19    for name, shrub in kwargs.items():
21        if valid_shrub(shrub):20        if valid_shrub(shrub):
22            total_cost += shrub_cost(shrub)21            total_cost += shrub_cost(shrub)
23        unique_letters.update(set(name))22        unique_letters.update(set(name))
2423
25    if total_cost >= 42.00:24    if total_cost >= 42.00:
26        return 'Ni!'25        return 'Ni!'
27    26    
n28    print('{}лв.'.format(total_cost, '.2f'))n
29 
30    if (len(unique_letters) % int(total_cost)) == 0:27    if (len(unique_letters) % int(total_cost)) == 0:
n31        return '{:.2f}лв.'.format(total_cost)       # не работи ПОМОЩ Т-Тn28        return '{:.2f}лв.'.format(total_cost)      
32    else:29    else:
33        return 'Ni!'30        return 'Ni!'
tt31    
32print(function_that_says_ni({"name": "храст", "cost": 120}))
33print(function_that_says_ni({"name": "храст", "cost": 1}))
34print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op