I got the python code below that solves the expression above. I believe there should be a more generic Pythonic approach. By generic I mean it should solve any combinations of numbers (not just: 2 ** 2 ** 2 ** 2 ** 0) e.g: 3 ** 2 ** 4 ** 1 ** 0, 3 ** 3 ** 1 ** 2 ** 0 etc
def get_expo(expo, index, cur_expo):
return int(expo[index-1]) ** cur_expo
def solve_expo(expo):
expo = expo.split("^")
index = len(expo) - 1
cur_expo = int(expo[-1])
result = 0
for i in range(len(expo)):
if index == 1:
result = get_expo(expo, index, cur_expo)
return "Answer is: " + str(result)
else:
cur_expo = get_expo(expo, index, cur_expo)
index -= 1
print solve_expo("2^2^2^2^0")
I need a better pythonic approach to solving it?