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

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

10 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    total_price = 0
 3    valid_bushes = False
 4    for arg in args:
 5        if isinstance(arg, dict) and is_bush(arg):
 6            valid_bushes = True
 7            total_price += cost_of_bush(arg)
 8
 9    for kwarg_value in kwargs.values():
10        if isinstance(kwarg_value, dict) and is_bush(kwarg_value):
11            valid_bushes = True
12            total_price += cost_of_bush(kwarg_value)
13
14    output = generate_output(kwargs, total_price, valid_bushes)
15    return output
16
17def generate_output(kwargs, total_price, valid_bushes):
18    output_text = 'Ni!'
19    if valid_bushes == False:
20        return output_text
21    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):
22        return output_text
23    else:
24        return f'{total_price:.2f}лв'
25
26def is_bush(arg):
27    key = 'name'
28    return key in arg.keys() and str(arg[key]).lower() in ('храст', 'shrub', 'bush')
29
30def cost_of_bush(arg):
31    value = 'cost'
32    if value in arg.keys() and isinstance(arg[value], (int, float)):
33        price = arg[value]
34        return price
35    return 0
36
37def one_that_looks_nice(kwargs, price):
38    whole_part_price = int(price)
39    if whole_part_price == 0:
40        return False
41    if len(kwargs) == 0:
42        return True
43    elif kwargs:
44        unique_chars = set()
45        for kwarg_key, kwarg_value in kwargs.items():
46            if isinstance(kwarg_value, dict) and is_bush(kwarg_value):
47                unique_chars.update(kwarg_key)
48        unique_chars_count = len(unique_chars)
49        looks_nice = unique_chars_count % whole_part_price == 0
50        return looks_nice
51    else:
52        return False
53
54def is_too_expensive(price):
55    return price > 42.00

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

OK

Дискусия
Росица Илиева
20.10.2024 12:53

Мерси много за обратната връзка. Ще се поправя!🙂
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    total_price = 02    total_price = 0
3    valid_bushes = False3    valid_bushes = False
4    for arg in args:4    for arg in args:
n5        if isinstance(arg, dict):n5        if isinstance(arg, dict) and is_bush(arg):
6            if is_bush(arg):
7                valid_bushes = True6            valid_bushes = True
8                total_price += cost_of_bush(arg)7            total_price += cost_of_bush(arg)
98
10    for kwarg_value in kwargs.values():9    for kwarg_value in kwargs.values():
n11        if isinstance(kwarg_value, dict):n10        if isinstance(kwarg_value, dict) and is_bush(kwarg_value):
12            if is_bush(kwarg_value):
13                valid_bushes = True11            valid_bushes = True
14                total_price += cost_of_bush(kwarg_value)12            total_price += cost_of_bush(kwarg_value)
1513
16    output = generate_output(kwargs, total_price, valid_bushes)14    output = generate_output(kwargs, total_price, valid_bushes)
17    return output15    return output
1816
19def generate_output(kwargs, total_price, valid_bushes):17def generate_output(kwargs, total_price, valid_bushes):
20    output_text = 'Ni!'18    output_text = 'Ni!'
21    if valid_bushes == False:19    if valid_bushes == False:
22        return output_text20        return output_text
23    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):21    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):
24        return output_text22        return output_text
25    else:23    else:
26        return f'{total_price:.2f}лв'24        return f'{total_price:.2f}лв'
2725
28def is_bush(arg):26def is_bush(arg):
29    key = 'name'27    key = 'name'
30    return key in arg.keys() and str(arg[key]).lower() in ('храст', 'shrub', 'bush')28    return key in arg.keys() and str(arg[key]).lower() in ('храст', 'shrub', 'bush')
3129
32def cost_of_bush(arg):30def cost_of_bush(arg):
33    value = 'cost'31    value = 'cost'
34    if value in arg.keys() and isinstance(arg[value], (int, float)):32    if value in arg.keys() and isinstance(arg[value], (int, float)):
35        price = arg[value]33        price = arg[value]
36        return price34        return price
37    return 035    return 0
3836
39def one_that_looks_nice(kwargs, price):37def one_that_looks_nice(kwargs, price):
40    whole_part_price = int(price)38    whole_part_price = int(price)
41    if whole_part_price == 0:39    if whole_part_price == 0:
42        return False40        return False
43    if len(kwargs) == 0:41    if len(kwargs) == 0:
44        return True42        return True
45    elif kwargs:43    elif kwargs:
46        unique_chars = set()44        unique_chars = set()
47        for kwarg_key, kwarg_value in kwargs.items():45        for kwarg_key, kwarg_value in kwargs.items():
t48            if isinstance(kwarg_value, dict) and kwarg_value['name'] in ('храст', 'shrub', 'bush'):t46            if isinstance(kwarg_value, dict) and is_bush(kwarg_value):
49                unique_chars.update(kwarg_key)47                unique_chars.update(kwarg_key)
50        unique_chars_count = len(unique_chars)48        unique_chars_count = len(unique_chars)
51        looks_nice = unique_chars_count % whole_part_price == 049        looks_nice = unique_chars_count % whole_part_price == 0
52        return looks_nice50        return looks_nice
53    else:51    else:
54        return False52        return False
5553
56def is_too_expensive(price):54def is_too_expensive(price):
57    return price > 42.0055    return price > 42.00
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    total_price = 02    total_price = 0
n3    valid_bushes = 0n3    valid_bushes = False
4    for arg in args:4    for arg in args:
5        if isinstance(arg, dict):5        if isinstance(arg, dict):
n6            if is_a_bush_validation(arg):n6            if is_bush(arg):
7                valid_bushes +17                valid_bushes = True
8                total_price += get_cost_of_bush(arg)8                total_price += cost_of_bush(arg)
99
10    for kwarg_value in kwargs.values():10    for kwarg_value in kwargs.values():
11        if isinstance(kwarg_value, dict):11        if isinstance(kwarg_value, dict):
n12            if is_a_bush_validation(kwarg_value):n12            if is_bush(kwarg_value):
13                valid_bushes +113                valid_bushes = True
14                total_price += get_cost_of_bush(kwarg_value)14                total_price += cost_of_bush(kwarg_value)
1515
16    output = generate_output(kwargs, total_price, valid_bushes)16    output = generate_output(kwargs, total_price, valid_bushes)
17    return output17    return output
1818
19def generate_output(kwargs, total_price, valid_bushes):19def generate_output(kwargs, total_price, valid_bushes):
20    output_text = 'Ni!'20    output_text = 'Ni!'
n21    if valid_bushes == 0:n21    if valid_bushes == False:
22        return(output_text)    22        return output_text
23    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):23    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):
n24        return output_text      n24        return output_text
25    else:25    else:
n26        return (get_formated_price(total_price))n26        return f'{total_price:.2f}лв'
2727
nn28def is_bush(arg):
29    key = 'name'
30    return key in arg.keys() and str(arg[key]).lower() in ('храст', 'shrub', 'bush')
2831
n29def is_a_bush_validation(arg):   n
30    key = 'name'
31    if key in arg.keys() and any(bush_values == str(arg[key]).lower() for bush_values in ['храст', 'shrub', 'bush']):
32        return True
33    else:
34        return False
35 
36def get_cost_of_bush(arg):32def cost_of_bush(arg):
37    value = 'cost'33    value = 'cost'
38    if value in arg.keys() and isinstance(arg[value], (int, float)):34    if value in arg.keys() and isinstance(arg[value], (int, float)):
39        price = arg[value]35        price = arg[value]
40        return price36        return price
n41    else:n
42        return 037    return 0
43    38 
44def one_that_looks_nice(kwargs, price):39def one_that_looks_nice(kwargs, price):
45    whole_part_price = int(price)40    whole_part_price = int(price)
46    if whole_part_price == 0:41    if whole_part_price == 0:
47        return False42        return False
48    if len(kwargs) == 0:43    if len(kwargs) == 0:
n49        return 0 % whole_part_price == 0n44        return True
50    elif kwargs:45    elif kwargs:
51        unique_chars = set()46        unique_chars = set()
n52        for kwarg_key,kwarg_value in kwargs.items():n47        for kwarg_key, kwarg_value in kwargs.items():
53            if(isinstance(kwarg_value, dict) and kwarg_value['name'] in ['храст', 'shrub', 'bush']): 48            if isinstance(kwarg_value, dict) and kwarg_value['name'] in ('храст', 'shrub', 'bush'):
54                unique_chars.update(kwarg_key)49                unique_chars.update(kwarg_key)
55        unique_chars_count = len(unique_chars)50        unique_chars_count = len(unique_chars)
n56        looks_nice = unique_chars_count % whole_part_price == 0 n51        looks_nice = unique_chars_count % whole_part_price == 0
57        return looks_nice52        return looks_nice
58    else:53    else:
n59        return False n54        return False
60   55 
61def is_too_expensive(price):56def is_too_expensive(price):
t62    if price > 42.00:t57    return price > 42.00
63        return True
64    return False
65 
66def get_formated_price(total_price):
67    return f'{total_price:.2f}лв'
68    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1def function_that_says_ni(*args,**kwargs):n1def function_that_says_ni(*args, **kwargs):
2    total_price = 02    total_price = 0
3    valid_bushes = 03    valid_bushes = 0
4    for arg in args:4    for arg in args:
5        if isinstance(arg, dict):5        if isinstance(arg, dict):
6            if is_a_bush_validation(arg):6            if is_a_bush_validation(arg):
7                valid_bushes += 17                valid_bushes += 1
8                total_price += get_cost_of_bush(arg)8                total_price += get_cost_of_bush(arg)
99
10    for kwarg_value in kwargs.values():10    for kwarg_value in kwargs.values():
11        if isinstance(kwarg_value, dict):11        if isinstance(kwarg_value, dict):
12            if is_a_bush_validation(kwarg_value):12            if is_a_bush_validation(kwarg_value):
13                valid_bushes += 113                valid_bushes += 1
14                total_price += get_cost_of_bush(kwarg_value)14                total_price += get_cost_of_bush(kwarg_value)
1515
16    output = generate_output(kwargs, total_price, valid_bushes)16    output = generate_output(kwargs, total_price, valid_bushes)
17    return output17    return output
1818
19def generate_output(kwargs, total_price, valid_bushes):19def generate_output(kwargs, total_price, valid_bushes):
20    output_text = 'Ni!'20    output_text = 'Ni!'
21    if valid_bushes == 0:21    if valid_bushes == 0:
22        return(output_text)    22        return(output_text)    
n23    elif not one_that_looks_nice(kwargs,total_price) or is_too_expensive(total_price):n23    elif not one_that_looks_nice(kwargs, total_price) or is_too_expensive(total_price):
24        return output_text      24        return output_text      
25    else:25    else:
26        return (get_formated_price(total_price))26        return (get_formated_price(total_price))
2727
2828
29def is_a_bush_validation(arg):   29def is_a_bush_validation(arg):   
30    key = 'name'30    key = 'name'
31    if key in arg.keys() and any(bush_values == str(arg[key]).lower() for bush_values in ['храст', 'shrub', 'bush']):31    if key in arg.keys() and any(bush_values == str(arg[key]).lower() for bush_values in ['храст', 'shrub', 'bush']):
32        return True32        return True
33    else:33    else:
34        return False34        return False
3535
36def get_cost_of_bush(arg):36def get_cost_of_bush(arg):
37    value = 'cost'37    value = 'cost'
t38    if value in arg.keys() and isinstance(arg[value], (int,float)):t38    if value in arg.keys() and isinstance(arg[value], (int, float)):
39        price = arg[value]39        price = arg[value]
40        return price40        return price
41    else:41    else:
42        return 042        return 0
43    43    
44def one_that_looks_nice(kwargs, price):44def one_that_looks_nice(kwargs, price):
45    whole_part_price = int(price)45    whole_part_price = int(price)
46    if whole_part_price == 0:46    if whole_part_price == 0:
47        return False47        return False
48    if len(kwargs) == 0:48    if len(kwargs) == 0:
49        return 0 % whole_part_price == 049        return 0 % whole_part_price == 0
50    elif kwargs:50    elif kwargs:
51        unique_chars = set()51        unique_chars = set()
52        for kwarg_key,kwarg_value in kwargs.items():52        for kwarg_key,kwarg_value in kwargs.items():
53            if(isinstance(kwarg_value, dict) and kwarg_value['name'] in ['храст', 'shrub', 'bush']): 53            if(isinstance(kwarg_value, dict) and kwarg_value['name'] in ['храст', 'shrub', 'bush']): 
54                unique_chars.update(kwarg_key)54                unique_chars.update(kwarg_key)
55        unique_chars_count = len(unique_chars)55        unique_chars_count = len(unique_chars)
56        looks_nice = unique_chars_count % whole_part_price == 0 56        looks_nice = unique_chars_count % whole_part_price == 0 
57        return looks_nice57        return looks_nice
58    else:58    else:
59        return False 59        return False 
60   60   
61def is_too_expensive(price):61def is_too_expensive(price):
62    if price > 42.00:62    if price > 42.00:
63        return True63        return True
64    return False64    return False
6565
66def get_formated_price(total_price):66def get_formated_price(total_price):
67    return f'{total_price:.2f}лв'67    return f'{total_price:.2f}лв'
68    68    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op