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

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

10 точки общо

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

 1def is_valid_bush(potential_bush):
 2    valid_keys = ('храст', 'shrub', 'bush')
 3
 4    if type(potential_bush) is dict:
 5        if 'name' in potential_bush:
 6            if potential_bush['name'].lower() in valid_keys:
 7                return True
 8    return False
 9
10
11def not_too_expensive(total_cost):
12    max_cost = 42.00
13    return total_cost <= max_cost
14
15
16def one_that_looks_nice(unique_letters, total_cost):
17    integer_cost = int(total_cost)
18    return integer_cost != 0 and len(unique_letters) % integer_cost == 0
19
20
21def function_that_says_ni(*args, **kwargs):
22    total_cost = 0.00
23    unique_letters = set()
24
25    for arg in args:
26        if is_valid_bush(arg):
27            total_cost += arg.get('cost', 0)
28
29    for key, value in kwargs.items():
30        if is_valid_bush(value):
31            current_letters = set(key)
32            unique_letters |= current_letters
33            total_cost += value.get('cost', 0)
34
35    if not_too_expensive(total_cost) and one_that_looks_nice(unique_letters, total_cost):
36        return f'{total_cost:.2f}лв'
37
38    else:
39        return 'Ni!'

..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

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

f1def is_valid_bush(potential_bush):f1def is_valid_bush(potential_bush):
2    valid_keys = ('храст', 'shrub', 'bush')2    valid_keys = ('храст', 'shrub', 'bush')
33
4    if type(potential_bush) is dict:4    if type(potential_bush) is dict:
5        if 'name' in potential_bush:5        if 'name' in potential_bush:
6            if potential_bush['name'].lower() in valid_keys:6            if potential_bush['name'].lower() in valid_keys:
7                return True7                return True
8    return False8    return False
99
1010
11def not_too_expensive(total_cost):11def not_too_expensive(total_cost):
12    max_cost = 42.0012    max_cost = 42.00
13    return total_cost <= max_cost13    return total_cost <= max_cost
1414
1515
16def one_that_looks_nice(unique_letters, total_cost):16def one_that_looks_nice(unique_letters, total_cost):
17    integer_cost = int(total_cost)17    integer_cost = int(total_cost)
18    return integer_cost != 0 and len(unique_letters) % integer_cost == 018    return integer_cost != 0 and len(unique_letters) % integer_cost == 0
1919
2020
21def function_that_says_ni(*args, **kwargs):21def function_that_says_ni(*args, **kwargs):
22    total_cost = 0.0022    total_cost = 0.00
23    unique_letters = set()23    unique_letters = set()
2424
25    for arg in args:25    for arg in args:
26        if is_valid_bush(arg):26        if is_valid_bush(arg):
27            total_cost += arg.get('cost', 0)27            total_cost += arg.get('cost', 0)
2828
29    for key, value in kwargs.items():29    for key, value in kwargs.items():
30        if is_valid_bush(value):30        if is_valid_bush(value):
31            current_letters = set(key)31            current_letters = set(key)
32            unique_letters |= current_letters32            unique_letters |= current_letters
33            total_cost += value.get('cost', 0)33            total_cost += value.get('cost', 0)
3434
35    if not_too_expensive(total_cost) and one_that_looks_nice(unique_letters, total_cost):35    if not_too_expensive(total_cost) and one_that_looks_nice(unique_letters, total_cost):
t36        return f'{total_cost:.2f}лв.'t36        return f'{total_cost:.2f}лв'
3737
38    else:38    else:
39        return 'Ni!'39        return 'Ni!'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op