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

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

10 точки общо

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

 1def is_valid_value(val): 
 2    valid_bushes_values = ['храст', 'shrub', 'bush']
 3    if type(val) is dict: 
 4        if val.get('name'):
 5            if val['name'].lower() in valid_bushes_values:
 6                return True
 7    return False
 8
 9
10def function_that_says_ni(*args, **kwargs):
11    cost = 0
12    unique_letters_in_name = set()
13    
14    for arg in args:
15        if is_valid_value(arg):
16            current_cost = arg.get('cost', 0)
17            cost += current_cost
18                    
19    for key, val in kwargs.items():
20        if is_valid_value(val):
21            current_cost = val.get('cost', 0)
22            cost += current_cost
23            for letter in key:
24                unique_letters_in_name.add(letter)
25
26    if cost > 42: 
27        return 'Ni!'
28    
29    whole_part_of_cost = int(cost)
30
31    unique_letters_in_name_count = len(unique_letters_in_name)
32
33    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
34        return 'Ni!'
35
36    return f'{cost:.2f}лв'

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

OK

Дискусия
Виктор Бечев
22.10.2024 15:54

Валидно за да премести малко повторения, разбира се има и по-универсално решение, но това можеш да видиш след няколко часа.
Илиана Миладинова
21.10.2024 23:07

Всъщност, това валидно решение ли е на проблема с двата идентични for-цикъла? Просто изнесох if-овете в отделна функция :)
Илиана Миладинова
21.10.2024 22:26

Направих го без флагова променлива. Благодаря за обратната връзка!
Виктор Бечев
21.10.2024 19:10

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

f1def is_valid_value(val): f1def is_valid_value(val): 
2    valid_bushes_values = ['храст', 'shrub', 'bush']2    valid_bushes_values = ['храст', 'shrub', 'bush']
3    if type(val) is dict: 3    if type(val) is dict: 
n4            if val.get('name'):n4        if val.get('name'):
5                if val['name'].lower() in valid_bushes_values:5            if val['name'].lower() in valid_bushes_values:
6                    return True6                return True
7    return False7    return False
88
99
10def function_that_says_ni(*args, **kwargs):10def function_that_says_ni(*args, **kwargs):
11    cost = 011    cost = 0
12    unique_letters_in_name = set()12    unique_letters_in_name = set()
13    13    
14    for arg in args:14    for arg in args:
t15         if is_valid_value(arg):t15        if is_valid_value(arg):
16            current_cost = arg.get('cost', 0)16            current_cost = arg.get('cost', 0)
17            cost += current_cost17            cost += current_cost
18                    18                    
19    for key, val in kwargs.items():19    for key, val in kwargs.items():
20        if is_valid_value(val):20        if is_valid_value(val):
21            current_cost = val.get('cost', 0)21            current_cost = val.get('cost', 0)
22            cost += current_cost22            cost += current_cost
23            for letter in key:23            for letter in key:
24                unique_letters_in_name.add(letter)24                unique_letters_in_name.add(letter)
2525
26    if cost > 42: 26    if cost > 42: 
27        return 'Ni!'27        return 'Ni!'
28    28    
29    whole_part_of_cost = int(cost)29    whole_part_of_cost = int(cost)
3030
31    unique_letters_in_name_count = len(unique_letters_in_name)31    unique_letters_in_name_count = len(unique_letters_in_name)
3232
33    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:33    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
34        return 'Ni!'34        return 'Ni!'
3535
36    return f'{cost:.2f}лв'36    return f'{cost:.2f}лв'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

