Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Functions Advanced - Lab
- # https://judge.softuni.org/Contests/Practice/Index/1838#0
- -----------------------------------------------------------------------------------------------------
- # 01. Multiplication Function
- # 02. Person Info
- # 03. Cheese Showcase
- # 04. Rectangle
- # 05. Operate
- # 06. Recursive Power
- -----------------------------------------------------------------------------------------------------
- # 01. Multiplication Function
- def multiply(*args):
- result = 1
- for num in args:
- result = result * num
- return result
- -----------------------------------------------------------------------------------------------------
- # 02. Person Info
- def get_info(**kwargs):
- result = f'This is {kwargs["name"]} from {kwargs["town"]} and he is {kwargs["age"]} years old'
- return result
- -----------------------------------------------------------------------------------------------------
- # 03. Cheese Showcase
- def sorting_cheeses(**kwargs):
- result = ""
- sorted_cheese = sorted(kwargs.items(), key=lambda x: (-len(x[1]), x[0]))
- for name, pieces in sorted_cheese:
- result += name + '\n'
- sorted_pieces = sorted(pieces, reverse=True)
- result += '\n'.join([str(x) for x in sorted_pieces]) + '\n'
- return result
- -----------------------------------------------------------------------------------------------------
- # 04. Rectangle
- def rectangle_area(args):
- return args[0] * args[1]
- def rectangle_perimeter(args):
- return (args[0] + args[1]) * 2
- def are_arguments_valid(args):
- for el in args:
- if not isinstance(el, int):
- return False
- return True
- def rectangle(*args):
- if are_arguments_valid(args):
- return f'Rectangle area: {rectangle_area(args)}\nRectangle perimeter: {rectangle_perimeter(args)}'
- else:
- return 'Enter valid values!'
- -----------------------------------------------------------------------------------------------------
- # 05. Operate
- import functools
- def operate(opers, *args):
- result = 0
- if opers == '+':
- result = functools.reduce(lambda x, y: x + y, args)
- elif opers == '-':
- result = functools.reduce(lambda x, y: x - y, args)
- elif opers == '*':
- result = functools.reduce(lambda x, y: x * y, args)
- elif opers == '/':
- result = functools.reduce(lambda x, y: x / y, args)
- return result
- -----------------------------------------------------------------------------------------------------
- # 06. Recursive Power
- def recursive_power(num, power):
- if power == 0:
- return 1
- return num * recursive_power(num, power - 1)
- -----------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement