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

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

9 точки общо

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

 1def is_good_named_shrub(shrub):
 2    if 'name' in map(str.lower, shrub.keys()):
 3        name_value = shrub.get('name')
 4        if name_value.lower() in ['bush', 'shrub', 'храст']:
 5            return True
 6        return False
 7    return False
 8
 9
10def function_that_says_ni(*args, **kwargs):
11    costs = 0
12    unique_letters = set()
13    have_bush = False
14
15    for key, value in kwargs.items():
16        if type(value) == dict and is_good_named_shrub(value):
17            unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_'))
18            have_bush = True
19            if 'cost' in value:
20                costs += value.get('cost')
21
22    unique_letters_count = len(unique_letters)
23
24    for element in args:
25        if type(element) == dict and is_good_named_shrub(element):
26            have_bush = True
27            if 'cost' in element:
28                costs += element.get('cost')
29
30    is_one_that_looks_nice = 0
31    if int(costs) != 0:
32        is_one_that_looks_nice = unique_letters_count % int(costs) == 0
33
34    if have_bush and costs < 42.00 and is_one_that_looks_nice:
35        return f"{costs:.2f}лв"
36    else:
37        return 'Ni!'

.....F....
======================================================================
FAIL: test_multiple_shrubs_sumс (test.TestNi.test_multiple_shrubs_sumс)
Test with a multiple shrubs and cornercase costs.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 34, in test_multiple_shrubs_sumс
self.assertEqual(function_that_says_ni({'name': 'shrub', 'cost': 20.00},
AssertionError: 'Ni!' != '42.00лв'
- Ni!
+ 42.00лв

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

FAILED (failures=1)

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

f1def is_good_named_shrub(shrub):f1def is_good_named_shrub(shrub):
2    if 'name' in map(str.lower, shrub.keys()):2    if 'name' in map(str.lower, shrub.keys()):
3        name_value = shrub.get('name')3        name_value = shrub.get('name')
4        if name_value.lower() in ['bush', 'shrub', 'храст']:4        if name_value.lower() in ['bush', 'shrub', 'храст']:
5            return True5            return True
6        return False6        return False
7    return False7    return False
88
99
10def function_that_says_ni(*args, **kwargs):10def function_that_says_ni(*args, **kwargs):
11    costs = 011    costs = 0
12    unique_letters = set()12    unique_letters = set()
13    have_bush = False13    have_bush = False
1414
15    for key, value in kwargs.items():15    for key, value in kwargs.items():
16        if type(value) == dict and is_good_named_shrub(value):16        if type(value) == dict and is_good_named_shrub(value):
17            unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_'))17            unique_letters.update(char.lower() for char in key if (char.isalpha() or char == '_'))
18            have_bush = True18            have_bush = True
19            if 'cost' in value:19            if 'cost' in value:
20                costs += value.get('cost')20                costs += value.get('cost')
2121
22    unique_letters_count = len(unique_letters)22    unique_letters_count = len(unique_letters)
2323
24    for element in args:24    for element in args:
25        if type(element) == dict and is_good_named_shrub(element):25        if type(element) == dict and is_good_named_shrub(element):
26            have_bush = True26            have_bush = True
27            if 'cost' in element:27            if 'cost' in element:
28                costs += element.get('cost')28                costs += element.get('cost')
2929
30    is_one_that_looks_nice = 030    is_one_that_looks_nice = 0
31    if int(costs) != 0:31    if int(costs) != 0:
32        is_one_that_looks_nice = unique_letters_count % int(costs) == 032        is_one_that_looks_nice = unique_letters_count % int(costs) == 0
3333
34    if have_bush and costs < 42.00 and is_one_that_looks_nice:34    if have_bush and costs < 42.00 and is_one_that_looks_nice:
n35        return f"{costs: .2f} лв"n35        return f"{costs:.2f}лв"
36    else:36    else:
37        return 'Ni!'37        return 'Ni!'
t38 t
39 
40print(function_that_says_ni({"name": "Храст", "cost": 11.0}, ad={"name": "дърво", "cost": 1.80}, c = "pypy"))
41 
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op