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

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

6 точки общо

6 успешни теста
4 неуспешни теста
Код

 1def function_that_says_ni(*args, **kwargs):
 2   if not there_is_a_shrub(*args, **kwargs):
 3       return "Ni!"
 4   elif not is_one_that_looks_nice(*args, **kwargs):
 5       return "Ni!"
 6   else:
 7       total_cost = get_cost_from_args(*args,**kwargs)
 8       return f"{total_cost:.2f}лв"
 9        
10
11def there_is_a_shrub(*args, **kwargs):
12    for curr_name in args:
13        if "name" in curr_name and is_shrub(curr_name["name"]):
14            return True
15    for curr_dict in kwargs.values():
16        if "name" in curr_dict and is_shrub(curr_dict["name"]):
17            return True
18   
19    return False
20
21def get_cost_from_args(*args, **kwargs):
22     total = 0
23     for current_dict in *args, *kwargs.values():
24        if not isDict(current_dict):
25            continue
26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):
27             total += float(current_dict["cost"])
28     return total
29   
30def is_one_that_looks_nice(*args, **kwargs):
31    total_cost = float(get_cost_from_args(*args, **kwargs))
32    if is_too_expensive(total_cost):
33        return False
34    diff_letters = get_diff_letters_from_named_args(**kwargs)
35    if not price_by_letters_check(diff_letters, total_cost):
36        return False
37    return True
38
39def price_by_letters_check(diff_letters, price):
40     integer = int(price)
41     if price == 0.00 and diff_letters > 0:
42        return False
43     if  diff_letters == 0 or integer % diff_letters == 0:
44         return True
45     return False
46
47def get_diff_letters_from_named_args(**kwargs):
48    diff_letters = []
49    for current_string in kwargs:
50        curr_lower = current_string.lower()
51        for current_letter in curr_lower:
52            if current_letter not in diff_letters:
53                diff_letters.append(current_letter)
54
55    return len(diff_letters)
56
57def is_too_expensive(price):
58    return price > 42
59
60def is_shrub(name):
61    name_lower = name.lower()
62    return name_lower in ("shrub", "храст", "bush")
63
64def isDict(argument):
65    return type(argument) == dict
66    

..F.F..E.F
======================================================================
ERROR: test_other_than_dicts (test.TestNi.test_other_than_dicts)
Test with inputs other than dicts.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 74, in test_other_than_dicts
self.assertEqual(function_that_says_ni(1, 3.14, ['some_list'], some_arg={1, 2, 3}), self.NI)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 2, in function_that_says_ni
if not there_is_a_shrub(*args, **kwargs):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 13, in there_is_a_shrub
if "name" in curr_name and is_shrub(curr_name["name"]):
^^^^^^^^^^^^^^^^^^^
TypeError: argument of type 'int' is not iterable

======================================================================
FAIL: 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},
AssertionError: '0.30лв' != 'Ni!'
- 0.30лв
+ Ni!

======================================================================
FAIL: test_invalid_strings (test.TestNi.test_invalid_strings)
Test with invalid strings that might be misinterpreted.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 69, in test_invalid_strings
self.assertEqual(function_that_says_ni({'name': 'shrub', ' cost': 1}), self.NI) # Space before cost
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: '0.00лв' != 'Ni!'
- 0.00лв
+ Ni!

======================================================================
FAIL: test_with_no_cost (test.TestNi.test_with_no_cost)
Test with a shrub without defined cost.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 30, in test_with_no_cost
self.assertEqual(function_that_says_ni({'name': 'shrub'}), self.NI)
AssertionError: '0.00лв' != 'Ni!'
- 0.00лв
+ Ni!

----------------------------------------------------------------------
Ran 10 tests in 0.002s

FAILED (failures=3, errors=1)

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