nn1def is_valid_value(val): 
2    valid_bushes_values = ['храст', 'shrub', 'bush']
3    if type(val) is dict: 
4            if val.get('name'):
5                if val['name'].lower() in valid_bushes_values:
6                    return True
7    return False
8 
9 
1def function_that_says_ni(*args, **kwargs):10def function_that_says_ni(*args, **kwargs):
2    cost = 011    cost = 0
3    unique_letters_in_name = set()12    unique_letters_in_name = set()
n4    valid_bushes_values = ['храст', 'shrub', 'bush']n
5    13    
6    for arg in args:14    for arg in args:
n7        if type(arg) is dict: n15         if is_valid_value(arg):
8            if arg.get('name'):
9                if arg['name'].lower() in valid_bushes_values:
10                    current_cost = arg.get('cost', 0) 16            current_cost = arg.get('cost', 0)
11                    cost += current_cost17            cost += current_cost
12                    18                    
13    for key, val in kwargs.items():19    for key, val in kwargs.items():
t14        if type(val) is dict:t20        if is_valid_value(val):
15            if val.get('name'):
16                if val['name'].lower() in valid_bushes_values:
17                     current_cost = val.get('cost', 0)21            current_cost = val.get('cost', 0)
18                     cost += current_cost22            cost += current_cost
19                     for letter in key:23            for letter in key:
20                        unique_letters_in_name.add(letter)24                unique_letters_in_name.add(letter)
2125
22    if cost > 42: 26    if cost > 42: 
23        return 'Ni!'27        return 'Ni!'
24    28    
25    whole_part_of_cost = int(cost)29    whole_part_of_cost = int(cost)
2630
27    unique_letters_in_name_count = len(unique_letters_in_name)31    unique_letters_in_name_count = len(unique_letters_in_name)
2832
29    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:33    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
30        return 'Ni!'34        return 'Ni!'
3135
32    return f'{cost:.2f}лв'36    return f'{cost:.2f}лв'
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    is_valid = Truen
3    cost = 02    cost = 0
4    unique_letters_in_name = set()3    unique_letters_in_name = set()
5    valid_bushes_values = ['храст', 'shrub', 'bush']4    valid_bushes_values = ['храст', 'shrub', 'bush']
6    5    
7    for arg in args:6    for arg in args:
8        if type(arg) is dict: 7        if type(arg) is dict: 
9            if arg.get('name'):8            if arg.get('name'):
10                if arg['name'].lower() in valid_bushes_values:9                if arg['name'].lower() in valid_bushes_values:
11                    current_cost = arg.get('cost', 0) 10                    current_cost = arg.get('cost', 0) 
n12                    if type(current_cost) is int or type(current_cost) is float:n
13                        cost += current_cost11                    cost += current_cost
14                    12                    
15    for key, val in kwargs.items():13    for key, val in kwargs.items():
16        if type(val) is dict:14        if type(val) is dict:
17            if val.get('name'):15            if val.get('name'):
18                if val['name'].lower() in valid_bushes_values:16                if val['name'].lower() in valid_bushes_values:
n19                     current_cost = val.get('cost',0)n17                     current_cost = val.get('cost', 0)
20                     if type(current_cost) is int or type(current_cost) is float:
21                        cost += current_cost18                     cost += current_cost
22                        for letter in key:19                     for letter in key:
23                            unique_letters_in_name.add(letter)20                        unique_letters_in_name.add(letter)
2421
25    if cost > 42: 22    if cost > 42: 
n26         is_valid = Falsen23        return 'Ni!'
27    24    
28    whole_part_of_cost = int(cost)25    whole_part_of_cost = int(cost)
2926
30    unique_letters_in_name_count = len(unique_letters_in_name)27    unique_letters_in_name_count = len(unique_letters_in_name)
3128
32    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:29    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
n33        is_valid = Falsen30        return 'Ni!'
3431
t35    if is_valid == True:t
36        return f'{cost:.2f}лв'32    return f'{cost:.2f}лв'
37    else:
38        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    is_valid = True2    is_valid = True
3    cost = 03    cost = 0
4    unique_letters_in_name = set()4    unique_letters_in_name = set()
5    valid_bushes_values = ['храст', 'shrub', 'bush']5    valid_bushes_values = ['храст', 'shrub', 'bush']
6    6    
7    for arg in args:7    for arg in args:
8        if type(arg) is dict: 8        if type(arg) is dict: 
9            if arg.get('name'):9            if arg.get('name'):
10                if arg['name'].lower() in valid_bushes_values:10                if arg['name'].lower() in valid_bushes_values:
11                    current_cost = arg.get('cost', 0) 11                    current_cost = arg.get('cost', 0) 
12                    if type(current_cost) is int or type(current_cost) is float:12                    if type(current_cost) is int or type(current_cost) is float:
13                        cost += current_cost13                        cost += current_cost
14                    14                    
15    for key, val in kwargs.items():15    for key, val in kwargs.items():
16        if type(val) is dict:16        if type(val) is dict:
17            if val.get('name'):17            if val.get('name'):
18                if val['name'].lower() in valid_bushes_values:18                if val['name'].lower() in valid_bushes_values:
19                     current_cost = val.get('cost',0)19                     current_cost = val.get('cost',0)
20                     if type(current_cost) is int or type(current_cost) is float:20                     if type(current_cost) is int or type(current_cost) is float:
21                        cost += current_cost21                        cost += current_cost
22                        for letter in key:22                        for letter in key:
23                            unique_letters_in_name.add(letter)23                            unique_letters_in_name.add(letter)
2424
t25 t
26    if cost > 42: 25    if cost > 42: 
27         is_valid = False26         is_valid = False
28    27    
29    whole_part_of_cost = int(cost)28    whole_part_of_cost = int(cost)
3029
31    unique_letters_in_name_count = len(unique_letters_in_name)30    unique_letters_in_name_count = len(unique_letters_in_name)
3231
33    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:32    if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
34        is_valid = False33        is_valid = False
3534
36    if is_valid == True:35    if is_valid == True:
37        return f'{cost:.2f}лв'36        return f'{cost:.2f}лв'
38    else:37    else:
39        return 'Ni!'38        return 'Ni!'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op