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

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

9 точки общо

9 успешни теста
1 неуспешни теста
Код

 1def get_dictionaries_from_arguments(*args, **kwargs):
 2    dictionaries = []
 3    for arg in args:
 4        if type(arg) is dict:
 5            dictionaries.append(arg)
 6    
 7    for key, value in kwargs.items():
 8        if type(value) is dict:
 9            dictionaries.append(value)
10
11    return dictionaries 
12
13
14
15def is_shrub(dictionary):
16    possible_bush_names = ["храст", "bush", "shrub"]
17    return "name" in dictionary and type(dictionary["name"]) is str and dictionary["name"].lower() in possible_bush_names
18        
19
20
21def cost_of_shrubbery(shrubs):
22    cost = 0
23    for shrub in shrubs:
24        if shrub.get("cost"):
25            cost += shrub["cost"]
26    return cost
27
28
29
30def is_looking_nice(cost, **kwargs):
31    unique_letters = []
32    for key, value in kwargs.items():
33        for letter in key:
34            unique_letters.append(letter)
35    unique_letters = set(unique_letters)
36    return len(unique_letters) % int(cost) == 0
37
38
39
40def function_that_says_ni(*args, **kwargs):
41    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)
42    shrubs = list(filter(is_shrub, dictionaries))
43    cost = cost_of_shrubbery(shrubs)
44    if cost > 42 or cost == 0:
45        return("Ni!")
46    else:
47        if is_looking_nice(cost, **kwargs):
48            return(f"{cost:.2f}лв")
49        else:
50            return("Ni!")

..E.......
======================================================================
ERROR: test_cost_whole_part_zero (test.TestNi.test_cost_whole_part_zero)
Test with a total cost part equal to zero.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 43, in test_cost_whole_part_zero
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 0.1},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 47, in function_that_says_ni
if is_looking_nice(cost, **kwargs):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 36, in is_looking_nice
return len(unique_letters) % int(cost) == 0
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

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

FAILED (errors=1)

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

f1def get_dictionaries_from_arguments(*args, **kwargs):f1def get_dictionaries_from_arguments(*args, **kwargs):
2    dictionaries = []2    dictionaries = []
3    for arg in args:3    for arg in args:
4        if type(arg) is dict:4        if type(arg) is dict:
5            dictionaries.append(arg)5            dictionaries.append(arg)
n6 n
7    6    
8    for key, value in kwargs.items():7    for key, value in kwargs.items():
9        if type(value) is dict:8        if type(value) is dict:
10            dictionaries.append(value)9            dictionaries.append(value)
1110
12    return dictionaries 11    return dictionaries 
1312
1413
1514
16def is_shrub(dictionary):15def is_shrub(dictionary):
n17    if "name" in dictionary and(n16    possible_bush_names = ["храст", "bush", "shrub"]
18        type(dictionary["name"]) is str) and(17    return "name" in dictionary and type(dictionary["name"]) is str and dictionary["name"].lower() in possible_bush_names
19        dictionary["name"].lower() == "храст" or 
20        dictionary["name"].lower() == "bush" or 
21        dictionary["name"].lower() == "shrub"):
22        if "cost" not in dictionary:
23            dictionary["cost"] = 0
24        return True
25    return False
26        18        
2719
2820
29def cost_of_shrubbery(shrubs):21def cost_of_shrubbery(shrubs):
30    cost = 022    cost = 0
31    for shrub in shrubs:23    for shrub in shrubs:
nn24        if shrub.get("cost"):
32        cost += shrub["cost"]25            cost += shrub["cost"]
33    return cost26    return cost
3427
3528
3629
n37def isLookingNice(cost, **kwargs):n30def is_looking_nice(cost, **kwargs):
38    unique_letters = []31    unique_letters = []
39    for key, value in kwargs.items():32    for key, value in kwargs.items():
40        for letter in key:33        for letter in key:
41            unique_letters.append(letter)34            unique_letters.append(letter)
42    unique_letters = set(unique_letters)35    unique_letters = set(unique_letters)
n43    if len(unique_letters) % int(cost) == 0:n36    return len(unique_letters) % int(cost) == 0
44        return True
45    return False
4637
4738
4839
49def function_that_says_ni(*args, **kwargs):40def function_that_says_ni(*args, **kwargs):
50    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)41    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)
51    shrubs = list(filter(is_shrub, dictionaries))42    shrubs = list(filter(is_shrub, dictionaries))
52    cost = cost_of_shrubbery(shrubs)43    cost = cost_of_shrubbery(shrubs)
n53    if cost > 42.00 or cost == 0.00:n44    if cost > 42 or cost == 0:
54        print("Ni")45        return("Ni!")
55        return 0
56    else:46    else:
n57        if isLookingNice(cost, **kwargs):n47        if is_looking_nice(cost, **kwargs):
58            print(f"{cost:.2f}лв")48            return(f"{cost:.2f}лв")
59            return 0
60        else:49        else:
t61            print("Ni")t50            return("Ni!")
62            return 051 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def get_dictionaries_from_arguments(*args, **kwargs):f1def get_dictionaries_from_arguments(*args, **kwargs):
2    dictionaries = []2    dictionaries = []
3    for arg in args:3    for arg in args:
4        if type(arg) is dict:4        if type(arg) is dict:
5            dictionaries.append(arg)5            dictionaries.append(arg)
66
7    7    
8    for key, value in kwargs.items():8    for key, value in kwargs.items():
9        if type(value) is dict:9        if type(value) is dict:
10            dictionaries.append(value)10            dictionaries.append(value)
n11 n
1211
13    return dictionaries 12    return dictionaries 
1413
1514
1615
17def is_shrub(dictionary):16def is_shrub(dictionary):
18    if "name" in dictionary and(17    if "name" in dictionary and(
19        type(dictionary["name"]) is str) and(18        type(dictionary["name"]) is str) and(
20        dictionary["name"].lower() == "храст" or 19        dictionary["name"].lower() == "храст" or 
21        dictionary["name"].lower() == "bush" or 20        dictionary["name"].lower() == "bush" or 
22        dictionary["name"].lower() == "shrub"):21        dictionary["name"].lower() == "shrub"):
23        if "cost" not in dictionary:22        if "cost" not in dictionary:
24            dictionary["cost"] = 023            dictionary["cost"] = 0
25        return True24        return True
26    return False25    return False
27        26        
2827
2928
30def cost_of_shrubbery(shrubs):29def cost_of_shrubbery(shrubs):
31    cost = 030    cost = 0
32    for shrub in shrubs:31    for shrub in shrubs:
33        cost += shrub["cost"]32        cost += shrub["cost"]
34    return cost33    return cost
3534
3635
3736
38def isLookingNice(cost, **kwargs):37def isLookingNice(cost, **kwargs):
39    unique_letters = []38    unique_letters = []
40    for key, value in kwargs.items():39    for key, value in kwargs.items():
41        for letter in key:40        for letter in key:
42            unique_letters.append(letter)41            unique_letters.append(letter)
43    unique_letters = set(unique_letters)42    unique_letters = set(unique_letters)
44    if len(unique_letters) % int(cost) == 0:43    if len(unique_letters) % int(cost) == 0:
45        return True44        return True
46    return False45    return False
4746
4847
4948
50def function_that_says_ni(*args, **kwargs):49def function_that_says_ni(*args, **kwargs):
51    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)50    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)
52    shrubs = list(filter(is_shrub, dictionaries))51    shrubs = list(filter(is_shrub, dictionaries))
53    cost = cost_of_shrubbery(shrubs)52    cost = cost_of_shrubbery(shrubs)
54    if cost > 42.00 or cost == 0.00:53    if cost > 42.00 or cost == 0.00:
55        print("Ni")54        print("Ni")
56        return 055        return 0
57    else:56    else:
58        if isLookingNice(cost, **kwargs):57        if isLookingNice(cost, **kwargs):
59            print(f"{cost:.2f}лв")58            print(f"{cost:.2f}лв")
60            return 059            return 0
61        else:60        else:
62            print("Ni")61            print("Ni")
63            return 062            return 0
t64 t
65        
66 
67 
68# function_that_says_ni(123, "test", {"name": "bush", "cost": 2}, [1, 2, 3], my_bush = {"name": "sHrUb", "cost": 1}, my_not_bush = {"name": 3}, my_list = [1,2])
69# function_that_says_ni({"name": "храст", "cost": 120})
70# function_that_says_ni({"name": "храст", "cost": 1})
71# function_that_says_ni(aabcc={"name": "bush", "cost": 3.20})
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1def get_dictionaries_from_arguments(*args, **kwargs):f1def get_dictionaries_from_arguments(*args, **kwargs):
2    dictionaries = []2    dictionaries = []
3    for arg in args:3    for arg in args:
4        if type(arg) is dict:4        if type(arg) is dict:
5            dictionaries.append(arg)5            dictionaries.append(arg)
66
7    7    
8    for key, value in kwargs.items():8    for key, value in kwargs.items():
9        if type(value) is dict:9        if type(value) is dict:
10            dictionaries.append(value)10            dictionaries.append(value)
1111
1212
13    return dictionaries 13    return dictionaries 
1414
1515
1616
17def is_shrub(dictionary):17def is_shrub(dictionary):
18    if "name" in dictionary and(18    if "name" in dictionary and(
19        type(dictionary["name"]) is str) and(19        type(dictionary["name"]) is str) and(
20        dictionary["name"].lower() == "храст" or 20        dictionary["name"].lower() == "храст" or 
21        dictionary["name"].lower() == "bush" or 21        dictionary["name"].lower() == "bush" or 
22        dictionary["name"].lower() == "shrub"):22        dictionary["name"].lower() == "shrub"):
23        if "cost" not in dictionary:23        if "cost" not in dictionary:
24            dictionary["cost"] = 024            dictionary["cost"] = 0
25        return True25        return True
26    return False26    return False
27        27        
2828
2929
30def cost_of_shrubbery(shrubs):30def cost_of_shrubbery(shrubs):
31    cost = 031    cost = 0
32    for shrub in shrubs:32    for shrub in shrubs:
33        cost += shrub["cost"]33        cost += shrub["cost"]
34    return cost34    return cost
3535
3636
3737
38def isLookingNice(cost, **kwargs):38def isLookingNice(cost, **kwargs):
39    unique_letters = []39    unique_letters = []
40    for key, value in kwargs.items():40    for key, value in kwargs.items():
41        for letter in key:41        for letter in key:
42            unique_letters.append(letter)42            unique_letters.append(letter)
43    unique_letters = set(unique_letters)43    unique_letters = set(unique_letters)
44    if len(unique_letters) % int(cost) == 0:44    if len(unique_letters) % int(cost) == 0:
45        return True45        return True
46    return False46    return False
4747
4848
4949
50def function_that_says_ni(*args, **kwargs):50def function_that_says_ni(*args, **kwargs):
51    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)51    dictionaries = get_dictionaries_from_arguments(*args, **kwargs)
52    shrubs = list(filter(is_shrub, dictionaries))52    shrubs = list(filter(is_shrub, dictionaries))
53    cost = cost_of_shrubbery(shrubs)53    cost = cost_of_shrubbery(shrubs)
54    if cost > 42.00 or cost == 0.00:54    if cost > 42.00 or cost == 0.00:
55        print("Ni")55        print("Ni")
56        return 056        return 0
57    else:57    else:
58        if isLookingNice(cost, **kwargs):58        if isLookingNice(cost, **kwargs):
59            print(f"{cost:.2f}лв")59            print(f"{cost:.2f}лв")
60            return 060            return 0
61        else:61        else:
62            print("Ni")62            print("Ni")
63            return 063            return 0
6464
65        65        
6666
6767
t68function_that_says_ni(123, "test", {"name": "bush", "cost": 2}, [1, 2, 3], my_bush = {"name": "sHrUb", "cost": 1}, my_not_bush = {"name": 3}, my_list = [1,2])t68function_that_says_ni(123, "test", {"name": "bush", "cost": 2}, [1, 2, 3], my_bush = {"name": "sHrUb", "cost": 1}, my_not_bush = {"name": 3}, my_list = [1,2])
69function_that_says_ni({"name": "храст", "cost": 120})69function_that_says_ni({"name": "храст", "cost": 120})
70function_that_says_ni({"name": "храст", "cost": 1})70function_that_says_ni({"name": "храст", "cost": 1})
71function_that_says_ni(aabcc={"name": "bush", "cost": 3.20})71function_that_says_ni(aabcc={"name": "bush", "cost": 3.20})
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op