t1def function_that_says_ni(*args, **kwargs):t1def function_that_says_ni(*args, **kwargs):
2   if not there_is_a_shrub(*args, **kwargs):2   if not there_is_a_shrub(*args, **kwargs):
3       return "Ni!"3       return "Ni!"
4   elif not is_one_that_looks_nice(*args, **kwargs):4   elif not is_one_that_looks_nice(*args, **kwargs):
5       return "Ni!"5       return "Ni!"
6   else:6   else:
7       total_cost = get_cost_from_args(*args,**kwargs)7       total_cost = get_cost_from_args(*args,**kwargs)
8       return f"{total_cost:.2f}лв"8       return f"{total_cost:.2f}лв"
9        9        
1010
11def there_is_a_shrub(*args, **kwargs):11def there_is_a_shrub(*args, **kwargs):
12    for curr_name in args:12    for curr_name in args:
13        if "name" in curr_name and is_shrub(curr_name["name"]):13        if "name" in curr_name and is_shrub(curr_name["name"]):
14            return True14            return True
15    for curr_dict in kwargs.values():15    for curr_dict in kwargs.values():
16        if "name" in curr_dict and is_shrub(curr_dict["name"]):16        if "name" in curr_dict and is_shrub(curr_dict["name"]):
17            return True17            return True
18   18   
19    return False19    return False
2020
21def get_cost_from_args(*args, **kwargs):21def get_cost_from_args(*args, **kwargs):
22     total = 022     total = 0
23     for current_dict in *args, *kwargs.values():23     for current_dict in *args, *kwargs.values():
24        if not isDict(current_dict):24        if not isDict(current_dict):
25            continue25            continue
26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):
27             total += float(current_dict["cost"])27             total += float(current_dict["cost"])
28     return total28     return total
29   29   
30def is_one_that_looks_nice(*args, **kwargs):30def is_one_that_looks_nice(*args, **kwargs):
31    total_cost = float(get_cost_from_args(*args, **kwargs))31    total_cost = float(get_cost_from_args(*args, **kwargs))
32    if is_too_expensive(total_cost):32    if is_too_expensive(total_cost):
33        return False33        return False
34    diff_letters = get_diff_letters_from_named_args(**kwargs)34    diff_letters = get_diff_letters_from_named_args(**kwargs)
35    if not price_by_letters_check(diff_letters, total_cost):35    if not price_by_letters_check(diff_letters, total_cost):
36        return False36        return False
37    return True37    return True
3838
39def price_by_letters_check(diff_letters, price):39def price_by_letters_check(diff_letters, price):
40     integer = int(price)40     integer = int(price)
41     if price == 0.00 and diff_letters > 0:41     if price == 0.00 and diff_letters > 0:
42        return False42        return False
43     if  diff_letters == 0 or integer % diff_letters == 0:43     if  diff_letters == 0 or integer % diff_letters == 0:
44         return True44         return True
45     return False45     return False
4646
47def get_diff_letters_from_named_args(**kwargs):47def get_diff_letters_from_named_args(**kwargs):
48    diff_letters = []48    diff_letters = []
49    for current_string in kwargs:49    for current_string in kwargs:
50        curr_lower = current_string.lower()50        curr_lower = current_string.lower()
51        for current_letter in curr_lower:51        for current_letter in curr_lower:
52            if current_letter not in diff_letters:52            if current_letter not in diff_letters:
53                diff_letters.append(current_letter)53                diff_letters.append(current_letter)
5454
55    return len(diff_letters)55    return len(diff_letters)
5656
57def is_too_expensive(price):57def is_too_expensive(price):
58    return price > 4258    return price > 42
5959
60def is_shrub(name):60def is_shrub(name):
61    name_lower = name.lower()61    name_lower = name.lower()
62    return name_lower in ("shrub", "храст", "bush")62    return name_lower in ("shrub", "храст", "bush")
6363
64def isDict(argument):64def isDict(argument):
65    return type(argument) == dict65    return type(argument) == dict
66    66    
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1def function_that_says_ni(*args, **kwargs):t1def function_that_says_ni(*args, **kwargs):
2   if not there_is_a_shrub(*args, **kwargs):2   if not there_is_a_shrub(*args, **kwargs):
3       return "Ni!"3       return "Ni!"
4   elif not is_one_that_looks_nice(*args, **kwargs):4   elif not is_one_that_looks_nice(*args, **kwargs):
5       return "Ni!"5       return "Ni!"
6   else:6   else:
7       total_cost = get_cost_from_args(*args,**kwargs)7       total_cost = get_cost_from_args(*args,**kwargs)
8       return f"{total_cost:.2f}лв"8       return f"{total_cost:.2f}лв"
9        9        
1010
11def there_is_a_shrub(*args, **kwargs):11def there_is_a_shrub(*args, **kwargs):
12    for curr_name in args:12    for curr_name in args:
13        if "name" in curr_name and is_shrub(curr_name["name"]):13        if "name" in curr_name and is_shrub(curr_name["name"]):
14            return True14            return True
15    for curr_dict in kwargs.values():15    for curr_dict in kwargs.values():
16        if "name" in curr_dict and is_shrub(curr_dict["name"]):16        if "name" in curr_dict and is_shrub(curr_dict["name"]):
17            return True17            return True
18   18   
19    return False19    return False
2020
21def get_cost_from_args(*args, **kwargs):21def get_cost_from_args(*args, **kwargs):
22     total = 022     total = 0
23     for current_dict in *args, *kwargs.values():23     for current_dict in *args, *kwargs.values():
24        if not isDict(current_dict):24        if not isDict(current_dict):
25            continue25            continue
26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):
27             total += float(current_dict["cost"])27             total += float(current_dict["cost"])
28     return total28     return total
29   29   
30def is_one_that_looks_nice(*args, **kwargs):30def is_one_that_looks_nice(*args, **kwargs):
31    total_cost = float(get_cost_from_args(*args, **kwargs))31    total_cost = float(get_cost_from_args(*args, **kwargs))
32    if is_too_expensive(total_cost):32    if is_too_expensive(total_cost):
33        return False33        return False
34    diff_letters = get_diff_letters_from_named_args(**kwargs)34    diff_letters = get_diff_letters_from_named_args(**kwargs)
35    if not price_by_letters_check(diff_letters, total_cost):35    if not price_by_letters_check(diff_letters, total_cost):
36        return False36        return False
37    return True37    return True
3838
39def price_by_letters_check(diff_letters, price):39def price_by_letters_check(diff_letters, price):
40     integer = int(price)40     integer = int(price)
41     if price == 0.00 and diff_letters > 0:41     if price == 0.00 and diff_letters > 0:
42        return False42        return False
43     if  diff_letters == 0 or integer % diff_letters == 0:43     if  diff_letters == 0 or integer % diff_letters == 0:
44         return True44         return True
45     return False45     return False
4646
47def get_diff_letters_from_named_args(**kwargs):47def get_diff_letters_from_named_args(**kwargs):
48    diff_letters = []48    diff_letters = []
49    for current_string in kwargs:49    for current_string in kwargs:
50        curr_lower = current_string.lower()50        curr_lower = current_string.lower()
51        for current_letter in curr_lower:51        for current_letter in curr_lower:
52            if current_letter not in diff_letters:52            if current_letter not in diff_letters:
53                diff_letters.append(current_letter)53                diff_letters.append(current_letter)
5454
55    return len(diff_letters)55    return len(diff_letters)
5656
57def is_too_expensive(price):57def is_too_expensive(price):
58    return price > 4258    return price > 42
5959
60def is_shrub(name):60def is_shrub(name):
61    name_lower = name.lower()61    name_lower = name.lower()
62    return name_lower in ("shrub", "храст", "bush")62    return name_lower in ("shrub", "храст", "bush")
6363
64def isDict(argument):64def isDict(argument):
65    return type(argument) == dict65    return type(argument) == dict
66    66    
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):
2   if not there_is_a_shrub(*args, **kwargs):2   if not there_is_a_shrub(*args, **kwargs):
3       return "Ni!"3       return "Ni!"
4   elif not is_one_that_looks_nice(*args, **kwargs):4   elif not is_one_that_looks_nice(*args, **kwargs):
5       return "Ni!"5       return "Ni!"
6   else:6   else:
7       total_cost = get_cost_from_args(*args,**kwargs)7       total_cost = get_cost_from_args(*args,**kwargs)
8       return f"{total_cost:.2f}лв"8       return f"{total_cost:.2f}лв"
9        9        
1010
11def there_is_a_shrub(*args, **kwargs):11def there_is_a_shrub(*args, **kwargs):
12    for curr_name in args:12    for curr_name in args:
13        if "name" in curr_name and is_shrub(curr_name["name"]):13        if "name" in curr_name and is_shrub(curr_name["name"]):
14            return True14            return True
15    for curr_dict in kwargs.values():15    for curr_dict in kwargs.values():
16        if "name" in curr_dict and is_shrub(curr_dict["name"]):16        if "name" in curr_dict and is_shrub(curr_dict["name"]):
17            return True17            return True
18   18   
19    return False19    return False
2020
21def get_cost_from_args(*args, **kwargs):21def get_cost_from_args(*args, **kwargs):
22     total = 022     total = 0
23     for current_dict in *args, *kwargs.values():23     for current_dict in *args, *kwargs.values():
24        if not isDict(current_dict):24        if not isDict(current_dict):
25            continue25            continue
26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):
27             total += float(current_dict["cost"])27             total += float(current_dict["cost"])
28     return total28     return total
29   29   
30def is_one_that_looks_nice(*args, **kwargs):30def is_one_that_looks_nice(*args, **kwargs):
31    total_cost = float(get_cost_from_args(*args, **kwargs))31    total_cost = float(get_cost_from_args(*args, **kwargs))
32    if is_too_expensive(total_cost):32    if is_too_expensive(total_cost):
33        return False33        return False
34    diff_letters = get_diff_letters_from_named_args(**kwargs)34    diff_letters = get_diff_letters_from_named_args(**kwargs)
35    if not price_by_letters_check(diff_letters, total_cost):35    if not price_by_letters_check(diff_letters, total_cost):
36        return False36        return False
37    return True37    return True
3838
39def price_by_letters_check(diff_letters, price):39def price_by_letters_check(diff_letters, price):
40     integer = int(price)40     integer = int(price)
41     if price == 0.00 and diff_letters > 0:41     if price == 0.00 and diff_letters > 0:
42        return False42        return False
43     if  diff_letters == 0 or integer % diff_letters == 0:43     if  diff_letters == 0 or integer % diff_letters == 0:
44         return True44         return True
45     return False45     return False
4646
47def get_diff_letters_from_named_args(**kwargs):47def get_diff_letters_from_named_args(**kwargs):
48    diff_letters = []48    diff_letters = []
49    for current_string in kwargs:49    for current_string in kwargs:
50        curr_lower = current_string.lower()50        curr_lower = current_string.lower()
51        for current_letter in curr_lower:51        for current_letter in curr_lower:
52            if current_letter not in diff_letters:52            if current_letter not in diff_letters:
53                diff_letters.append(current_letter)53                diff_letters.append(current_letter)
5454
55    return len(diff_letters)55    return len(diff_letters)
5656
57def is_too_expensive(price):57def is_too_expensive(price):
58    return price > 4258    return price > 42
5959
60def is_shrub(name):60def is_shrub(name):
61    name_lower = name.lower()61    name_lower = name.lower()
62    return name_lower in ("shrub", "храст", "bush")62    return name_lower in ("shrub", "храст", "bush")
6363
64def isDict(argument):64def isDict(argument):
65    return type(argument) == dict65    return type(argument) == dict
66    66    
t67print(function_that_says_ni({"name": "храст", "cost": 1}))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):
2   if not there_is_a_shrub(*args, **kwargs):2   if not there_is_a_shrub(*args, **kwargs):
n3       return "Ni"n3       return "Ni!"
4   elif not is_one_that_looks_nice(*args, **kwargs):4   elif not is_one_that_looks_nice(*args, **kwargs):
n5       return "Ni"n5       return "Ni!"
6   else:6   else:
7       total_cost = get_cost_from_args(*args,**kwargs)7       total_cost = get_cost_from_args(*args,**kwargs)
8       return f"{total_cost:.2f}лв"8       return f"{total_cost:.2f}лв"
9        9        
1010
11def there_is_a_shrub(*args, **kwargs):11def there_is_a_shrub(*args, **kwargs):
n12    inKwargs = Falsen
13    inArgs =False
14    for curr_name in args:12    for curr_name in args:
15        if "name" in curr_name and is_shrub(curr_name["name"]):13        if "name" in curr_name and is_shrub(curr_name["name"]):
n16            inArgs = Truen14            return True
17    for curr_dict in kwargs.values():15    for curr_dict in kwargs.values():
18        if "name" in curr_dict and is_shrub(curr_dict["name"]):16        if "name" in curr_dict and is_shrub(curr_dict["name"]):
n19            inKwargs = Truen
20    if inArgs or inKwargs:
21        return True17            return True
18   
22    return False19    return False
2320
24def get_cost_from_args(*args, **kwargs):21def get_cost_from_args(*args, **kwargs):
25     total = 022     total = 0
n26     for current_dict in args:n23     for current_dict in *args, *kwargs.values():
27        if not isDict(current_dict):24        if not isDict(current_dict):
28            continue25            continue
29        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):26        if "cost" in current_dict and current_dict["cost"] and is_shrub(current_dict["name"]):
n30             total+= float(current_dict["cost"])n27             total += float(current_dict["cost"])
31     for named_dict in kwargs.values():
32        if not isDict(named_dict):
33            continue
34        if "cost" in named_dict and named_dict["cost"] and is_shrub(named_dict["name"]):
35             total+= float(named_dict["cost"])
36     return total28     return total
37   29   
38def is_one_that_looks_nice(*args, **kwargs):30def is_one_that_looks_nice(*args, **kwargs):
39    total_cost = float(get_cost_from_args(*args, **kwargs))31    total_cost = float(get_cost_from_args(*args, **kwargs))
40    if is_too_expensive(total_cost):32    if is_too_expensive(total_cost):
41        return False33        return False
42    diff_letters = get_diff_letters_from_named_args(**kwargs)34    diff_letters = get_diff_letters_from_named_args(**kwargs)
n43    if not price_by_letters_check(diff_letters,total_cost):n35    if not price_by_letters_check(diff_letters, total_cost):
44        return False36        return False
45    return True37    return True
4638
n47def price_by_letters_check(diff_letters,price):n39def price_by_letters_check(diff_letters, price):
48     integer= int(price)40     integer = int(price)
49     if diff_letters == 0:
50         return True
51     if price == 0.00 and diff_letters>0:41     if price == 0.00 and diff_letters > 0:
52        return False42        return False
n53     if integer % diff_letters == 0:n43     if  diff_letters == 0 or integer % diff_letters == 0:
54         return True44         return True
55     return False45     return False
5646
57def get_diff_letters_from_named_args(**kwargs):47def get_diff_letters_from_named_args(**kwargs):
58    diff_letters = []48    diff_letters = []
59    for current_string in kwargs:49    for current_string in kwargs:
60        curr_lower = current_string.lower()50        curr_lower = current_string.lower()
61        for current_letter in curr_lower:51        for current_letter in curr_lower:
62            if current_letter not in diff_letters:52            if current_letter not in diff_letters:
63                diff_letters.append(current_letter)53                diff_letters.append(current_letter)
6454
65    return len(diff_letters)55    return len(diff_letters)
6656
67def is_too_expensive(price):57def is_too_expensive(price):
n68    if price>42.00:n58    return price > 42
69        return True
70    return False
7159
72def is_shrub(name):60def is_shrub(name):
n73    name_lower = str(name).lower()n61    name_lower = name.lower()
74    if name_lower == "shrub" or name_lower == "bush" or name_lower == "храст":62    return name_lower in ("shrub", "храст", "bush")
75        return True
76    return False
7763
78def isDict(argument):64def isDict(argument):
79    return type(argument) == dict65    return type(argument) == dict
80    66    
tt67print(function_that_says_ni({"name": "храст", "cost": 1}))
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op