Implement a recusive solution to the Tower of Hanoi using stacks. The code for t
ID: 3731411 • Letter: I
Question
Implement a recusive solution to the Tower of Hanoi using stacks. The code for this part is only suppose to 6 lines long and 2 of them are an if and else conditional. Using the specifications provide fill in the code using python. Useful infromation: n = 3, source = Stack(), auxilary = Stack(), destination = Stack(), Tower_of_Hanoi(n, source, auxilary, destination)
def Tower_of_Hanoi(n, a, b, c):
1.
2.
3.
4.
5.
6.
print(n, s, a, d)
print()
The outcome should be:
2 [ 0, 1, 2] [ 1 01 1 [ 0, 1, 2] [ ] 01 0 Ce] 2] 1 1 1 2 1 [ 0, 11 [] I 21 0 e, 1] 2] 0 1 ] 2] 0 ] 7 1 1 [e, 1, 2 1Explanation / Answer
Below is the code in 6 lines:
def Tower_of_Hanoi(n, a, b, c):
if n == 0:
towers[c].push(towers[a].pop())
else:
Tower_of_Hanoi(n-1, a,c, b)
towers[c].push(towers[a].pop())
Tower_of_Hanoi(n-1, b, a, c)
print(n, s, a, d)
print()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.