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

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

10 точки общо

10 успешни теста
0 неуспешни теста
Код

 1ALLOWED_BUSH_TYPES = {'храст', 'shrub', 'bush'}
 2
 3def is_valid_bush_name(bush):
 4    bush_name_lower = bush.get('name', '').lower()
 5    if bush_name_lower in ALLOWED_BUSH_TYPES:
 6        return True
 7    return False
 8
 9def function_that_says_ni(*args, **kwargs):
10    sum_of_costs = 0
11    unique_letters = set()
12    for arg in args:
13        if type(arg) is dict and is_valid_bush_name(arg):
14            sum_of_costs += arg.get('cost', 0)           
15    for key in kwargs:
16        if type(kwargs[key]) is dict and is_valid_bush_name(kwargs[key]):
17            sum_of_costs += kwargs[key].get('cost', 0)
18            unique_letters.update(key)
19            
20    whole_part_of_costs = int(sum_of_costs)
21    if whole_part_of_costs <= 0:
22        return 'Ni!'
23
24    looks_nice = len(unique_letters) % whole_part_of_costs 
25    if sum_of_costs > 42.00 or looks_nice != 0:
26        return 'Ni!'
27    
28    return f'{sum_of_costs:.2f}лв'    

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

OK

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

n1allowed_bush_types = {'храст', 'shrub', 'bush'}n1ALLOWED_BUSH_TYPES = {'храст', 'shrub', 'bush'}
22
3def is_valid_bush_name(bush):3def is_valid_bush_name(bush):
n4    if 'name' in bush:n
5        bush_name_lower = bush['name'].lower()4    bush_name_lower = bush.get('name', '').lower()
6        for bush_type in allowed_bush_types:5    if bush_name_lower in ALLOWED_BUSH_TYPES:
7            if bush_type.lower() == bush_name_lower:
8                return True6        return True
9    return False7    return False
108
11def function_that_says_ni(*args, **kwargs):9def function_that_says_ni(*args, **kwargs):
12    sum_of_costs = 010    sum_of_costs = 0
13    unique_letters = set()11    unique_letters = set()
14    for arg in args:12    for arg in args:
15        if type(arg) is dict and is_valid_bush_name(arg):13        if type(arg) is dict and is_valid_bush_name(arg):
n16            if 'cost' in arg:n14            sum_of_costs += arg.get('cost', 0)           
17                sum_of_costs += arg['cost']            
18    for key in kwargs:15    for key in kwargs:
19        if type(kwargs[key]) is dict and is_valid_bush_name(kwargs[key]):16        if type(kwargs[key]) is dict and is_valid_bush_name(kwargs[key]):
n20            if 'cost' in kwargs[key]:n
21                sum_of_costs += kwargs[key]['cost']17            sum_of_costs += kwargs[key].get('cost', 0)
22            unique_letters.update(key)18            unique_letters.update(key)
23            19            
24    whole_part_of_costs = int(sum_of_costs)20    whole_part_of_costs = int(sum_of_costs)
25    if whole_part_of_costs <= 0:21    if whole_part_of_costs <= 0:
26        return 'Ni!'22        return 'Ni!'
2723
28    looks_nice = len(unique_letters) % whole_part_of_costs 24    looks_nice = len(unique_letters) % whole_part_of_costs 
29    if sum_of_costs > 42.00 or looks_nice != 0:25    if sum_of_costs > 42.00 or looks_nice != 0:
30        return 'Ni!'26        return 'Ni!'
31    27    
t32    return (f'{sum_of_costs:.2f}лв')     t28    return f'{sum_of_costs:.2f}лв'    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1allowed_bush_types = {'храст', 'shrub', 'bush'}f1allowed_bush_types = {'храст', 'shrub', 'bush'}
22
3def is_valid_bush_name(bush):3def is_valid_bush_name(bush):
4    if 'name' in bush:4    if 'name' in bush:
5        bush_name_lower = bush['name'].lower()5        bush_name_lower = bush['name'].lower()
6        for bush_type in allowed_bush_types:6        for bush_type in allowed_bush_types:
7            if bush_type.lower() == bush_name_lower:7            if bush_type.lower() == bush_name_lower:
8                return True8                return True
9    return False9    return False
1010
11def function_that_says_ni(*args, **kwargs):11def function_that_says_ni(*args, **kwargs):
12    sum_of_costs = 012    sum_of_costs = 0
13    unique_letters = set()13    unique_letters = set()
n14    n
15    for arg in args:14    for arg in args:
16        if type(arg) is dict and is_valid_bush_name(arg):15        if type(arg) is dict and is_valid_bush_name(arg):
17            if 'cost' in arg:16            if 'cost' in arg:
n18                sum_of_costs += arg['cost']n17                sum_of_costs += arg['cost']            
19                
20    for key in kwargs:18    for key in kwargs:
21        if type(kwargs[key]) is dict and is_valid_bush_name(kwargs[key]):19        if type(kwargs[key]) is dict and is_valid_bush_name(kwargs[key]):
22            if 'cost' in kwargs[key]:20            if 'cost' in kwargs[key]:
23                sum_of_costs += kwargs[key]['cost']21                sum_of_costs += kwargs[key]['cost']
24            unique_letters.update(key)22            unique_letters.update(key)
25            23            
26    whole_part_of_costs = int(sum_of_costs)24    whole_part_of_costs = int(sum_of_costs)
27    if whole_part_of_costs <= 0:25    if whole_part_of_costs <= 0:
28        return 'Ni!'26        return 'Ni!'
t29    t27 
30    looks_nice = len(unique_letters) % whole_part_of_costs 28    looks_nice = len(unique_letters) % whole_part_of_costs 
31    if sum_of_costs > 42.00 or looks_nice != 0:29    if sum_of_costs > 42.00 or looks_nice != 0:
32        return 'Ni!'30        return 'Ni!'
33    31    
34    return (f'{sum_of_costs:.2f}лв')     32    return (f'{sum_of_costs:.2f}лв')     
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op