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

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

10 точки общо

10 успешни теста
0 неуспешни теста
Код (Also, grammar!)
Скрий всички коментари

 1"""Just a bit different of a solution, nothing too fancy."""
 2
 3
 4VALID_SHRUBS = ('храст', 'shrub', 'bush')
 5MAX_COST = 42
 6
 7
 8def function_that_says_ni(*args, **kwargs):
 9    # Get all pairs as key-value pairs (tuples) with empty "keys" for positional args
10    all_arg_pairs = [('', arg) for arg in args] + list(kwargs.items())
11    # Filter only dicts that contain shrubs, bushes, etc.
12    shrub_pairs = [pair for pair in all_arg_pairs
13                   if isinstance(pair[1], dict)
14                   and pair[1].get('name', 'шак шак шука, дюнера е тука').lower() in VALID_SHRUBS]
15    # The following is pretty self-explanatory
16    total_unique_chars = len(set(''.join(shrub_pair[0] for shrub_pair in shrub_pairs)))
17    total_cost = sum(shrub_pair[1].get('cost', 0) for shrub_pair in shrub_pairs)
18    total_cost_whole_part = int(total_cost)
19    # Determine if the shrubbery is satisfactory
20    if (total_cost <= MAX_COST        # Ensure it's not too expensive
21        and total_cost_whole_part     # Protect from division by 0
22        and not total_unique_chars % total_cost_whole_part): # Ensure it's pretty
23        return f'{total_cost:.2f}лв'
24    return 'Ni!'

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

OK

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

t1"""Just a bit different solution, nothing too fancy."""t1"""Just a bit different of a solution, nothing too fancy."""
22
33
4VALID_SHRUBS = ('храст', 'shrub', 'bush')4VALID_SHRUBS = ('храст', 'shrub', 'bush')
5MAX_COST = 425MAX_COST = 42
66
77
8def function_that_says_ni(*args, **kwargs):8def function_that_says_ni(*args, **kwargs):
9    # Get all pairs as key-value pairs (tuples) with empty "keys" for positional args9    # Get all pairs as key-value pairs (tuples) with empty "keys" for positional args
10    all_arg_pairs = [('', arg) for arg in args] + list(kwargs.items())10    all_arg_pairs = [('', arg) for arg in args] + list(kwargs.items())
11    # Filter only dicts that contain shrubs, bushes, etc.11    # Filter only dicts that contain shrubs, bushes, etc.
12    shrub_pairs = [pair for pair in all_arg_pairs12    shrub_pairs = [pair for pair in all_arg_pairs
13                   if isinstance(pair[1], dict)13                   if isinstance(pair[1], dict)
14                   and pair[1].get('name', 'шак шак шука, дюнера е тука').lower() in VALID_SHRUBS]14                   and pair[1].get('name', 'шак шак шука, дюнера е тука').lower() in VALID_SHRUBS]
15    # The following is pretty self-explanatory15    # The following is pretty self-explanatory
16    total_unique_chars = len(set(''.join(shrub_pair[0] for shrub_pair in shrub_pairs)))16    total_unique_chars = len(set(''.join(shrub_pair[0] for shrub_pair in shrub_pairs)))
17    total_cost = sum(shrub_pair[1].get('cost', 0) for shrub_pair in shrub_pairs)17    total_cost = sum(shrub_pair[1].get('cost', 0) for shrub_pair in shrub_pairs)
18    total_cost_whole_part = int(total_cost)18    total_cost_whole_part = int(total_cost)
19    # Determine if the shrubbery is satisfactory19    # Determine if the shrubbery is satisfactory
20    if (total_cost <= MAX_COST        # Ensure it's not too expensive20    if (total_cost <= MAX_COST        # Ensure it's not too expensive
21        and total_cost_whole_part     # Protect from division by 021        and total_cost_whole_part     # Protect from division by 0
22        and not total_unique_chars % total_cost_whole_part): # Ensure it's pretty22        and not total_unique_chars % total_cost_whole_part): # Ensure it's pretty
23        return f'{total_cost:.2f}лв'23        return f'{total_cost:.2f}лв'
24    return 'Ni!'24    return 'Ni!'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1"""Just a bit different solution, nothing too fancy."""f1"""Just a bit different solution, nothing too fancy."""
nn2 
23
3VALID_SHRUBS = ('храст', 'shrub', 'bush')4VALID_SHRUBS = ('храст', 'shrub', 'bush')
4MAX_COST = 425MAX_COST = 42
tt6 
57
6def function_that_says_ni(*args, **kwargs):8def function_that_says_ni(*args, **kwargs):
7    # Get all pairs as key-value pairs (tuples) with empty "keys" for positional args9    # Get all pairs as key-value pairs (tuples) with empty "keys" for positional args
8    all_arg_pairs = [('', arg) for arg in args] + list(kwargs.items())10    all_arg_pairs = [('', arg) for arg in args] + list(kwargs.items())
9    # Filter only dicts that contain shrubs, bushes, etc.11    # Filter only dicts that contain shrubs, bushes, etc.
10    shrub_pairs = [pair for pair in all_arg_pairs12    shrub_pairs = [pair for pair in all_arg_pairs
11                   if isinstance(pair[1], dict)13                   if isinstance(pair[1], dict)
12                   and pair[1].get('name', 'шак шак шука, дюнера е тука').lower() in VALID_SHRUBS]14                   and pair[1].get('name', 'шак шак шука, дюнера е тука').lower() in VALID_SHRUBS]
13    # The following is pretty self-explanatory15    # The following is pretty self-explanatory
14    total_unique_chars = len(set(''.join(shrub_pair[0] for shrub_pair in shrub_pairs)))16    total_unique_chars = len(set(''.join(shrub_pair[0] for shrub_pair in shrub_pairs)))
15    total_cost = sum(shrub_pair[1].get('cost', 0) for shrub_pair in shrub_pairs)17    total_cost = sum(shrub_pair[1].get('cost', 0) for shrub_pair in shrub_pairs)
16    total_cost_whole_part = int(total_cost)18    total_cost_whole_part = int(total_cost)
17    # Determine if the shrubbery is satisfactory19    # Determine if the shrubbery is satisfactory
18    if (total_cost <= MAX_COST        # Ensure it's not too expensive20    if (total_cost <= MAX_COST        # Ensure it's not too expensive
19        and total_cost_whole_part     # Protect from division by 021        and total_cost_whole_part     # Protect from division by 0
20        and not total_unique_chars % total_cost_whole_part): # Ensure it's pretty22        and not total_unique_chars % total_cost_whole_part): # Ensure it's pretty
21        return f'{total_cost:.2f}лв'23        return f'{total_cost:.2f}лв'
22    return 'Ni!'24    return 'Ni!'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op