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

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код (Благодаря за коментарите)

 1def get_count_unique_letters_for_keys(**kwargs):
 2
 3    unique_letters = set()
 4    for key, value in kwargs.items():
 5        unique_letters.update(key)
 6
 7    return len(unique_letters)
 8
 9valid_bush_names = ['храст', 'bush', 'shrub']
10
11def function_that_says_ni(*args, **kwargs):
12    cost = 0
13    has_named = False
14
15    # execute for kwargs(named)
16    count = get_count_unique_letters_for_keys(**kwargs)
17
18    for name, dictionary in kwargs.items():
19        if 'name' in dictionary and dictionary.get('name').lower() in valid_bush_names: #Good bush
20            cost += dictionary.get('cost', 0)
21            has_named = True
22
23    # execute for args(positional)
24    for arg in args:
25        if type(arg) is not dict:
26            continue
27
28        dictionary = dict(arg)
29
30        if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names:
31            cost += dictionary.get('cost', 0)
32
33    if cost == 0 or cost > 42:
34        return 'Ni!'
35    if has_named and count % (cost // 1) != 0:
36        return 'Ni!'
37
38    return f'{cost:.2f}лв'

..F.......
======================================================================
FAIL: 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},
AssertionError: '0.30лв' != 'Ni!'
- 0.30лв
+ Ni!

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

FAILED (failures=1)

Дискусия
История

t1def get_count_unique_letters_for_keys(**kwargs):t1def get_count_unique_letters_for_keys(**kwargs):
22
3    unique_letters = set()3    unique_letters = set()
4    for key, value in kwargs.items():4    for key, value in kwargs.items():
5        unique_letters.update(key)5        unique_letters.update(key)
66
7    return len(unique_letters)7    return len(unique_letters)
88
9valid_bush_names = ['храст', 'bush', 'shrub']9valid_bush_names = ['храст', 'bush', 'shrub']
1010
11def function_that_says_ni(*args, **kwargs):11def function_that_says_ni(*args, **kwargs):
12    cost = 012    cost = 0
13    has_named = False13    has_named = False
1414
15    # execute for kwargs(named)15    # execute for kwargs(named)
16    count = get_count_unique_letters_for_keys(**kwargs)16    count = get_count_unique_letters_for_keys(**kwargs)
1717
18    for name, dictionary in kwargs.items():18    for name, dictionary in kwargs.items():
19        if 'name' in dictionary and dictionary.get('name').lower() in valid_bush_names: #Good bush19        if 'name' in dictionary and dictionary.get('name').lower() in valid_bush_names: #Good bush
20            cost += dictionary.get('cost', 0)20            cost += dictionary.get('cost', 0)
21            has_named = True21            has_named = True
2222
23    # execute for args(positional)23    # execute for args(positional)
24    for arg in args:24    for arg in args:
25        if type(arg) is not dict:25        if type(arg) is not dict:
26            continue26            continue
2727
28        dictionary = dict(arg)28        dictionary = dict(arg)
2929
30        if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names:30        if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names:
31            cost += dictionary.get('cost', 0)31            cost += dictionary.get('cost', 0)
3232
33    if cost == 0 or cost > 42:33    if cost == 0 or cost > 42:
34        return 'Ni!'34        return 'Ni!'
35    if has_named and count % (cost // 1) != 0:35    if has_named and count % (cost // 1) != 0:
36        return 'Ni!'36        return 'Ni!'
3737
38    return f'{cost:.2f}лв'38    return f'{cost:.2f}лв'
3939
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def get_count_unique_letters_for_keys(**kwargs):f1def get_count_unique_letters_for_keys(**kwargs):
22
3    unique_letters = set()3    unique_letters = set()
4    for key, value in kwargs.items():4    for key, value in kwargs.items():
5        unique_letters.update(key)5        unique_letters.update(key)
66
7    return len(unique_letters)7    return len(unique_letters)
88
9valid_bush_names = ['храст', 'bush', 'shrub']9valid_bush_names = ['храст', 'bush', 'shrub']
1010
11def function_that_says_ni(*args, **kwargs):11def function_that_says_ni(*args, **kwargs):
12    cost = 012    cost = 0
nn13    has_named = False
1314
n14    if len(args) == 0:n15    # execute for kwargs(named)
15        #execute for named arguments
16        count = get_count_unique_letters_for_keys(**kwargs)16    count = get_count_unique_letters_for_keys(**kwargs)
1717
n18        for name, dictionary in kwargs.items():n18    for name, dictionary in kwargs.items():
19            if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names: #Good bush19        if 'name' in dictionary and dictionary.get('name').lower() in valid_bush_names: #Good bush
20                if 'cost' in dictionary:
21                    cost += dictionary['cost']20            cost += dictionary.get('cost', 0)
21            has_named = True
2222
n23        if count % (cost // 1) != 0:n23    # execute for args(positional)
24            return 'Ni'24    for arg in args:
25        if type(arg) is not dict:
26            continue
2527
n26    else:n28        dictionary = dict(arg)
27        #execute for one dictionary
2829
n29        for arg in args:n30        if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names:
30            dictionary = dict(arg)31            cost += dictionary.get('cost', 0)
3132
n32            if 'name' in dictionary and dictionary['name'].lower() in valid_bush_names:n33    if cost == 0 or cost > 42:
33                if 'cost' in dictionary:
34                    if dictionary['cost'] // 1 > 42:
35                        return 'Ni'
36                    else:
37                        cost += dictionary['cost']
38            else:
39                return 'Ni'34        return 'Ni!'
35    if has_named and count % (cost // 1) != 0:
36        return 'Ni!'
4037
41    return f'{cost:.2f}лв'38    return f'{cost:.2f}лв'
4239
t43 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op