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

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

9 точки общо

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

 1def function_that_says_ni(*args, **kwargs):
 2    costs = 0
 3    unique_letters = set()
 4
 5    for el in args:
 6        if type(el) == dict:
 7            for key, value in el.items():
 8                if key == 'name':
 9                    if value.lower() not in ('храст', 'shrub', 'bush'):
10                        break
11                if key == 'cost':
12                    costs += round(value, 2)
13    
14    for key, value in kwargs.items():
15        if type(value) == dict:
16            for inner_key, inner_value in value.items():
17                if inner_key == 'name':
18                    if inner_value.lower() not in ('храст', 'shrub', 'bush'):
19                        break
20                if inner_key == 'cost':
21                    costs += round(inner_value, 2)
22            for letter in key:
23                if letter not in unique_letters:
24                    unique_letters.add(letter)
25
26    letters_count = len(unique_letters)    
27
28    if costs <= 42.00 and not int(costs) == 0:
29        if not letters_count % int(costs):
30            return '{:.2f}'.format(costs) + 'лв'
31
32    return 'Ni!'

....F.....
======================================================================
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 67, in test_invalid_strings
self.assertEqual(function_that_says_ni({'no_name': 'bush', 'cost': 1}), self.NI)
AssertionError: '1.00лв' != 'Ni!'
- 1.00лв
+ Ni!

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

FAILED (failures=1)

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

n1def  function_that_says_ni(*args, **kwargs):n1def function_that_says_ni(*args, **kwargs):
2    sum = 0.002    costs = 0
3    unique_letters = set()3    unique_letters = set()
44
5    for el in args:5    for el in args:
6        if type(el) == dict:6        if type(el) == dict:
7            for key, value in el.items():7            for key, value in el.items():
8                if key == 'name':8                if key == 'name':
n9                    if value.lower() == 'храст' or value.lower() == 'shrub' or value.lower() =='bush':n9                    if value.lower() not in ('храст', 'shrub', 'bush'):
10                        continue
11                    else:
12                        break10                        break
13                if key == 'cost':11                if key == 'cost':
n14                    sum += round(value,2)n12                    costs += round(value, 2)
15    13    
16    for key, value in kwargs.items():14    for key, value in kwargs.items():
17        if type(value) == dict:15        if type(value) == dict:
18            for inner_key, inner_value in value.items():16            for inner_key, inner_value in value.items():
19                if inner_key == 'name':17                if inner_key == 'name':
n20                    if inner_value.lower() == 'храст' or inner_value.lower() == 'shrub' or inner_value.lower() =='bush':n18                    if inner_value.lower() not in ('храст', 'shrub', 'bush'):
21                        continue
22                    else:
23                        break19                        break
24                if inner_key == 'cost':20                if inner_key == 'cost':
n25                    sum += round(inner_value,2)n21                    costs += round(inner_value, 2)
26            for letter in key:22            for letter in key:
27                if letter not in unique_letters:23                if letter not in unique_letters:
28                    unique_letters.add(letter)24                    unique_letters.add(letter)
2925
n30    int_sum = int(sum)n
31    letters_count = len(unique_letters)    26    letters_count = len(unique_letters)    
3227
n33    if sum <= 42.00 and not int_sum == 0:n28    if costs <= 42.00 and not int(costs) == 0:
34        if not letters_count % int_sum:29        if not letters_count % int(costs):
35            return str(round(sum,2)) + 'лв'30            return '{:.2f}'.format(costs) + 'лв'
3631
37    return 'Ni!'32    return 'Ni!'
t38 t
39    
40 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op