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

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

11 точки общо

10 успешни теста
0 неуспешни теста
Код (Last change)

 1def valid_bush_name(bush):
 2    if not isinstance(bush, dict):
 3        return False
 4    current_key_name = bush.get("name")
 5    valid_bush_names = ("храст", "shrub", "bush")
 6    if not current_key_name:
 7        return False
 8    return current_key_name.lower() in valid_bush_names
 9
10
11def function_that_says_ni(*args, **kwargs):
12    potential_bushes = list(filter(valid_bush_name, [*args, *kwargs.values()]))
13    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())
14
15    max_price = 42.0
16    generated_bush_price = 0.0
17
18    for bush in potential_bushes:
19        if bush.get("cost"):
20            generated_bush_price += bush["cost"]
21        if generated_bush_price > max_price:
22            return "Ni!"
23
24    unique_letters = len({letter for (string, val) in kwargs.items() for letter in string if valid_bush_name(val)})
25
26    if int(generated_bush_price) == 0:
27        return "Ni!"
28    if unique_letters % int(generated_bush_price) == 0:
29        return f"{generated_bush_price:.2f}лв"
30    return "Ni!"

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

OK

Дискусия
Виктор Бечев
17.10.2024 12:24

Чисто решение, нямам забележки.
История

f1def valid_bush_name(bush):f1def valid_bush_name(bush):
nn2    if not isinstance(bush, dict):
3        return False
2    current_key_name = bush.get("name")4    current_key_name = bush.get("name")
3    valid_bush_names = ("храст", "shrub", "bush")5    valid_bush_names = ("храст", "shrub", "bush")
4    if not current_key_name:6    if not current_key_name:
5        return False7        return False
6    return current_key_name.lower() in valid_bush_names8    return current_key_name.lower() in valid_bush_names
79
nn10 
8def function_that_says_ni(*args, **kwargs):11def function_that_says_ni(*args, **kwargs):
n9    potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()]))n12    potential_bushes = list(filter(valid_bush_name, [*args, *kwargs.values()]))
10    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())13    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())
1114
12    max_price = 42.015    max_price = 42.0
13    generated_bush_price = 0.016    generated_bush_price = 0.0
1417
15    for bush in potential_bushes:18    for bush in potential_bushes:
n16        if not valid_bush_name(bush):n
17            return "Ni!"
18        if bush.get("cost"):19        if bush.get("cost"):
19            generated_bush_price += bush["cost"]20            generated_bush_price += bush["cost"]
20        if generated_bush_price > max_price:21        if generated_bush_price > max_price:
21            return "Ni!"22            return "Ni!"
2223
n23    unique_letters = len({letter for string in kwargs.keys() for letter in string})n24    unique_letters = len({letter for (string, val) in kwargs.items() for letter in string if valid_bush_name(val)})
2425
25    if int(generated_bush_price) == 0:26    if int(generated_bush_price) == 0:
26        return "Ni!"27        return "Ni!"
27    if unique_letters % int(generated_bush_price) == 0:28    if unique_letters % int(generated_bush_price) == 0:
28        return f"{generated_bush_price:.2f}лв"29        return f"{generated_bush_price:.2f}лв"
29    return "Ni!"30    return "Ni!"
t30 t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def valid_bush_name(bush):f1def valid_bush_name(bush):
2    current_key_name = bush.get("name")2    current_key_name = bush.get("name")
3    valid_bush_names = ("храст", "shrub", "bush")3    valid_bush_names = ("храст", "shrub", "bush")
4    if not current_key_name:4    if not current_key_name:
5        return False5        return False
6    return current_key_name.lower() in valid_bush_names6    return current_key_name.lower() in valid_bush_names
n7 n
8 
9def get_unique_letters_count(data):
10    letter_set = set()
11    for string in data:
12        for letter in string:
13            letter_set.add(letter)
14    return len(letter_set)
15 
167
17def function_that_says_ni(*args, **kwargs):8def function_that_says_ni(*args, **kwargs):
18    potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()]))9    potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()]))
19    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())10    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())
2011
21    max_price = 42.012    max_price = 42.0
22    generated_bush_price = 0.013    generated_bush_price = 0.0
2314
24    for bush in potential_bushes:15    for bush in potential_bushes:
25        if not valid_bush_name(bush):16        if not valid_bush_name(bush):
26            return "Ni!"17            return "Ni!"
27        if bush.get("cost"):18        if bush.get("cost"):
28            generated_bush_price += bush["cost"]19            generated_bush_price += bush["cost"]
29        if generated_bush_price > max_price:20        if generated_bush_price > max_price:
30            return "Ni!"21            return "Ni!"
3122
t32    unique_letters = get_unique_letters_count(kwargs.keys())t23    unique_letters = len({letter for string in kwargs.keys() for letter in string})
3324
34    if int(generated_bush_price) == 0:25    if int(generated_bush_price) == 0:
35        return "Ni!"26        return "Ni!"
36    if unique_letters % int(generated_bush_price) == 0:27    if unique_letters % int(generated_bush_price) == 0:
37        return f"{generated_bush_price:.2f}лв"28        return f"{generated_bush_price:.2f}лв"
38    return "Ni!"29    return "Ni!"
3930
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def valid_bush_name(bush):f1def valid_bush_name(bush):
2    current_key_name = bush.get("name")2    current_key_name = bush.get("name")
3    valid_bush_names = ("храст", "shrub", "bush")3    valid_bush_names = ("храст", "shrub", "bush")
4    if not current_key_name:4    if not current_key_name:
5        return False5        return False
6    return current_key_name.lower() in valid_bush_names6    return current_key_name.lower() in valid_bush_names
77
88
9def get_unique_letters_count(data):9def get_unique_letters_count(data):
10    letter_set = set()10    letter_set = set()
11    for string in data:11    for string in data:
12        for letter in string:12        for letter in string:
13            letter_set.add(letter)13            letter_set.add(letter)
14    return len(letter_set)14    return len(letter_set)
1515
1616
17def function_that_says_ni(*args, **kwargs):17def function_that_says_ni(*args, **kwargs):
18    potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()]))18    potential_bushes = list(filter(lambda x: isinstance(x, dict), [*args, *kwargs.values()]))
19    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())19    # не сме споменавали isinstance ама се ползва ако искаме да напишем нещо от сорта на type(x) == type(dict())
2020
21    max_price = 42.021    max_price = 42.0
22    generated_bush_price = 0.022    generated_bush_price = 0.0
2323
24    for bush in potential_bushes:24    for bush in potential_bushes:
25        if not valid_bush_name(bush):25        if not valid_bush_name(bush):
n26            return "Ni"n26            return "Ni!"
27        if bush.get("cost"):27        if bush.get("cost"):
28            generated_bush_price += bush["cost"]28            generated_bush_price += bush["cost"]
29        if generated_bush_price > max_price:29        if generated_bush_price > max_price:
n30            return "Ni"n30            return "Ni!"
3131
32    unique_letters = get_unique_letters_count(kwargs.keys())32    unique_letters = get_unique_letters_count(kwargs.keys())
nn33 
34    if int(generated_bush_price) == 0:
35        return "Ni!"
33    if unique_letters % int(generated_bush_price) == 0:36    if unique_letters % int(generated_bush_price) == 0:
n34        return f"{generated_bush_price:.2f}лв" # дори да го няма в тестовете се затраховамn37        return f"{generated_bush_price:.2f}лв"
35    return "Ni"38    return "Ni!"
3639
t37 t
38print(function_that_says_ni(aabcc={"name": "bush", "cost": 3.20}))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op