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

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

10 точки общо

10 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

 1def is_shrub(random_dict):
 2    if type(random_dict) is not dict:
 3        return False
 4    for key in random_dict.keys():
 5        if key == 'name':
 6            if type(random_dict[key]) is not str:
 7                return False
 8            elif random_dict[key].lower() in {'храст', 'shrub', 'bush'}:
 9                return True
10    return False
11
12def get_shrub_cost(random_shrub):
13    if type(random_shrub) is not dict:
14        return
15    elif 'cost' not in random_shrub.keys():
16        return 0
17    elif type(random_shrub['cost']) is not int \
18        and type(random_shrub['cost']) is not float:
19        return 0
20    else:
21        return random_shrub['cost']
22
23def function_that_says_ni(*args, **kwargs):
24    positional_shrubs = filter(is_shrub, args)
25    keyword_shrubs = filter(is_shrub,kwargs.values())
26
27    shrubs = []
28    for positional in positional_shrubs:
29        shrubs.append(positional)
30
31    for keyword in keyword_shrubs:    
32        shrubs.append(keyword)
33
34    total_cost = 0
35    for shrub in shrubs:
36        total_cost += get_shrub_cost(shrub)
37
38    if total_cost > 42:
39        return 'Ni!'
40
41    kwarg_keys_string = ''
42    for kwarg_key in kwargs.keys():
43        kwarg_keys_string += kwarg_key
44    
45    unique_symbols_in_kwargs = len(set(kwarg_keys_string))
46    shrubbery_is_pretty = int(total_cost) is not 0 and unique_symbols_in_kwargs % int(total_cost) == 0
47
48    if shrubbery_is_pretty:
49        return '{:.2f}'.format(total_cost) + 'лв'
50    else:
51        return 'Ni!'

/tmp/solution.py:46: SyntaxWarning: "is not" with 'int' literal. Did you mean "!="?
shrubbery_is_pretty = int(total_cost) is not 0 and unique_symbols_in_kwargs % int(total_cost) == 0
..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

Дискусия
История
Това решение има само една версия.