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

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

10 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    valid_keys = ("bush", "shrub", "храст") # added tuple with valid keywords
 3    cost = 0.0
 4    kwargs_names = []
 5    unique_letters = set()
 6
 7    for arg in args:
 8        if type(arg) == dict:
 9            name = arg.get("name")
10            if name is not None:
11                if type(name) == str:
12                    name = name.lower()
13                    if name in valid_keys:
14                        # corrected without additional if-check
15                        cost += arg.get("cost", 0)
16
17    for key, value in kwargs.items():
18        if type(value) == dict:
19            name = value.get("name")
20            if name is not None:
21                if type(name) == str:
22                    name = name.lower()
23                    if name in valid_keys:
24                        kwargs_names.append(key)
25                        # corrected without additional if-check
26                        cost += value.get("cost", 0)
27
28    for name in kwargs_names:
29        # corrected using built-in method
30        unique_letters.update(name)
31
32    unique_letters_count = len(unique_letters)
33    whole_part = int(cost)
34
35    if cost <= 42.0 and whole_part > 0:
36        if unique_letters_count % whole_part == 0:
37            # corrected return
38            return f"{cost:.2f}лв"
39
40    return "Ni!"

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

OK

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

f1def function_that_says_ni(*args, **kwargs):f1def function_that_says_ni(*args, **kwargs):
n2    valid_keys = ("bush", "shrub", "храст")n2    valid_keys = ("bush", "shrub", "храст") # added tuple with valid keywords
3    cost = 0.03    cost = 0.0
4    kwargs_names = []4    kwargs_names = []
5    unique_letters = set()5    unique_letters = set()
66
7    for arg in args:7    for arg in args:
8        if type(arg) == dict:8        if type(arg) == dict:
9            name = arg.get("name")9            name = arg.get("name")
10            if name is not None:10            if name is not None:
11                if type(name) == str:11                if type(name) == str:
12                    name = name.lower()12                    name = name.lower()
13                    if name in valid_keys:13                    if name in valid_keys:
nn14                        # corrected without additional if-check
14                        cost += arg.get("cost", 0)15                        cost += arg.get("cost", 0)
1516
16    for key, value in kwargs.items():17    for key, value in kwargs.items():
17        if type(value) == dict:18        if type(value) == dict:
18            name = value.get("name")19            name = value.get("name")
19            if name is not None:20            if name is not None:
20                if type(name) == str:21                if type(name) == str:
21                    name = name.lower()22                    name = name.lower()
22                    if name in valid_keys:23                    if name in valid_keys:
23                        kwargs_names.append(key)24                        kwargs_names.append(key)
nn25                        # corrected without additional if-check
24                        cost += value.get("cost", 0)26                        cost += value.get("cost", 0)
2527
26    for name in kwargs_names:28    for name in kwargs_names:
nn29        # corrected using built-in method
27        unique_letters.update(name)30        unique_letters.update(name)
2831
29    unique_letters_count = len(unique_letters)32    unique_letters_count = len(unique_letters)
30    whole_part = int(cost)33    whole_part = int(cost)
3134
32    if cost <= 42.0 and whole_part > 0:35    if cost <= 42.0 and whole_part > 0:
33        if unique_letters_count % whole_part == 0:36        if unique_letters_count % whole_part == 0:
nn37            # corrected return
34            return f"{cost:.2f}лв"38            return f"{cost:.2f}лв"
3539
36    return "Ni!"40    return "Ni!"
t37 t
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):
n2    is_shrub = False  #"храсталак"n2    valid_keys = ("bush", "shrub", "храст")
3    cost = 0.03    cost = 0.0
4    kwargs_names = []4    kwargs_names = []
5    unique_letters = set()5    unique_letters = set()
66
7    for arg in args:7    for arg in args:
8        if type(arg) == dict:8        if type(arg) == dict:
9            name = arg.get("name")9            name = arg.get("name")
10            if name is not None:10            if name is not None:
11                if type(name) == str:11                if type(name) == str:
12                    name = name.lower()12                    name = name.lower()
n13                    if name == "bush" or name == "shrub" or name == "храст":n13                    if name in valid_keys:
14                        if arg.get("cost") is not None:
15                            cost += arg.get("cost")14                        cost += arg.get("cost", 0)
1615
17    for key, value in kwargs.items():16    for key, value in kwargs.items():
18        if type(value) == dict:17        if type(value) == dict:
19            name = value.get("name")18            name = value.get("name")
20            if name is not None:19            if name is not None:
21                if type(name) == str:20                if type(name) == str:
22                    name = name.lower()21                    name = name.lower()
n23                    if name == "bush" or name == "shrub" or name == "храст":n22                    if name in valid_keys:
24                        kwargs_names.append(key)23                        kwargs_names.append(key)
n25                        if value.get("cost") is not None:n
26                            cost += value.get("cost")24                        cost += value.get("cost", 0)
2725
28    for name in kwargs_names:26    for name in kwargs_names:
n29        for char in name:n
30            unique_letters.add(char)27        unique_letters.update(name)
3128
32    unique_letters_count = len(unique_letters)29    unique_letters_count = len(unique_letters)
33    whole_part = int(cost)30    whole_part = int(cost)
3431
35    if cost <= 42.0 and whole_part > 0:32    if cost <= 42.0 and whole_part > 0:
36        if unique_letters_count % whole_part == 0:33        if unique_letters_count % whole_part == 0:
n37            is_shrub = Truen34            return f"{cost:.2f}лв"
3835
t39    if not is_shrub:t
40        return "Ni!"36    return "Ni!"
41    else:37 
42        return f"{cost:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op