If you want to know the logic of the tower of Hanoi, please refer to this article Python recursion – The Tower of Hanoi. They are exactly the same, just using different language.
I don’t have problem with recursion, but I did have problem with understanding the logic of the tower of Hanoi before. My husband even tried to explain the logic to me when he got a bad cold, but my brain was just like sticking in some place and couldn’t understand why.
But the logic is super simple when I finally figured it out.
Here is the simple question: Please use recursion to revert 12345 to 54321
def reverse(val, new_val): if val == '': print(new_val) else: reverse(val[:-1], new_val + val[-1:]) if __name__ == '__main__': reverse(raw_input('Please provide any number: '), '')