[Solved] Generic Pythonic Code to Solve: 2**2**2**2**0 [closed]


Alternative approach to achieve this by using reduce() function as:

>>> operation_str="3^2^4^1^0"
>>> reduce(lambda x, y: int(y)**int(x), operation_str.split('^')[::-1])
43046721

Explanation based on step by step operation:

>>> operation_str.split('^')
['3', '2', '4', '1', '0'] # Converted str into list
>>> operation_str.split('^')[::-1]
['0', '1', '4', '2', '3'] # Reversed list
>>> reduce(lambda x, y: int(y)**int(x), operation_str.split('^')[::-1])
43046721  # Check reduce() fucntion linked above to know how it works

Also read: Why should exec() and eval() be avoided?

1

solved Generic Pythonic Code to Solve: 2**2**2**2**0 [closed]