Домашни > Функцията, която казва "Ni!" > Решения > Решението на Георги Балтиев

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

11 точки общо

10 успешни теста
0 неуспешни теста
Код (Допълнителни корекции)

 1ACCEPTED_NAMES = ['храст', 'bush', 'shrub']
 2
 3def is_a_valid_bush(argument):
 4    if type(argument) != dict:
 5        return False
 6    else:
 7        if not len(argument) or argument.get("name") is None:
 8            return False
 9        else:
10            return (lambda x: x.lower())(argument.get("name")) in ACCEPTED_NAMES
11
12def calculate_bush_price(argument):
13    return 0 if argument.get("cost") is None else argument.get("cost")
14
15def function_that_says_ni(*args, **kwargs) :
16    found_any_valid_bush = False
17    valid_bushes = list(filter(is_a_valid_bush, args))
18
19    if len(valid_bushes):
20        found_any_valid_bush = True
21
22    bushes_sum = sum(map(calculate_bush_price, valid_bushes))
23
24    letters_set= set()
25
26    for key, value in kwargs.items():
27        if not is_a_valid_bush(value):
28            continue
29        else:
30            found_any_valid_bush = True
31
32        for letter in key:
33            letters_set.add(letter)
34
35        bushes_sum += calculate_bush_price(value)
36
37    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_set) % int(bushes_sum) != 0:
38        return "Ni!"
39    else:
40        return f"{format(bushes_sum, ".2f")}лв"

..........
----------------------------------------------------------------------
Ran 10 tests in 0.001s

OK

Дискусия
Георги Кунчев
20.10.2024 12:08

Само заради скоростта, с която адресира коментарите ми, давам бонус точка.
История

f1ACCEPTED_NAMES = ['храст', 'bush', 'shrub']f1ACCEPTED_NAMES = ['храст', 'bush', 'shrub']
22
3def is_a_valid_bush(argument):3def is_a_valid_bush(argument):
4    if type(argument) != dict:4    if type(argument) != dict:
5        return False5        return False
6    else:6    else:
7        if not len(argument) or argument.get("name") is None:7        if not len(argument) or argument.get("name") is None:
8            return False8            return False
9        else:9        else:
10            return (lambda x: x.lower())(argument.get("name")) in ACCEPTED_NAMES10            return (lambda x: x.lower())(argument.get("name")) in ACCEPTED_NAMES
1111
12def calculate_bush_price(argument):12def calculate_bush_price(argument):
13    return 0 if argument.get("cost") is None else argument.get("cost")13    return 0 if argument.get("cost") is None else argument.get("cost")
1414
15def function_that_says_ni(*args, **kwargs) :15def function_that_says_ni(*args, **kwargs) :
16    found_any_valid_bush = False16    found_any_valid_bush = False
17    valid_bushes = list(filter(is_a_valid_bush, args))17    valid_bushes = list(filter(is_a_valid_bush, args))
1818
19    if len(valid_bushes):19    if len(valid_bushes):
20        found_any_valid_bush = True20        found_any_valid_bush = True
2121
22    bushes_sum = sum(map(calculate_bush_price, valid_bushes))22    bushes_sum = sum(map(calculate_bush_price, valid_bushes))
2323
n24    letters_dict = {}n24    letters_set= set()
2525
26    for key, value in kwargs.items():26    for key, value in kwargs.items():
27        if not is_a_valid_bush(value):27        if not is_a_valid_bush(value):
28            continue28            continue
29        else:29        else:
30            found_any_valid_bush = True30            found_any_valid_bush = True
3131
n32        for x in key:n32        for letter in key:
33            letters_dict[x] = 133            letters_set.add(letter)
3434
35        bushes_sum += calculate_bush_price(value)35        bushes_sum += calculate_bush_price(value)
3636
t37    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_dict) % int(bushes_sum) != 0:t37    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_set) % int(bushes_sum) != 0:
38        return "Ni!"38        return "Ni!"
39    else:39    else:
40        return f"{format(bushes_sum, ".2f")}лв"40        return f"{format(bushes_sum, ".2f")}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

