Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Functions Advanced - Exercise
- # https://judge.softuni.org/Contests/Practice/Index/1839#0
- --------------------------------------------------------------------------------------------------------
- # 01. Negative vs Positive
- # 02. Keyword Arguments Length
- # 03. Even or Odd
- # 04. Numbers Filter
- # 05. Concatenate
- # 06. Function Executor
- # 07. Grocery
- # 08. Age Assignment
- # 09. Recursion Palindrome
- # 10. *Fill the Box
- # 11. *Math Operations
- --------------------------------------------------------------------------------------------------------
- # 01. Negative vs Positive
- def calc_sums(*args):
- positive = []
- negative = []
- for el in data:
- if el > 0:
- positive.append(el)
- else:
- negative.append(el)
- return negative, positive
- data = [int(x) for x in input().split()]
- negative, positive = calc_sums(data)
- result_negative = sum(negative)
- result_positive = sum(positive)
- print(result_negative)
- print(result_positive)
- if abs(result_negative) > abs(result_positive):
- print('The negatives are stronger than the positives')
- else:
- print('The positives are stronger than the negatives')
- --------------------------------------------------------------------------------------------------------
- # 02. Keyword Arguments Length
- def kwargs_length(**kwargs):
- dict_len = len(kwargs)
- return dict_len
- --------------------------------------------------------------------------------------------------------
- # 03. Even or Odd
- def even_odd(*args):
- if args[-1] == 'even':
- return [int(x) for x in args[:-1] if x % 2 == 0]
- else:
- return [int(x) for x in args[:-1] if x % 2 != 0]
- --------------------------------------------------------------------------------------------------------
- # 04. Numbers Filter
- def even_odd_filter(**kwargs):
- my_dict = {}
- for el, value in kwargs.items():
- if el == 'odd':
- if el not in my_dict:
- my_dict[el] = []
- my_dict[el] = ([int(x) for x in value if x % 2 != 0])
- else:
- if el not in my_dict:
- my_dict[el] = []
- my_dict[el] = ([int(x) for x in value if x % 2 == 0])
- return dict(sorted(my_dict.items(), key=lambda x: -len(x[1])))
- --------------------------------------------------------------------------------------------------------
- # 05. Concatenate
- def concatenate(*args, **kwargs):
- new_string = ""
- for word in args:
- new_string += word
- for key, value in kwargs.items():
- if key in new_string:
- new_string = new_string.replace(key, value)
- return new_string
- --------------------------------------------------------------------------------------------------------
- # 06. Function Executor
- def func_executor(*args):
- results = []
- for function, arg in args:
- result = function(*arg)
- results.append(f'{function.__name__} - {result}')
- return '\n'.join(results)
- --------------------------------------------------------------------------------------------------------
- # 07. Grocery
- def grocery_store(**kwargs):
- final_print = ''
- kwargs = sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))
- for key, value in kwargs:
- final_print += f'{key}: {value}\n'
- return final_print
- --------------------------------------------------------------------------------------------------------
- # 08. Age Assignment
- def age_assignment(*args, **kwargs):
- args = sorted(args)
- kwargs = sorted(kwargs.items())
- result = zip(args, kwargs)
- final_print = ""
- for name, ages in result:
- final_print += f'{name} is {ages[1]} years old.\n'
- return final_print
- --------------------------------------------------------------------------------------------------------
- # 09. Recursion Palindrome
- def palindrome(text, index):
- if index == len(text) // 2:
- return f'{text} is a palindrome'
- left = text[index]
- right = text[len(text) - 1 - index]
- if left != right:
- return f"{text} is not a palindrome"
- return palindrome(text, index + 1)
- --------------------------------------------------------------------------------------------------------
- # 10. *Fill the Box
- from collections import deque
- def fill_the_box(height, length, width, *args):
- box_capacity = height * length * width
- all_cubes = deque(args)
- while True:
- current_el = all_cubes.popleft()
- if current_el == 'Finish':
- break
- box_capacity -= current_el
- if box_capacity > 0:
- return f'There is free space in the box. You could put {box_capacity} more cubes.'
- return f'No more free space! You have {abs(box_capacity)} more cubes.'
- --------------------------------------------------------------------------------------------------------
- # 11. *Math Operations
- from collections import deque
- def math_operations(*args, **kwargs):
- nums = deque(args)
- position_counter = 0
- while nums:
- position_counter += 1
- current_num = nums.popleft()
- if position_counter == 1:
- kwargs['a'] += current_num
- elif position_counter == 2:
- kwargs['s'] -= current_num
- elif position_counter == 3:
- if current_num != 0:
- kwargs['d'] /= current_num
- elif position_counter == 4:
- kwargs['m'] *= current_num
- position_counter = 0
- kwargs = sorted(kwargs.items(), key=lambda x: (-x[1], x[0]))
- result = ""
- for el, value in kwargs:
- result += f'{el}: {value:.1f}\n'
- return result
- --------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement