Advertisement
GeorgiLukanov87

Functions Advanced - Exercise

Sep 7th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.83 KB | None | 0 0
  1. # Functions Advanced - Exercise
  2. # https://judge.softuni.org/Contests/Practice/Index/1839#0
  3.  
  4. --------------------------------------------------------------------------------------------------------
  5.  
  6. # 01. Negative vs Positive
  7. # 02. Keyword Arguments Length
  8. # 03. Even or Odd
  9. # 04. Numbers Filter
  10. # 05. Concatenate
  11. # 06. Function Executor
  12. # 07. Grocery
  13. # 08. Age Assignment
  14. # 09. Recursion Palindrome
  15. # 10. *Fill the Box
  16. # 11. *Math Operations
  17.  
  18. --------------------------------------------------------------------------------------------------------
  19.  
  20. # 01. Negative vs Positive
  21.  
  22.  
  23. def calc_sums(*args):
  24.     positive = []
  25.     negative = []
  26.     for el in data:
  27.         if el > 0:
  28.             positive.append(el)
  29.         else:
  30.             negative.append(el)
  31.     return negative, positive
  32.  
  33.  
  34. data = [int(x) for x in input().split()]
  35.  
  36. negative, positive = calc_sums(data)
  37.  
  38. result_negative = sum(negative)
  39. result_positive = sum(positive)
  40.  
  41. print(result_negative)
  42. print(result_positive)
  43.  
  44. if abs(result_negative) > abs(result_positive):
  45.     print('The negatives are stronger than the positives')
  46. else:
  47.     print('The positives are stronger than the negatives')
  48.  
  49.    
  50. --------------------------------------------------------------------------------------------------------
  51.  
  52. # 02. Keyword Arguments Length
  53.  
  54.  
  55. def kwargs_length(**kwargs):
  56.     dict_len = len(kwargs)
  57.     return dict_len
  58.  
  59.  
  60. --------------------------------------------------------------------------------------------------------
  61.  
  62. # 03. Even or Odd
  63.  
  64. def even_odd(*args):
  65.     if args[-1] == 'even':
  66.         return [int(x) for x in args[:-1] if x % 2 == 0]
  67.     else:
  68.         return [int(x) for x in args[:-1] if x % 2 != 0]
  69.    
  70.    
  71. --------------------------------------------------------------------------------------------------------
  72.    
  73. # 04. Numbers Filter
  74.  
  75. def even_odd_filter(**kwargs):
  76.     my_dict = {}
  77.     for el, value in kwargs.items():
  78.         if el == 'odd':
  79.             if el not in my_dict:
  80.                 my_dict[el] = []
  81.             my_dict[el] = ([int(x) for x in value if x % 2 != 0])
  82.         else:
  83.             if el not in my_dict:
  84.                 my_dict[el] = []
  85.             my_dict[el] = ([int(x) for x in value if x % 2 == 0])
  86.  
  87.     return dict(sorted(my_dict.items(), key=lambda x: -len(x[1])))
  88.  
  89.  
  90. --------------------------------------------------------------------------------------------------------
  91.  
  92. # 05. Concatenate
  93.  
  94. def concatenate(*args, **kwargs):
  95.     new_string = ""
  96.     for word in args:
  97.         new_string += word
  98.  
  99.     for key, value in kwargs.items():
  100.         if key in new_string:
  101.             new_string = new_string.replace(key, value)
  102.  
  103.     return new_string
  104.  
  105.  
  106. --------------------------------------------------------------------------------------------------------
  107.  
  108. # 06. Function Executor
  109.  
  110. def func_executor(*args):
  111.     results = []
  112.     for function, arg in args:
  113.         result = function(*arg)
  114.         results.append(f'{function.__name__} - {result}')
  115.     return '\n'.join(results)
  116.  
  117.  
  118. --------------------------------------------------------------------------------------------------------
  119.  
  120. # 07. Grocery
  121.  
  122. def grocery_store(**kwargs):
  123.     final_print = ''
  124.     kwargs = sorted(kwargs.items(), key=lambda x: (-x[1], -len(x[0]), x[0]))
  125.  
  126.     for key, value in kwargs:
  127.         final_print += f'{key}: {value}\n'
  128.     return final_print
  129.  
  130.  
  131. --------------------------------------------------------------------------------------------------------
  132.  
  133. # 08. Age Assignment
  134.  
  135.  
  136. def age_assignment(*args, **kwargs):
  137.     args = sorted(args)
  138.     kwargs = sorted(kwargs.items())
  139.     result = zip(args, kwargs)
  140.  
  141.     final_print = ""
  142.    
  143.     for name, ages in result:
  144.         final_print += f'{name} is {ages[1]} years old.\n'
  145.  
  146.     return final_print
  147.  
  148. --------------------------------------------------------------------------------------------------------
  149.  
  150. # 09. Recursion Palindrome
  151.  
  152. def palindrome(text, index):
  153.     if index == len(text) // 2:
  154.         return f'{text} is a palindrome'
  155.    
  156.     left = text[index]
  157.     right = text[len(text) - 1 - index]
  158.    
  159.     if left != right:
  160.         return f"{text} is not a palindrome"
  161.    
  162.     return palindrome(text, index + 1)
  163.  
  164.  
  165. --------------------------------------------------------------------------------------------------------
  166. # 10. *Fill the Box
  167.  
  168.  
  169. from collections import deque
  170.  
  171.  
  172. def fill_the_box(height, length, width, *args):
  173.     box_capacity = height * length * width
  174.     all_cubes = deque(args)
  175.  
  176.     while True:
  177.         current_el = all_cubes.popleft()
  178.         if current_el == 'Finish':
  179.             break
  180.  
  181.         box_capacity -= current_el
  182.  
  183.     if box_capacity > 0:
  184.         return f'There is free space in the box. You could put {box_capacity} more cubes.'
  185.     return f'No more free space! You have {abs(box_capacity)} more cubes.'
  186.  
  187.  
  188. --------------------------------------------------------------------------------------------------------
  189.  
  190. # 11. *Math Operations
  191.  
  192. from collections import deque
  193.  
  194.  
  195. def math_operations(*args, **kwargs):
  196.     nums = deque(args)
  197.     position_counter = 0
  198.     while nums:
  199.         position_counter += 1
  200.         current_num = nums.popleft()
  201.  
  202.         if position_counter == 1:
  203.             kwargs['a'] += current_num
  204.  
  205.         elif position_counter == 2:
  206.             kwargs['s'] -= current_num
  207.  
  208.         elif position_counter == 3:
  209.             if current_num != 0:
  210.                 kwargs['d'] /= current_num
  211.  
  212.         elif position_counter == 4:
  213.             kwargs['m'] *= current_num
  214.             position_counter = 0
  215.  
  216.     kwargs = sorted(kwargs.items(), key=lambda x: (-x[1], x[0]))
  217.  
  218.     result = ""
  219.     for el, value in kwargs:
  220.         result += f'{el}: {value:.1f}\n'
  221.     return result
  222.  
  223.  
  224. --------------------------------------------------------------------------------------------------------
  225.  
  226.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement