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

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

8 точки общо

8 успешни теста
2 неуспешни теста
Код
Скрий всички коментари

 1#checks if the shrub is valid
 2def is_valid(shrub):
 3        if "name" in shrub:
 4            name_val = shrub["name"].lower()
 5            if name_val in ["храст", "shrub", "bush"]:
 6                return True
 7        return False
 8 
 9#checks if the total cost is bigger than 42    
10def total_cost_check(total_cost):
11      return total_cost > 42
12
13#checks if the bush is nice
14def nice(unique_letters_count, total_cost):
15      if unique_letters_count == 0:
16         return False
17      return unique_letters_count % int(total_cost) == 0
18
19#checks if the total cost is zero
20def is_zero(total_cost):
21      return int(total_cost) == 0
22
23
24def function_that_says_ni(*args, **kwargs):
25    # empty list to store valid shrubs
26    shrubs = []
27
28    # empty set to keep track of unique letters
29    unique_letters = set()
30
31    # total cost of shrubs
32    total_cost = 0.0
33
34    # check if it is valid shrub
35    
36
37    for arg in args:
38        if isinstance(arg, dict):
39            if is_valid(arg):
40                shrubs.append(arg)
41                total_cost += arg.get("cost", 0)
42
43    for key, value in kwargs.items():
44        if isinstance(value, dict):
45            if is_valid(value):
46                shrubs.append(value)
47                total_cost += value.get("cost", 0)
48                unique_letters.add(key)
49
50    if total_cost_check(total_cost):
51        return "Ni!"
52
53    unique_letters_count = len(unique_letters)
54
55    if is_zero(total_cost):
56      return "Ni!"
57    
58    if nice(unique_letters_count, total_cost):
59        return "Ni!"
60
61    f_cost = f"{total_cost:.2f}лв"
62    return f_cost

.F....F...
======================================================================
FAIL: test_combination_of_arguments (test.TestNi.test_combination_of_arguments)
Test with combination of named and positional arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 60, in test_combination_of_arguments
self.assertEqual(function_that_says_ni({'name': 'bush', 'cost': 1},
AssertionError: 'Ni!' != '2.00лв'
- Ni!
+ 2.00лв

======================================================================
FAIL: test_named_arguments (test.TestNi.test_named_arguments)
Test with named arguments.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 55, in test_named_arguments
self.assertEqual(function_that_says_ni(a={'name': 'shrub', 'cost': 1},
AssertionError: '3.00лв' != 'Ni!'
- 3.00лв
+ Ni!

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

FAILED (failures=2)

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

f1#checks if the shrub is validf1#checks if the shrub is valid
2def is_valid(shrub):2def is_valid(shrub):
3        if "name" in shrub:3        if "name" in shrub:
4            name_val = shrub["name"].lower()4            name_val = shrub["name"].lower()
5            if name_val in ["храст", "shrub", "bush"]:5            if name_val in ["храст", "shrub", "bush"]:
6                return True6                return True
7        return False7        return False
8 8 
9#checks if the total cost is bigger than 42    9#checks if the total cost is bigger than 42    
10def total_cost_check(total_cost):10def total_cost_check(total_cost):
11      return total_cost > 4211      return total_cost > 42
1212
13#checks if the bush is nice13#checks if the bush is nice
14def nice(unique_letters_count, total_cost):14def nice(unique_letters_count, total_cost):
15      if unique_letters_count == 0:15      if unique_letters_count == 0:
16         return False16         return False
17      return unique_letters_count % int(total_cost) == 017      return unique_letters_count % int(total_cost) == 0
1818
19#checks if the total cost is zero19#checks if the total cost is zero
20def is_zero(total_cost):20def is_zero(total_cost):
21      return int(total_cost) == 021      return int(total_cost) == 0
2222
2323
24def function_that_says_ni(*args, **kwargs):24def function_that_says_ni(*args, **kwargs):
25    # empty list to store valid shrubs25    # empty list to store valid shrubs
26    shrubs = []26    shrubs = []
2727
28    # empty set to keep track of unique letters28    # empty set to keep track of unique letters
29    unique_letters = set()29    unique_letters = set()
3030
31    # total cost of shrubs31    # total cost of shrubs
32    total_cost = 0.032    total_cost = 0.0
3333
34    # check if it is valid shrub34    # check if it is valid shrub
35    35    
3636
37    for arg in args:37    for arg in args:
38        if isinstance(arg, dict):38        if isinstance(arg, dict):
39            if is_valid(arg):39            if is_valid(arg):
40                shrubs.append(arg)40                shrubs.append(arg)
41                total_cost += arg.get("cost", 0)41                total_cost += arg.get("cost", 0)
4242
43    for key, value in kwargs.items():43    for key, value in kwargs.items():
44        if isinstance(value, dict):44        if isinstance(value, dict):
45            if is_valid(value):45            if is_valid(value):
46                shrubs.append(value)46                shrubs.append(value)
47                total_cost += value.get("cost", 0)47                total_cost += value.get("cost", 0)
48                unique_letters.add(key)48                unique_letters.add(key)
4949
50    if total_cost_check(total_cost):50    if total_cost_check(total_cost):
51        return "Ni!"51        return "Ni!"
5252
53    unique_letters_count = len(unique_letters)53    unique_letters_count = len(unique_letters)
5454
55    if is_zero(total_cost):55    if is_zero(total_cost):
56      return "Ni!"56      return "Ni!"
57    57    
58    if nice(unique_letters_count, total_cost):58    if nice(unique_letters_count, total_cost):
59        return "Ni!"59        return "Ni!"
6060
61    f_cost = f"{total_cost:.2f}лв"61    f_cost = f"{total_cost:.2f}лв"
62    return f_cost62    return f_cost
6363
6464
t65print(function_that_says_ni(c={"name": "ffff", "cost": 100}, a = {"name": "sHrub", "cost": 3}, b={"name": "buSH", "cost": 4.50}))t
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op