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

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

6 точки общо

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

 1def get_unique_letters_count(used_keywords):
 2    unique_letters = set()
 3    for keyword in used_keywords:
 4        unique_letters.update(keyword)
 5    
 6    return len(unique_letters)
 7
 8def get_only_dicts(all_args):
 9    dicts = []
10    for arg in all_args:
11        if isinstance(arg, dict):
12            dicts.append(arg)
13    
14    return dicts
15
16def get_only_valid_brushes(all_args):
17    valid_bushes = []
18
19    dicts = get_only_dicts(all_args)
20    valid_names = ("храст", "shrub", "bush")
21
22    for d in dicts:
23        if "name" in d and isinstance(d["name"], str) and d["name"].lower() in valid_names:
24            valid_bushes.append(d)
25
26    return valid_bushes
27
28def get_total_cost(bushes):
29    result = 0
30
31    for bush in bushes:
32        if "cost" in bush and isinstance(bush["cost"], (float, int)):
33            result += bush["cost"]
34    
35    return result
36
37def function_that_says_ni(*args, **kwargs):
38    used_keywords = list(kwargs.keys())
39    count_of_unique_letters = get_unique_letters_count(used_keywords)
40    
41    all_args = list(args)
42    all_args.extend(kwargs.values())
43    
44    bushes = get_only_valid_brushes(all_args)
45
46    if not bushes:
47        return "Ni!"
48
49    total_cost = get_total_cost(bushes)
50
51    if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
52        return f"{total_cost:.2f}лв"
53
54    return "Ni!"

..E.EF...E
======================================================================
ERROR: 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},
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

======================================================================
ERROR: 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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

======================================================================
ERROR: 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)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/solution.py", line 51, in function_that_says_ni
if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
ZeroDivisionError: integer modulo by zero

======================================================================
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.002s

FAILED (failures=1, errors=3)

Дискусия
Николай Радилов
17.10.2024 15:21

Мерси
Виктор Бечев
17.10.2024 15:06

Можеш да качваш колкото решиш версии на домашните - независимо дали е заради корекции спрямо наш фийдбек или ти си се сетил за нещо, което можеш да направиш по-добре (или си си намерил проблем в решението и т.н.).
Николай Радилов
17.10.2024 14:49

След като съм си оправил грешките от коментарите, мога ли да го кача наново?
История

f1def get_unique_letters_count(used_keywords):f1def get_unique_letters_count(used_keywords):
2    unique_letters = set()2    unique_letters = set()
3    for keyword in used_keywords:3    for keyword in used_keywords:
n4        for letter in keyword:n4        unique_letters.update(keyword)
5            unique_letters.add(letter)
6    5    
7    return len(unique_letters)6    return len(unique_letters)
87
9def get_only_dicts(all_args):8def get_only_dicts(all_args):
10    dicts = []9    dicts = []
11    for arg in all_args:10    for arg in all_args:
n12        if type(arg) is dict:n11        if isinstance(arg, dict):
13            dicts.append(arg)12            dicts.append(arg)
14    13    
15    return dicts14    return dicts
1615
17def get_only_valid_brushes(all_args):16def get_only_valid_brushes(all_args):
18    valid_bushes = []17    valid_bushes = []
1918
20    dicts = get_only_dicts(all_args)19    dicts = get_only_dicts(all_args)
21    valid_names = ("храст", "shrub", "bush")20    valid_names = ("храст", "shrub", "bush")
2221
n23    for dict in dicts:n22    for d in dicts:
24        if "name" in dict and type(dict["name"]) is str and dict["name"].lower() in valid_names:23        if "name" in d and isinstance(d["name"], str) and d["name"].lower() in valid_names:
25            valid_bushes.append(dict)24            valid_bushes.append(d)
2625
27    return valid_bushes26    return valid_bushes
2827
29def get_total_cost(bushes):28def get_total_cost(bushes):
30    result = 029    result = 0
3130
32    for bush in bushes:31    for bush in bushes:
t33        if "cost" in bush and (type(bush["cost"]) is int or type(bush["cost"]) is float):t32        if "cost" in bush and isinstance(bush["cost"], (float, int)):
34            result += bush["cost"]33            result += bush["cost"]
35    34    
36    return result35    return result
3736
38def function_that_says_ni(*args, **kwargs):37def function_that_says_ni(*args, **kwargs):
39    used_keywords = list(kwargs.keys())38    used_keywords = list(kwargs.keys())
40    count_of_unique_letters = get_unique_letters_count(used_keywords)39    count_of_unique_letters = get_unique_letters_count(used_keywords)
41    40    
42    all_args = list(args)41    all_args = list(args)
43    all_args.extend(kwargs.values())42    all_args.extend(kwargs.values())
44    43    
45    bushes = get_only_valid_brushes(all_args)44    bushes = get_only_valid_brushes(all_args)
4645
47    if not bushes:46    if not bushes:
48        return "Ni!"47        return "Ni!"
4948
50    total_cost = get_total_cost(bushes)49    total_cost = get_total_cost(bushes)
5150
52    if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:51    if total_cost < 42 and count_of_unique_letters % int(total_cost) == 0:
53        return f"{total_cost:.2f}лв"52        return f"{total_cost:.2f}лв"
5453
55    return "Ni!"54    return "Ni!"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op