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

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

11 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    valid_names = {"храст", "shrub", "bush"}
 3    total_cost = 0
 4    unique_letters = set()
 5
 6    for arg in args:
 7        if isinstance(arg, dict) and "name" in arg:
 8            name = arg["name"].lower()
 9            if name in valid_names:
10                cost = arg.get("cost", 0)
11                total_cost += cost
12    for key, data in kwargs.items():
13        if isinstance(data, dict) and "name" in data:
14            name = data["name"].lower()
15            if name in valid_names:
16                cost = data.get("cost", 0)
17                total_cost += cost
18                unique_letters.update(ch for ch in key)
19
20    if total_cost > 42:
21        return "Ni!"
22    whole_part = int(total_cost)
23    if whole_part == 0:
24        return "Ni!"
25    if len(unique_letters) % whole_part != 0:
26        return "Ni!"
27    return f"{total_cost:.2f}лв"

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

OK

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

https://peps.python.org/pep-0008/#blank-lines Препоръчително е да намалиш празните редове в тялото на функцията си. Наблягам на думата "препоръчително". Не е задължително.
История

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
n2    valid_names = ["храст", "shrub", "bush"]n2    valid_names = {"храст", "shrub", "bush"}
3    total_cost = 03    total_cost = 0
4    unique_letters = set()4    unique_letters = set()
55
6    for arg in args:6    for arg in args:
7        if isinstance(arg, dict) and "name" in arg:7        if isinstance(arg, dict) and "name" in arg:
8            name = arg["name"].lower()8            name = arg["name"].lower()
n9 n
10            if name in valid_names:9            if name in valid_names:
n11                cost = arg["cost"] if "cost" in arg else 0n10                cost = arg.get("cost", 0)
12                total_cost += cost11                total_cost += cost
n13 n
14    for key, data in kwargs.items():12    for key, data in kwargs.items():
15        if isinstance(data, dict) and "name" in data:13        if isinstance(data, dict) and "name" in data:
16            name = data["name"].lower()14            name = data["name"].lower()
n17 n
18            if name in valid_names:15            if name in valid_names:
n19                cost = data["cost"] if "cost" in data else 0n16                cost = data.get("cost", 0)
20                total_cost += cost17                total_cost += cost
n21 n18                unique_letters.update(ch for ch in key)
22                unique_letters.update(ch for ch in key if ch == "_" or (ch.islower() and ch.isalpha()))
2319
24    if total_cost > 42:20    if total_cost > 42:
25        return "Ni!"21        return "Ni!"
n26 n
27    whole_part = int(total_cost)22    whole_part = int(total_cost)
n28 n
29    if whole_part == 0:23    if whole_part == 0:
30        return "Ni!"24        return "Ni!"
n31 n
32    if len(unique_letters) % whole_part != 0:25    if len(unique_letters) % whole_part != 0:
33        return "Ni!"26        return "Ni!"
t34 t
35    return f"{total_cost:.2f}лв"27    return f"{total_cost:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op