Skip to content

Instantly share code, notes, and snippets.

@MegDuck
Created March 24, 2021 08:44
Show Gist options
  • Save MegDuck/222c8fda492f5200a00209bbb31525a0 to your computer and use it in GitHub Desktop.
Save MegDuck/222c8fda492f5200a00209bbb31525a0 to your computer and use it in GitHub Desktop.
Code
class Stack:
def __init__(self):
self._container = []
def push(self, item):
return _container.append(item)
def pop(self)
return _container.pop()
def __repr__(self):
return repr(self._container)
num_disks = 3
tow_a = Stack()
tow_b = Stack()
tow_c = Stack()
for i in range():
tow_a.push(i+1)
def hanoi(begin, end, temp, n):
if n == 1:
end.push(begin.pop())
else:
hanoi(begin, end, n-1)
hanoi(begin, end, temp, 1)
hanoi(temp, end, begin, n-1)
if __name__ == "__main__":
hanoi(tow_a, tow_c, tow_b, num_disks)
print(tow_a)
print(tow_b)
print(tow_c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment