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

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

10 точки общо

10 успешни теста
0 неуспешни теста
Код (леки промени според обратната връзка)

 1def function_that_says_ni(*args, **kwargs):
 2    total_cost = 0
 3    accepted_bushes_count = 0
 4
 5    for argument in args:
 6        if type(argument) is dict and is_bush_valid(argument):
 7            total_cost += argument.get("cost", 0)
 8            accepted_bushes_count += 1
 9
10    accepted_bushes_keys = []
11
12    for key, value in kwargs.items():
13        if type(value) is dict and is_bush_valid(value):
14            total_cost += value.get("cost", 0)
15            accepted_bushes_count += 1
16            accepted_bushes_keys.append(key)
17
18    if (accepted_bushes_count != 0 and
19        is_shrubbery_nice(total_cost, accepted_bushes_keys) and not
20        total_cost > 42
21    ):
22        return f"{total_cost:.2f}лв"
23    else:
24        return "Ni!"
25
26
27def get_unique_symbols_count(keys):
28    return len(set(''.join(keys)))
29
30
31def is_bush_valid(bush):
32    return ("name" in bush and
33            type(bush["name"]) is str and
34            bush["name"].lower() in {"храст", "shrub", "bush"})
35
36
37def is_shrubbery_nice(total, accepted_keys):
38    whole_part = int(total)
39    return (whole_part != 0 and
40            get_unique_symbols_count(accepted_keys) % whole_part == 0)

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

OK

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

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
2    total_cost = 02    total_cost = 0
3    accepted_bushes_count = 03    accepted_bushes_count = 0
44
5    for argument in args:5    for argument in args:
n6        if type(argument) == dict and is_bush_valid(argument):n6        if type(argument) is dict and is_bush_valid(argument):
7            total_cost += argument.get("cost", 0)7            total_cost += argument.get("cost", 0)
8            accepted_bushes_count += 18            accepted_bushes_count += 1
99
10    accepted_bushes_keys = []10    accepted_bushes_keys = []
1111
12    for key, value in kwargs.items():12    for key, value in kwargs.items():
n13        if type(value) == dict and is_bush_valid(value):n13        if type(value) is dict and is_bush_valid(value):
14            total_cost += value.get("cost", 0)14            total_cost += value.get("cost", 0)
15            accepted_bushes_count += 115            accepted_bushes_count += 1
16            accepted_bushes_keys.append(key)16            accepted_bushes_keys.append(key)
1717
n18    if (n
19        accepted_bushes_count != 0 and18    if (accepted_bushes_count != 0 and
20        is_shrubbery_nice(total_cost, accepted_bushes_keys) and19        is_shrubbery_nice(total_cost, accepted_bushes_keys) and not
21        not is_sum_expensive(total_cost)20        total_cost > 42
22    ):21    ):
23        return f"{total_cost:.2f}лв"22        return f"{total_cost:.2f}лв"
24    else:23    else:
25        return "Ni!"24        return "Ni!"
2625
2726
n28def is_sum_expensive(total):n
29    return total > 42
30 
31 
32def get_unique_symbols_count(keys):27def get_unique_symbols_count(keys):
n33    keys_str = ''.join(keys)n
34    return len(set(keys_str))28    return len(set(''.join(keys)))
3529
3630
37def is_bush_valid(bush):31def is_bush_valid(bush):
38    return ("name" in bush and32    return ("name" in bush and
t39            type(bush["name"]) == str andt33            type(bush["name"]) is str and
40            bush["name"].lower() in {"храст", "shrub", "bush"})34            bush["name"].lower() in {"храст", "shrub", "bush"})
4135
4236
43def is_shrubbery_nice(total, accepted_keys):37def is_shrubbery_nice(total, accepted_keys):
44    whole_part = int(total)38    whole_part = int(total)
45    return (whole_part != 0 and39    return (whole_part != 0 and
46            get_unique_symbols_count(accepted_keys) % whole_part == 0)40            get_unique_symbols_count(accepted_keys) % whole_part == 0)
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op