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

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

10 точки общо

10 успешни теста
0 неуспешни теста
Код

 1def is_a_bush(potential_bush, total_cost):
 2    valid_names = ("храст", "bush", "shrub")
 3    
 4    name = potential_bush.get('name', '').lower()
 5
 6    # Checking if the name is valid
 7    for curr_name in valid_names:
 8        if curr_name.lower() == name:
 9            cost = potential_bush.get('cost', 0)
10
11            # Checking if the cost is valid - int or float
12            if isinstance(cost, (int, float)):
13                total_cost += cost
14                
15            return True, total_cost
16                
17    return False, total_cost
18                    
19def function_that_says_ni(*args, **kwargs):
20    total_cost = 0
21    unique_letters = set()
22
23    # Looping through all args and searching for a bush
24    for arg in args:
25        if isinstance(arg, dict):
26            _, total_cost = is_a_bush(arg, total_cost)
27
28    # Looping through all kwargs and searching for a bush
29    for var_name, kwarg in kwargs.items():
30        if isinstance(kwarg, dict):
31            is_bush, total_cost = is_a_bush(kwarg, total_cost)
32            if is_bush:
33                unique_letters.update(set(var_name))
34    
35    unique_symbols_count = len(unique_letters)
36
37    if (int(total_cost) != 0 and total_cost <= 42 and unique_symbols_count >= 0 
38       and unique_symbols_count % int(total_cost) == 0):
39        return f"{total_cost:.2f}лв"
40    else:
41        return "Ni!"

..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s

OK

Дискусия
Василена Станойска
21.10.2024 12:41

Здравейте, благодаря за обратната връзка. Взех предвид вашите насоки и прикачвам второ поправено решение.
История

f1def is_a_bush(potential_bush, total_cost):f1def is_a_bush(potential_bush, total_cost):
nn2    valid_names = ("храст", "bush", "shrub")
2    3    
n3    is_bush = Falsen
4    valid_names = ["храст", "bush", "shrub"]
5    
6    if( potential_bush.get('name')):
7        name = potential_bush.get('name').lower()4    name = potential_bush.get('name', '').lower()
85
n9        # Checking if the name is validn6    # Checking if the name is valid
10        for _name in valid_names:7    for curr_name in valid_names:
11            if(_name.lower() == name):8        if curr_name.lower() == name:
12                is_bush = True
13                cost = potential_bush.get('cost', 0)9            cost = potential_bush.get('cost', 0)
1410
n15                # Checking if the cost is valid - int or floatn11            # Checking if the cost is valid - int or float
16                if isinstance(cost, (int, float)):12            if isinstance(cost, (int, float)):
17                    total_cost += cost13                total_cost += cost
18                14                
nn15            return True, total_cost
16                
19    return is_bush, total_cost17    return False, total_cost
20                    18                    
21def function_that_says_ni(*args, **kwargs):19def function_that_says_ni(*args, **kwargs):
n22    n
23    total_cost = 020    total_cost = 0
n24    is_bush = Falsen
25    unique_letters = set()21    unique_letters = set()
2622
27    # Looping through all args and searching for a bush23    # Looping through all args and searching for a bush
28    for arg in args:24    for arg in args:
29        if isinstance(arg, dict):25        if isinstance(arg, dict):
n30            is_bush, total_cost = is_a_bush(arg, total_cost)n26            _, total_cost = is_a_bush(arg, total_cost)
3127
32    # Looping through all kwargs and searching for a bush28    # Looping through all kwargs and searching for a bush
33    for var_name, kwarg in kwargs.items():29    for var_name, kwarg in kwargs.items():
34        if isinstance(kwarg, dict):30        if isinstance(kwarg, dict):
35            is_bush, total_cost = is_a_bush(kwarg, total_cost)31            is_bush, total_cost = is_a_bush(kwarg, total_cost)
n36            if(is_bush):n32            if is_bush:
37                unique_letters.update(set(var_name))33                unique_letters.update(set(var_name))
38    34    
39    unique_symbols_count = len(unique_letters)35    unique_symbols_count = len(unique_letters)
4036
n41    if(int(total_cost) != 0 and total_cost <= 42 and unique_symbols_count >= 0 n37    if (int(total_cost) != 0 and total_cost <= 42 and unique_symbols_count >= 0 
42       and unique_symbols_count % int(total_cost) == 0):38       and unique_symbols_count % int(total_cost) == 0):
t43        is_bush = Truet
44    else:
45        is_bush = False
46            
47    if(is_bush):
48        return f"{total_cost:.2f}лв"39        return f"{total_cost:.2f}лв"
49    else:40    else:
50        return "Ni!"41        return "Ni!"
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op