1def is_valid_value(val):
2 valid_bushes_values = ['храст', 'shrub', 'bush']
3 if type(val) is dict:
4 if val.get('name'):
5 if val['name'].lower() in valid_bushes_values:
6 return True
7 return False
8
9
10def function_that_says_ni(*args, **kwargs):
11 cost = 0
12 unique_letters_in_name = set()
13
14 for arg in args:
15 if is_valid_value(arg):
16 current_cost = arg.get('cost', 0)
17 cost += current_cost
18
19 for key, val in kwargs.items():
20 if is_valid_value(val):
21 current_cost = val.get('cost', 0)
22 cost += current_cost
23 for letter in key:
24 unique_letters_in_name.add(letter)
25
26 if cost > 42:
27 return 'Ni!'
28
29 whole_part_of_cost = int(cost)
30
31 unique_letters_in_name_count = len(unique_letters_in_name)
32
33 if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0:
34 return 'Ni!'
35
36 return f'{cost:.2f}лв'
..........
----------------------------------------------------------------------
Ran 10 tests in 0.000s
OK
Виктор Бечев
22.10.2024 15:54Валидно за да премести малко повторения, разбира се има и по-универсално решение, но това можеш да видиш след няколко часа.
|
Илиана Миладинова
21.10.2024 23:07Всъщност, това валидно решение ли е на проблема с двата идентични for-цикъла? Просто изнесох if-овете в отделна функция :)
|
Илиана Миладинова
21.10.2024 22:26Направих го без флагова променлива. Благодаря за обратната връзка!
|
Виктор Бечев
21.10.2024 19:10Отвъд горните забележки, единственото, което търпи подобрение е повторението на код (2-та почти идентични `for`-а).
Не го приемай като изискване, не те караме да рискуваш решението си на този етап, но ако ти е интересно как може да се спести - из решенията (включително нашите) ще видиш варианти.
|
| f | 1 | def is_valid_value(val): | f | 1 | def is_valid_value(val): |
| 2 | valid_bushes_values = ['храст', 'shrub', 'bush'] | 2 | valid_bushes_values = ['храст', 'shrub', 'bush'] | ||
| 3 | if type(val) is dict: | 3 | if type(val) is dict: | ||
| n | 4 | if val.get('name'): | n | 4 | if val.get('name'): |
| 5 | if val['name'].lower() in valid_bushes_values: | 5 | if val['name'].lower() in valid_bushes_values: | ||
| 6 | return True | 6 | return True | ||
| 7 | return False | 7 | return False | ||
| 8 | 8 | ||||
| 9 | 9 | ||||
| 10 | def function_that_says_ni(*args, **kwargs): | 10 | def function_that_says_ni(*args, **kwargs): | ||
| 11 | cost = 0 | 11 | cost = 0 | ||
| 12 | unique_letters_in_name = set() | 12 | unique_letters_in_name = set() | ||
| 13 | 13 | ||||
| 14 | for arg in args: | 14 | for arg in args: | ||
| t | 15 | if is_valid_value(arg): | t | 15 | if is_valid_value(arg): |
| 16 | current_cost = arg.get('cost', 0) | 16 | current_cost = arg.get('cost', 0) | ||
| 17 | cost += current_cost | 17 | cost += current_cost | ||
| 18 | 18 | ||||
| 19 | for key, val in kwargs.items(): | 19 | for key, val in kwargs.items(): | ||
| 20 | if is_valid_value(val): | 20 | if is_valid_value(val): | ||
| 21 | current_cost = val.get('cost', 0) | 21 | current_cost = val.get('cost', 0) | ||
| 22 | cost += current_cost | 22 | cost += current_cost | ||
| 23 | for letter in key: | 23 | for letter in key: | ||
| 24 | unique_letters_in_name.add(letter) | 24 | unique_letters_in_name.add(letter) | ||
| 25 | 25 | ||||
| 26 | if cost > 42: | 26 | if cost > 42: | ||
| 27 | return 'Ni!' | 27 | return 'Ni!' | ||
| 28 | 28 | ||||
| 29 | whole_part_of_cost = int(cost) | 29 | whole_part_of_cost = int(cost) | ||
| 30 | 30 | ||||
| 31 | unique_letters_in_name_count = len(unique_letters_in_name) | 31 | unique_letters_in_name_count = len(unique_letters_in_name) | ||
| 32 | 32 | ||||
| 33 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | 33 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | ||
| 34 | return 'Ni!' | 34 | return 'Ni!' | ||
| 35 | 35 | ||||
| 36 | return f'{cost:.2f}лв' | 36 | return f'{cost:.2f}лв' |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | n | 1 | def is_valid_value(val): | ||
| 2 | valid_bushes_values = ['храст', 'shrub', 'bush'] | ||||
| 3 | if type(val) is dict: | ||||
| 4 | if val.get('name'): | ||||
| 5 | if val['name'].lower() in valid_bushes_values: | ||||
| 6 | return True | ||||
| 7 | return False | ||||
| 8 | |||||
| 9 | |||||
| 1 | def function_that_says_ni(*args, **kwargs): | 10 | def function_that_says_ni(*args, **kwargs): | ||
| 2 | cost = 0 | 11 | cost = 0 | ||
| 3 | unique_letters_in_name = set() | 12 | unique_letters_in_name = set() | ||
| n | 4 | valid_bushes_values = ['храст', 'shrub', 'bush'] | n | ||
| 5 | 13 | ||||
| 6 | for arg in args: | 14 | for arg in args: | ||
| n | 7 | if type(arg) is dict: | n | 15 | if is_valid_value(arg): |
| 8 | if arg.get('name'): | ||||
| 9 | if arg['name'].lower() in valid_bushes_values: | ||||
| 10 | current_cost = arg.get('cost', 0) | 16 | current_cost = arg.get('cost', 0) | ||
| 11 | cost += current_cost | 17 | cost += current_cost | ||
| 12 | 18 | ||||
| 13 | for key, val in kwargs.items(): | 19 | for key, val in kwargs.items(): | ||
| t | 14 | if type(val) is dict: | t | 20 | if is_valid_value(val): |
| 15 | if val.get('name'): | ||||
| 16 | if val['name'].lower() in valid_bushes_values: | ||||
| 17 | current_cost = val.get('cost', 0) | 21 | current_cost = val.get('cost', 0) | ||
| 18 | cost += current_cost | 22 | cost += current_cost | ||
| 19 | for letter in key: | 23 | for letter in key: | ||
| 20 | unique_letters_in_name.add(letter) | 24 | unique_letters_in_name.add(letter) | ||
| 21 | 25 | ||||
| 22 | if cost > 42: | 26 | if cost > 42: | ||
| 23 | return 'Ni!' | 27 | return 'Ni!' | ||
| 24 | 28 | ||||
| 25 | whole_part_of_cost = int(cost) | 29 | whole_part_of_cost = int(cost) | ||
| 26 | 30 | ||||
| 27 | unique_letters_in_name_count = len(unique_letters_in_name) | 31 | unique_letters_in_name_count = len(unique_letters_in_name) | ||
| 28 | 32 | ||||
| 29 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | 33 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | ||
| 30 | return 'Ni!' | 34 | return 'Ni!' | ||
| 31 | 35 | ||||
| 32 | return f'{cost:.2f}лв' | 36 | return f'{cost:.2f}лв' |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
| n | 2 | is_valid = True | n | ||
| 3 | cost = 0 | 2 | cost = 0 | ||
| 4 | unique_letters_in_name = set() | 3 | unique_letters_in_name = set() | ||
| 5 | valid_bushes_values = ['храст', 'shrub', 'bush'] | 4 | valid_bushes_values = ['храст', 'shrub', 'bush'] | ||
| 6 | 5 | ||||
| 7 | for arg in args: | 6 | for arg in args: | ||
| 8 | if type(arg) is dict: | 7 | if type(arg) is dict: | ||
| 9 | if arg.get('name'): | 8 | if arg.get('name'): | ||
| 10 | if arg['name'].lower() in valid_bushes_values: | 9 | if arg['name'].lower() in valid_bushes_values: | ||
| 11 | current_cost = arg.get('cost', 0) | 10 | current_cost = arg.get('cost', 0) | ||
| n | 12 | if type(current_cost) is int or type(current_cost) is float: | n | ||
| 13 | cost += current_cost | 11 | cost += current_cost | ||
| 14 | 12 | ||||
| 15 | for key, val in kwargs.items(): | 13 | for key, val in kwargs.items(): | ||
| 16 | if type(val) is dict: | 14 | if type(val) is dict: | ||
| 17 | if val.get('name'): | 15 | if val.get('name'): | ||
| 18 | if val['name'].lower() in valid_bushes_values: | 16 | if val['name'].lower() in valid_bushes_values: | ||
| n | 19 | current_cost = val.get('cost',0) | n | 17 | current_cost = val.get('cost', 0) |
| 20 | if type(current_cost) is int or type(current_cost) is float: | ||||
| 21 | cost += current_cost | 18 | cost += current_cost | ||
| 22 | for letter in key: | 19 | for letter in key: | ||
| 23 | unique_letters_in_name.add(letter) | 20 | unique_letters_in_name.add(letter) | ||
| 24 | 21 | ||||
| 25 | if cost > 42: | 22 | if cost > 42: | ||
| n | 26 | is_valid = False | n | 23 | return 'Ni!' |
| 27 | 24 | ||||
| 28 | whole_part_of_cost = int(cost) | 25 | whole_part_of_cost = int(cost) | ||
| 29 | 26 | ||||
| 30 | unique_letters_in_name_count = len(unique_letters_in_name) | 27 | unique_letters_in_name_count = len(unique_letters_in_name) | ||
| 31 | 28 | ||||
| 32 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | 29 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | ||
| n | 33 | is_valid = False | n | 30 | return 'Ni!' |
| 34 | 31 | ||||
| t | 35 | if is_valid == True: | t | ||
| 36 | return f'{cost:.2f}лв' | 32 | return f'{cost:.2f}лв' | ||
| 37 | else: | ||||
| 38 | return 'Ni!' |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | def function_that_says_ni(*args, **kwargs): | f | 1 | def function_that_says_ni(*args, **kwargs): |
| 2 | is_valid = True | 2 | is_valid = True | ||
| 3 | cost = 0 | 3 | cost = 0 | ||
| 4 | unique_letters_in_name = set() | 4 | unique_letters_in_name = set() | ||
| 5 | valid_bushes_values = ['храст', 'shrub', 'bush'] | 5 | valid_bushes_values = ['храст', 'shrub', 'bush'] | ||
| 6 | 6 | ||||
| 7 | for arg in args: | 7 | for arg in args: | ||
| 8 | if type(arg) is dict: | 8 | if type(arg) is dict: | ||
| 9 | if arg.get('name'): | 9 | if arg.get('name'): | ||
| 10 | if arg['name'].lower() in valid_bushes_values: | 10 | if arg['name'].lower() in valid_bushes_values: | ||
| 11 | current_cost = arg.get('cost', 0) | 11 | current_cost = arg.get('cost', 0) | ||
| 12 | if type(current_cost) is int or type(current_cost) is float: | 12 | if type(current_cost) is int or type(current_cost) is float: | ||
| 13 | cost += current_cost | 13 | cost += current_cost | ||
| 14 | 14 | ||||
| 15 | for key, val in kwargs.items(): | 15 | for key, val in kwargs.items(): | ||
| 16 | if type(val) is dict: | 16 | if type(val) is dict: | ||
| 17 | if val.get('name'): | 17 | if val.get('name'): | ||
| 18 | if val['name'].lower() in valid_bushes_values: | 18 | if val['name'].lower() in valid_bushes_values: | ||
| 19 | current_cost = val.get('cost',0) | 19 | current_cost = val.get('cost',0) | ||
| 20 | if type(current_cost) is int or type(current_cost) is float: | 20 | if type(current_cost) is int or type(current_cost) is float: | ||
| 21 | cost += current_cost | 21 | cost += current_cost | ||
| 22 | for letter in key: | 22 | for letter in key: | ||
| 23 | unique_letters_in_name.add(letter) | 23 | unique_letters_in_name.add(letter) | ||
| 24 | 24 | ||||
| t | 25 | t | |||
| 26 | if cost > 42: | 25 | if cost > 42: | ||
| 27 | is_valid = False | 26 | is_valid = False | ||
| 28 | 27 | ||||
| 29 | whole_part_of_cost = int(cost) | 28 | whole_part_of_cost = int(cost) | ||
| 30 | 29 | ||||
| 31 | unique_letters_in_name_count = len(unique_letters_in_name) | 30 | unique_letters_in_name_count = len(unique_letters_in_name) | ||
| 32 | 31 | ||||
| 33 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | 32 | if whole_part_of_cost == 0 or unique_letters_in_name_count % whole_part_of_cost != 0: | ||
| 34 | is_valid = False | 33 | is_valid = False | ||
| 35 | 34 | ||||
| 36 | if is_valid == True: | 35 | if is_valid == True: | ||
| 37 | return f'{cost:.2f}лв' | 36 | return f'{cost:.2f}лв' | ||
| 38 | else: | 37 | else: | ||
| 39 | return 'Ni!' | 38 | return 'Ni!' |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
22.10.2024 15:55