Advertisement
GeorgiLukanov87

Functions Advanced - Lab

Sep 8th, 2022 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.80 KB | None | 0 0
  1. # Functions Advanced - Lab
  2. # https://judge.softuni.org/Contests/Practice/Index/1838#0
  3.  
  4. -----------------------------------------------------------------------------------------------------
  5.  
  6. # 01. Multiplication Function
  7. # 02. Person Info
  8. # 03. Cheese Showcase
  9. # 04. Rectangle
  10. # 05. Operate
  11. # 06. Recursive Power
  12.  
  13. -----------------------------------------------------------------------------------------------------
  14.  
  15. # 01. Multiplication Function
  16.  
  17. def multiply(*args):
  18.     result = 1
  19.     for num in args:
  20.         result = result * num
  21.     return result
  22.  
  23.  
  24. -----------------------------------------------------------------------------------------------------
  25.  
  26. # 02. Person Info
  27.  
  28. def get_info(**kwargs):
  29.     result = f'This is {kwargs["name"]} from {kwargs["town"]} and he is {kwargs["age"]} years old'
  30.     return result
  31.  
  32.  
  33. -----------------------------------------------------------------------------------------------------
  34.  
  35. # 03. Cheese Showcase
  36.  
  37. def sorting_cheeses(**kwargs):
  38.     result = ""
  39.     sorted_cheese = sorted(kwargs.items(), key=lambda x: (-len(x[1]), x[0]))
  40.    
  41.     for name, pieces in sorted_cheese:
  42.         result += name + '\n'
  43.         sorted_pieces = sorted(pieces, reverse=True)
  44.         result += '\n'.join([str(x) for x in sorted_pieces]) + '\n'
  45.  
  46.     return result
  47.  
  48.  
  49. -----------------------------------------------------------------------------------------------------
  50.  
  51. # 04. Rectangle
  52.  
  53. def rectangle_area(args):
  54.     return args[0] * args[1]
  55.  
  56.  
  57. def rectangle_perimeter(args):
  58.     return (args[0] + args[1]) * 2
  59.  
  60.  
  61. def are_arguments_valid(args):
  62.     for el in args:
  63.         if not isinstance(el, int):
  64.             return False
  65.     return True
  66.  
  67.  
  68. def rectangle(*args):
  69.     if are_arguments_valid(args):
  70.         return f'Rectangle area: {rectangle_area(args)}\nRectangle perimeter: {rectangle_perimeter(args)}'
  71.     else:
  72.         return 'Enter valid values!'
  73.    
  74.    
  75. -----------------------------------------------------------------------------------------------------
  76.  
  77. # 05. Operate
  78.  
  79. import functools
  80.  
  81.  
  82. def operate(opers, *args):
  83.     result = 0
  84.     if opers == '+':
  85.         result = functools.reduce(lambda x, y: x + y, args)
  86.     elif opers == '-':
  87.         result = functools.reduce(lambda x, y: x - y, args)
  88.     elif opers == '*':
  89.         result = functools.reduce(lambda x, y: x * y, args)
  90.     elif opers == '/':
  91.         result = functools.reduce(lambda x, y: x / y, args)
  92.  
  93.     return result
  94.  
  95.  
  96. -----------------------------------------------------------------------------------------------------
  97.  
  98. # 06. Recursive Power
  99.  
  100. def recursive_power(num, power):
  101.     if power == 0:
  102.         return 1
  103.     return num * recursive_power(num, power - 1)
  104.  
  105.  
  106. -----------------------------------------------------------------------------------------------------
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement