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

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

6 точки общо

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

 1NAMES = ["храст", "shrub", "bush"]
 2NI = "Ni!"
 3
 4
 5def function_that_says_ni(*args, **kwargs):
 6
 7    lst_valid_bushes = []
 8    st_unique_symbols_used = set()
 9    price = 0
10
11    for arg in args:
12        if "name" in arg and arg["name"].lower() in NAMES:
13            lst_valid_bushes.append(arg)
14
15            if "cost" in arg:
16                price += arg["cost"]
17
18    for kwarg in kwargs:
19        if "name" in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:
20            lst_valid_bushes.append(kwargs[kwarg])
21
22            if "cost" in kwargs[kwarg]:
23                price += kwargs[kwarg]["cost"]
24
25            for char in kwarg:
26                st_unique_symbols_used.add(char)
27
28    if len(lst_valid_bushes) == 0:
29        return NI
30
31    lenght = len(st_unique_symbols_used)
32    floor = int(price)
33
34    if lenght != 0:
35        rest = floor / lenght
36        if not rest.is_integer():
37            return NI
38
39    if price > 42.00:
40        return NI
41
42    return f"{price:.2f}лв"

..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 12, in function_that_says_ni
if "name" in arg and arg["name"].lower() in NAMES:
^^^^^^^^^^^^^
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.001s

FAILED (failures=3, errors=1)

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

n1import mathn
2 
3NAMES = ["храст", "shrub", "bush"]1NAMES = ["храст", "shrub", "bush"]
4NI = "Ni!"2NI = "Ni!"
53
64
7def function_that_says_ni(*args, **kwargs):5def function_that_says_ni(*args, **kwargs):
86
n9    lst = []n7    lst_valid_bushes = []
10    st = set()8    st_unique_symbols_used = set()
11    price = 09    price = 0
1210
13    for arg in args:11    for arg in args:
14        if "name" in arg and arg["name"].lower() in NAMES:12        if "name" in arg and arg["name"].lower() in NAMES:
n15            lst.append(arg)n13            lst_valid_bushes.append(arg)
1614
17            if "cost" in arg:15            if "cost" in arg:
18                price += arg["cost"]16                price += arg["cost"]
1917
20    for kwarg in kwargs:18    for kwarg in kwargs:
21        if "name" in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:19        if "name" in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:
n22            lst.append(kwargs[kwarg])n20            lst_valid_bushes.append(kwargs[kwarg])
2321
24            if "cost" in kwargs[kwarg]:22            if "cost" in kwargs[kwarg]:
25                price += kwargs[kwarg]["cost"]23                price += kwargs[kwarg]["cost"]
2624
27            for char in kwarg:25            for char in kwarg:
n28                st.add(char)n26                st_unique_symbols_used.add(char)
2927
n30    if len(lst) == 0:n28    if len(lst_valid_bushes) == 0:
31        return NI29        return NI
3230
n33    lenght = len(st)n31    lenght = len(st_unique_symbols_used)
34    floor = math.floor(price)32    floor = int(price)
3533
36    if lenght != 0:34    if lenght != 0:
37        rest = floor / lenght35        rest = floor / lenght
t38        if not (rest).is_integer():t36        if not rest.is_integer():
39            return NI37            return NI
4038
41    if price > 42.00:39    if price > 42.00:
42        return NI40        return NI
4341
44    return f"{price:.2f}лв"42    return f"{price:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
n3NAMES = ['храст''shrub''bush']n3NAMES = ["храст""shrub""bush"]
4NI = 'Ni!'4NI = "Ni!"
5 
56
6def function_that_says_ni(*args, **kwargs):7def function_that_says_ni(*args, **kwargs):
78
8    lst = []9    lst = []
9    st = set()10    st = set()
10    price = 011    price = 0
1112
12    for arg in args:13    for arg in args:
n13        if 'name' in arg and arg["name"].lower() in NAMES:n14        if "name" in arg and arg["name"].lower() in NAMES:
14            lst.append(arg)15            lst.append(arg)
1516
n16            if 'cost' in arg:n17            if "cost" in arg:
17                price += arg['cost']18                price += arg["cost"]
1819
19    for kwarg in kwargs:20    for kwarg in kwargs:
n20        if 'name' in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:n21        if "name" in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:
21            lst.append(kwargs[kwarg])22            lst.append(kwargs[kwarg])
2223
t23            if 'cost' in kwargs[kwarg]:t24            if "cost" in kwargs[kwarg]:
24                price += kwargs[kwarg]['cost']25                price += kwargs[kwarg]["cost"]
2526
26            for char in kwarg:27            for char in kwarg:
27                st.add(char)28                st.add(char)
2829
29    if len(lst) == 0:30    if len(lst) == 0:
30        return NI31        return NI
3132
32    lenght = len(st)33    lenght = len(st)
33    floor = math.floor(price)34    floor = math.floor(price)
3435
35    if lenght != 0:36    if lenght != 0:
36        rest = floor / lenght37        rest = floor / lenght
37        if not (rest).is_integer():38        if not (rest).is_integer():
38            return NI39            return NI
3940
40    if price > 42.00:41    if price > 42.00:
41        return NI42        return NI
4243
43    return f"{price:.2f}лв"44    return f"{price:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

t1import matht1import math
22
3NAMES = ['храст', 'shrub', 'bush']3NAMES = ['храст', 'shrub', 'bush']
4NI = 'Ni!'4NI = 'Ni!'
55
6def function_that_says_ni(*args, **kwargs):6def function_that_says_ni(*args, **kwargs):
77
8    lst = []8    lst = []
9    st = set()9    st = set()
10    price = 010    price = 0
1111
12    for arg in args:12    for arg in args:
13        if 'name' in arg and arg["name"].lower() in NAMES:13        if 'name' in arg and arg["name"].lower() in NAMES:
14            lst.append(arg)14            lst.append(arg)
1515
16            if 'cost' in arg:16            if 'cost' in arg:
17                price += arg['cost']17                price += arg['cost']
1818
19    for kwarg in kwargs:19    for kwarg in kwargs:
20        if 'name' in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:20        if 'name' in kwargs[kwarg] and kwargs[kwarg]["name"].lower() in NAMES:
21            lst.append(kwargs[kwarg])21            lst.append(kwargs[kwarg])
2222
23            if 'cost' in kwargs[kwarg]:23            if 'cost' in kwargs[kwarg]:
24                price += kwargs[kwarg]['cost']24                price += kwargs[kwarg]['cost']
2525
26            for char in kwarg:26            for char in kwarg:
27                st.add(char)27                st.add(char)
2828
29    if len(lst) == 0:29    if len(lst) == 0:
30        return NI30        return NI
3131
32    lenght = len(st)32    lenght = len(st)
33    floor = math.floor(price)33    floor = math.floor(price)
3434
35    if lenght != 0:35    if lenght != 0:
36        rest = floor / lenght36        rest = floor / lenght
37        if not (rest).is_integer():37        if not (rest).is_integer():
38            return NI38            return NI
3939
40    if price > 42.00:40    if price > 42.00:
41        return NI41        return NI
4242
43    return f"{price:.2f}лв"43    return f"{price:.2f}лв"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op