n1accepted_names = ['храст', 'bush', 'shrub']n1ACCEPTED_NAMES = ['храст', 'bush', 'shrub']
22
3def is_a_valid_bush(argument):3def is_a_valid_bush(argument):
4    if type(argument) != dict:4    if type(argument) != dict:
5        return False5        return False
6    else:6    else:
n7        if not len(argument) or len(argument) != len(set(argument.keys()).intersection({"cost", "name"})):n7        if not len(argument) or argument.get("name") is None:
8            return False8            return False
9        else:9        else:
n10            return False if (lambda x: x.lower())(argument.get("name")) not in accepted_names else Truen10            return (lambda x: x.lower())(argument.get("name")) in ACCEPTED_NAMES
1111
n12def calculate_bush_price(argument) :n12def calculate_bush_price(argument):
13    return 0 if argument.get("cost") is None else argument.get("cost")13    return 0 if argument.get("cost") is None else argument.get("cost")
1414
15def function_that_says_ni(*args, **kwargs) :15def function_that_says_ni(*args, **kwargs) :
16    found_any_valid_bush = False16    found_any_valid_bush = False
n17 n
18    valid_bushes = list(filter(is_a_valid_bush, args))17    valid_bushes = list(filter(is_a_valid_bush, args))
1918
20    if len(valid_bushes):19    if len(valid_bushes):
21        found_any_valid_bush = True20        found_any_valid_bush = True
2221
23    bushes_sum = sum(map(calculate_bush_price, valid_bushes))22    bushes_sum = sum(map(calculate_bush_price, valid_bushes))
2423
25    letters_dict = {}24    letters_dict = {}
2625
27    for key, value in kwargs.items():26    for key, value in kwargs.items():
28        if not is_a_valid_bush(value):27        if not is_a_valid_bush(value):
29            continue28            continue
30        else:29        else:
31            found_any_valid_bush = True30            found_any_valid_bush = True
3231
33        for x in key:32        for x in key:
34            letters_dict[x] = 133            letters_dict[x] = 1
3534
36        bushes_sum += calculate_bush_price(value)35        bushes_sum += calculate_bush_price(value)
3736
38    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_dict) % int(bushes_sum) != 0:37    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_dict) % int(bushes_sum) != 0:
39        return "Ni!"38        return "Ni!"
t40    else :t39    else:
41        return f"{format(bushes_sum, ".2f")}лв"40        return f"{format(bushes_sum, ".2f")}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1accepted_names = ['храст', 'bush', 'shrub']f1accepted_names = ['храст', 'bush', 'shrub']
22
3def is_a_valid_bush(argument):3def is_a_valid_bush(argument):
4    if type(argument) != dict:4    if type(argument) != dict:
5        return False5        return False
6    else:6    else:
7        if not len(argument) or len(argument) != len(set(argument.keys()).intersection({"cost", "name"})):7        if not len(argument) or len(argument) != len(set(argument.keys()).intersection({"cost", "name"})):
8            return False8            return False
9        else:9        else:
10            return False if (lambda x: x.lower())(argument.get("name")) not in accepted_names else True10            return False if (lambda x: x.lower())(argument.get("name")) not in accepted_names else True
1111
12def calculate_bush_price(argument) :12def calculate_bush_price(argument) :
n13    if argument.get("cost") is None:n13    return 0 if argument.get("cost") is None else argument.get("cost")
14        return 0
15    return argument.get("cost")
1614
17def function_that_says_ni(*args, **kwargs) :15def function_that_says_ni(*args, **kwargs) :
18    found_any_valid_bush = False16    found_any_valid_bush = False
1917
20    valid_bushes = list(filter(is_a_valid_bush, args))18    valid_bushes = list(filter(is_a_valid_bush, args))
2119
22    if len(valid_bushes):20    if len(valid_bushes):
23        found_any_valid_bush = True21        found_any_valid_bush = True
2422
25    bushes_sum = sum(map(calculate_bush_price, valid_bushes))23    bushes_sum = sum(map(calculate_bush_price, valid_bushes))
2624
27    letters_dict = {}25    letters_dict = {}
2826
29    for key, value in kwargs.items():27    for key, value in kwargs.items():
30        if not is_a_valid_bush(value):28        if not is_a_valid_bush(value):
31            continue29            continue
32        else:30        else:
33            found_any_valid_bush = True31            found_any_valid_bush = True
3432
35        for x in key:33        for x in key:
36            letters_dict[x] = 134            letters_dict[x] = 1
3735
38        bushes_sum += calculate_bush_price(value)36        bushes_sum += calculate_bush_price(value)
3937
40    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_dict) % int(bushes_sum) != 0:38    if not found_any_valid_bush or bushes_sum > 42 or int(bushes_sum) == 0 or len(letters_dict) % int(bushes_sum) != 0:
41        return "Ni!"39        return "Ni!"
42    else :40    else :
t43        return f"{format(bushes_sum, ".2f")}лв."t41        return f"{format(bushes_sum, ".2f")}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op