fix few details, stacks

This commit is contained in:
Mari Wahl 2014-08-27 16:11:26 -04:00
parent 578a4f28da
commit 415e9c02e2
18 changed files with 301 additions and 748 deletions

Binary file not shown.

View File

@ -1,3 +1,10 @@
#!/usr/bin/python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
""" A class for an animal shelter"""
class Node(object):
def __init__(self, name, which):
self.name = name
@ -14,44 +21,44 @@ class AnimalShelter(object):
self.last_cat = None
self.last_dog = None
self.counter = 0
def enqueue(self, name, which):
self.counter += 1
node = Node(name, which)
node.timestamp = self.counter
self.counter += 1
node = Node(name, which)
node.timestamp = self.counter
if which == 'cat':
if not self.first_cat:
self.first_cat = node
if self.last_cat:
self.last_cat.next = node
self.last_cat = node
if which == 'dog':
if not self.first_dog:
self.first_dog = node
if self.last_dog:
self.last_dog.next = node
self.last_dog = node
self.last_dog = node
def dequeueDog(self):
if self.first_dog:
node = self.first_dog
self.first_dog = node.next
return str(node.name)
raise Exception('No Dogs!')
raise Exception('No Dogs!')
def dequeueCat(self):
if self.first_cat:
node = self.first_cat
self.first_cat = node.next
return str(node.name)
raise Exception('No Cats!')
raise Exception('No Cats!')
@ -65,10 +72,10 @@ class AnimalShelter(object):
elif nodedog and nodecat:
if nodedog.timestamp < nodecat.timestamp:
return self.dequeueDog()
else:
else:
return self.dequeueCat()
raise Exception('No Animals!')
raise Exception('No Animals!')
@ -77,11 +84,11 @@ def main():
qs.enqueue('bob', 'cat')
qs.enqueue('mia', 'cat')
qs.enqueue('yoda', 'dog')
qs.enqueue('wolf', 'dog')
qs.enqueue('wolf', 'dog')
assert(qs.dequeueDog() == 'yoda')
assert(qs.dequeueCat() == 'bob')
print(qs.dequeueAny() == 'mia')
assert(qs.dequeueCat() == 'bob')
print(qs.dequeueAny() == 'mia')
if __name__ == '__main__':
main()

View File

@ -19,7 +19,7 @@ class LinkedQueue(object):
def isEmpty(self):
return bool(self.head)
return not bool(self.head)
def dequeue(self):

View File

@ -10,7 +10,7 @@ class Queue(object):
self.items = []
def isEmpty(self):
return bool(self.items)
return not bool(self.items)
def enqueue(self, item):
self.items.insert(0, item)

View File

@ -54,7 +54,7 @@ class Queue(object):
return "Queue empty!"
def isEmpty(self):
return not (bool(self.in_stack) or bool(self.out_stack))
return not ((bool(self.in_stack) or bool(self.out_stack)))

View File

@ -1,40 +1,42 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
''' use a stack to balance the parenteses of a string '''
from stack import Stack
def balance_par_str_with_stack(symbolString):
''' use a stack to balance the parenteses of a string '''
def balance_par_str_with_stack(str1):
s = Stack()
balanced = True
index = 0
while index < len(symbolString) and balanced:
symbol = symbolString[index]
while index < len(str1) and balanced:
symbol = str1[index]
if symbol == "(":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
def test_balance_par_str_with_stack(module_name='this module'):
if __name__ == '__main__':
print(balance_par_str_with_stack('((()))'))
print(balance_par_str_with_stack('(()'))
s = 'Tests in {name} have {con}!'
print(s.format(name=module_name, con='passed'))
if __name__ == '__main__':
test_balance_par_str_with_stack()

View File

@ -1,28 +1,30 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
'''transform a decimal number to a binary number with a stack '''
from stack import Stack
def dec2bin_with_stack(decnum):
'''transform a decimal number to a binary number with a stack '''
s = Stack()
str_aux = ''
while decnum > 0:
dig = decnum % 2
decnum = decnum//2
s.push(dig)
while not s.isEmpty():
str_aux += str(s.pop())
return str_aux
def test_dec2bin_with_stack(module_name='this module'):
decnum = 9
assert(dec2bin_with_stack(decnum) == '1001')
s = 'Tests in {name} have {con}!'
print(s.format(name=module_name, con='passed'))
if __name__ == '__main__':
test_dec2bin_with_stack()
decnum = 9
assert(dec2bin_with_stack(decnum) == '1001')

View File

@ -1,52 +1,73 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
class Node:
def __init__(self, value=None):
self.value = value
self.next = None
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
class StackwithNodes:
''' Define a Stack with nodes'''
""" A stack made of linked list"""
class Node(object):
def __init__(self, value=None, pointer=None):
self.value = value
self.pointer = pointer
class Stack(object):
def __init__(self):
self.top = None
self.head = None
def isEmpty(self):
return bool(self.top)
return not bool(self.head)
def push(self, item):
self.head = Node(item, self.head)
def pop(self):
node = self.top
self.top = node.next
return node.value
if self.head:
node = self.head
self.head = node.pointer
return node.value
else:
print('Stack is empty.')
def push(self, value):
node = Node(value)
node.next = self.top
self.top = node
def size(self):
node = self.top
if node not None: num_nodes = 1
else: return 0
while node.next:
num_nodes += 1
node = node.next
return num_nodes
def peek(self):
return self.top.value
def main():
stack = StackwithNodes()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.size())
print(stack.peek())
print(stack.pop())
print(stack.peek())
if self.head:
return self.head.value
else:
print('Stack is empty.')
def size(self):
node = self.head
count = 0
while node:
count +=1
node = node.pointer
return count
def _printList(self):
node = self.head
while node:
print(node.value)
node = node.pointer
if __name__ == '__main__':
main()
stack = Stack()
print("Is the stack empty? ", stack.isEmpty())
print("Adding 0 to 10 in the stack...")
for i in range(10):
stack.push(i)
stack._printList()
print("Stack size: ", stack.size())
print("Stack peek : ", stack.peek())
print("Pop...", stack.pop())
print("Stack peek: ", stack.peek())
print("Is the stack empty? ", stack.isEmpty())
stack._printList()

View File

@ -1,30 +1,31 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
import sys
import stack
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
''' Uses a stack to reverse a string '''
from stack import Stack
def reverse_string_with_stack(str1):
''' Uses a stack to reverse a string '''
s = stack.Stack()
s = Stack()
revStr = ''
for c in str1:
s.push(c)
while not s.isEmpty():
revStr += s.pop()
return revStr
def test_reverse_string_with_stack():
str1 = 'Buffy is a Slayer!'
assert(reverse_string_with_stack(str1) == '!reyalS a si yffuB')
print('Tests passed!')
return revStr
if __name__ == '__main__':
test_reverse_string_with_stack()
str1 = 'Buffy is a Slayer!'
print(str1)
print(reverse_string_with_stack(str1))

View File

@ -1,47 +1,57 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
class SetOfStacks(list):
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
""" define a class for a set of stacks """
from stack import Stack
class SetOfStacks(Stack):
def __init__(self, capacity=4):
self.stacks = []
self.last_stack = []
self.setofstacks = []
self.items = []
self.capacity = capacity
self.stacks.append(self.last_stack)
def __repr__(self):
return str(self.stacks)
def push(self, value):
last_stack = self.last_stack
if len(last_stack) is self.capacity:
last_stack = []
self.last_stack = last_stack
self.stacks.append(last_stack)
last_stack.append(value)
if self.size() >= self.capacity:
self.setofstacks.append(self.items)
self.items = []
self.items.append(value)
def pop(self):
last_stack = self.last_stack
value = last_stack.pop()
if len(last_stack) is 0:
self.stacks.pop()
self.last_stack = self.stacks[-1]
value = self.items.pop()
if self.isEmpty() and self.setofstacks:
self.items = self.setofstacks.pop()
return value
def main():
stack = SetOfStacks()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.push(6)
print(stack)
stack.pop()
stack.pop()
stack.pop()
print(stack)
def sizeStack(self):
return len(self.setofstacks)*self.capacity + self.size()
def __repr__(self):
aux = []
for s in self.setofstacks:
aux.extend(s)
aux.extend(self.items)
return '{}'.format(aux)
if __name__ == '__main__':
main()
capacity = 5
stack = SetOfStacks(capacity)
print("Is the stack empty? ", stack.isEmpty())
print("Adding 0 to 10 in the stack...")
for i in range(10):
stack.push(i)
print(stack)
print("Stack size: ", stack.sizeStack())
print("Stack peek : ", stack.peek())
print("Pop...", stack.pop())
print("Stack peek: ", stack.peek())
print("Is the stack empty? ", stack.isEmpty())
print(stack)

View File

@ -1,165 +0,0 @@
from stack import Stack, Node
class SetOfStacks(object):
def __init__(self, capacity):
assert(capacity > 1)
self.capacity = capacity
self.stack_now = None
self.stack_now_size = 0
self.old_stacks = []
def _checkIfFull(self):
return self.stack_now_size == self.capacity
def _addStack(self):
self.stack_now = Stack()
self.stack_now_size = 0
def _archiveStack(self):
self.old_stacks.append(self.stack_now)
self.stack_now = None
self.stack_now_size = 0
def _popItem(self):
if self.stack_now.top:
value = self.stack_now.top.value
if self.stack_now.top.next:
node = self.stack_now.top
self.stack_now.top = self.stack_now.top.next
else:
self.stack_now = []
return value
raise Exception('Stack is empty.')
def numberOfStacksUsed(self):
if self.stack_now:
return len(self.old_stacks) + 1
else:
return len(self.old_stacks)
def seeTop(self):
if self.stack_now:
return self.stack_now.top.value
elif self.old_stacks:
return self.old_stacks[-1].top.value
raise Exception('Stack is Empty')
def isEmpty(self):
return not (bool(self.stack_now) or bool(self.old_stacks))
def _sizeStackInUse(self):
return self.stack_now_size
def size(self):
return self._sizeStackInUse() + self.capacity*len(self.old_stacks)
def push(self, item):
if not self.stack_now:
self._addStack()
node = Node(item)
node.next = self.stack_now.top
self.stack_now.top = node
self.stack_now_size += 1
if self._checkIfFull():
self._archiveStack()
def pop(self):
if not self.stack_now:
if not self.old_stacks:
raise Exception('Stack is empty')
else:
self.stack_now = self.old_stacks.pop()
self.stack_now_size = self.capacity - 1
self._popItem()
else:
self._popItem()
if self.stack_now:
self.stack_now_size -= 1
else:
self.stack_now_size = 0
def main():
s1 = SetOfStacks(3)
print(s1.numberOfStacksUsed())
print(s1.isEmpty())
print(s1.size())
s1.push(1)
print('Push item 1')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(2)
s1.push(3)
print('Push item 2 and 3')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(4)
print('Push item 4')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(5)
print('Push item 5')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 5')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 4')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
s1.pop()
print('Pop item 3 e 2')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 1')
print(s1.numberOfStacksUsed())
print(s1.isEmpty())
print(s1.size())
if __name__ == '__main__':
main()

View File

@ -1,154 +0,0 @@
from stack import StackList
class SetOfStacksList(object):
def __init__(self, capacity):
assert(capacity > 1)
self.capacity = capacity
self.stack_now = []
self.old_stacks = []
def _checkIfFull(self):
return len(self.stack_now) == self.capacity
def _archiveStack(self):
self.old_stacks.append(self.stack_now)
self.stack_now = []
def _popItem(self):
if self.stack_now:
return self.stack_now.pop()
raise Exception('Stack is empty.')
def _sizeStackInUse(self):
return len(self.stack_now)
def numberOfStacksUsed(self):
if self.stack_now:
return len(self.old_stacks) + 1
else:
return len(self.old_stacks)
def seeTop(self):
if self.stack_now:
return self.stack_now[-1]
elif self.old_stacks:
return self.old_stacks[-1][-1]
raise Exception('Stack is Empty')
def isEmpty(self):
return not(bool(self.stack_now) or bool(self.old_stacks))
def size(self):
return len(self.stack_now) + self.capacity*len(self.old_stacks)
def push(self, item):
self.stack_now.append(item)
if self._checkIfFull():
self._archiveStack()
def pop(self):
if not self.stack_now:
if not self.old_stacks:
raise Exception('Stack is empty')
else:
self.stack_now = self.old_stacks.pop()
return self.stack_now.pop()
def popAt(self, index):
number_of_stacks = self.size()
if index < number_of_stacks:
if index == number_of_stacks - 1 and self.stack_now:
return self.stack_now.pop()
if index < number_of_stacks - 1:
stack_here = self.old_stacks[index]
return stack_here.pop()
raise Exception('Stack at index {} is empty.'.format(index))
raise Exception('Index larger than the number of stacks.')
def printStacks(self):
return str(self.old_stacks), str(self.stack_now)
def main():
s1 = SetOfStacksList(3)
print(s1.numberOfStacksUsed())
print(s1.isEmpty())
print(s1.size())
s1.push(1)
print('Push item 1')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(2)
s1.push(3)
print('Push item 2 and 3')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(4)
print('Push item 4')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.push(5)
print('Push item 5')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 5')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 4')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
s1.pop()
print('Pop item 3 e 2')
print(s1.numberOfStacksUsed())
print(s1.seeTop())
print(s1.isEmpty())
print(s1.size())
s1.pop()
print('Pop item 1')
print(s1.numberOfStacksUsed())
print(s1.isEmpty())
print(s1.size())
s2 = SetOfStacksList(3)
for i in range(1, 11):
s2.push(i)
print(s2.printStacks())
print(s2.popAt(2))
if __name__ == '__main__':
main()

View File

@ -1,21 +0,0 @@
def sortStack(s):
bufs = []
while s:
item = s.pop()
count, i = 0, 0
while bufs and bufs[-1] > item:
s.append(bufs.pop())
count += 1
bufs.append(item)
while i < count:
bufs.append(s.pop())
i += 1
return bufs
def main():
s = [3, 5, 1, 2, 6, 7, 8]
print(sortStack(s))
if __name__ == '__main__':
main()

View File

@ -1,40 +1,53 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
''' define the stack class '''
class Stack(object):
''' define the stack class '''
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
return not bool(self.items)
def push(self, items):
self.items.append(items)
def push(self, value):
self.items.append(value)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
value = self.items.pop()
if value:
return value
else:
print("Stack is empty.")
def size(self):
return len(self.items)
def peek(self):
if self.items:
return self.items[-1]
else:
print('Stack is empty.')
def __repr__(self):
return '{}'.format(self.items)
def main():
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.size())
print(stack.peek())
print(stack.pop())
print(stack.peek())
if __name__ == '__main__':
main()
stack = Stack()
print("Is the stack empty? ", stack.isEmpty())
print("Adding 0 to 10 in the stack...")
for i in range(10):
stack.push(i)
print("Stack size: ", stack.size())
print("Stack peek : ", stack.peek())
print("Pop...", stack.pop())
print("Stack peek: ", stack.peek())
print("Is the stack empty? ", stack.isEmpty())
print(stack)

View File

@ -1,107 +0,0 @@
from stack import Stack, Node, StackList
class StackMinElen(Stack):
def __init__(self):
self.top = None
self.mins = []
def printMin(self):
if self.mins:
return self.mins[-1]
raise Exception('Stack is empty.')
def pop(self):
if self.top:
self.mins.pop()
node = self.top
self.top = node.next
return node.value
raise Exception('Stack is empty.')
def push(self, item):
if self.top:
min_so_far = self.mins[-1]
if min_so_far > item:
self.mins.append(item)
else:
self.mins.append(min_so_far)
else:
self.mins.append(item)
node = Node(item)
node.next = self.top
self.top = node
class StackListMinElen(StackList):
def __init__(self):
self.items = []
self.mins = []
def printMin(self):
if self.mins:
return self.mins[-1]
raise Exception('Stack is Empty')
def push(self, item):
self.items.append(item)
if self.mins:
if self.mins[-1] > item:
self.mins.append(item)
else:
self.mins.append(self.mins[-1])
else:
self.mins.append(item)
def pop(self):
if self.items:
self.mins.pop()
return self.items.pop()
raise Exception('Stack is Empty')
def main():
s1 = StackListMinElen()
l1 = [4, 2, 6, 3, 1, 5]
for i in l1:
s1.push(i)
print('Min: ', s1.printMin())
print('Pop: ', s1.pop())
print('Min: ', s1.printMin())
print('Pop: ', s1.pop())
print('Min: ', s1.printMin())
print('Pop: ', s1.pop())
print('Min: ', s1.printMin())
print('Pop: ', s1.pop())
print('Min: ', s1.printMin())
print('Pop: ', s1.pop())
print('Min: ', s1.printMin())
s2 = StackMinElen()
l1 = [4, 2, 6, 3, 1, 5]
for i in l1:
s2.push(i)
print('Min: ', s2.printMin())
print('Pop: ', s2.pop())
print('Min: ', s2.printMin())
print('Pop: ', s2.pop())
print('Min: ', s2.printMin())
print('Pop: ', s2.pop())
print('Min: ', s2.printMin())
print('Pop: ', s2.pop())
print('Min: ', s2.printMin())
print('Pop: ', s2.pop())
print('Min: ', s2.printMin())
if __name__ == '__main__':
main()

View File

@ -1,94 +0,0 @@
#!/usr/bin/python3
# steinkirch at gmail.com
# astro.sunysb.edu/steinkirch
class Node(object):
def __init__(self, value=None):
self.value = value
self.next = None
class Stack(object):
def __init__(self):
self.top = None
def push(self, item):
node = Node(item)
node.next = self.top
self.top = node
def pop(self):
if self.top:
node = self.top
self.top = node.next
return node.value
raise Exception('Stack is empty.')
def isEmpty(self):
return bool(self.top)
def seeTop(self):
if self.top:
return self.top.value
raise Exception('Stack is empty.')
def size(self):
node = self.top
count = 0
while node:
count +=1
node = node.next
return count
class StackList(list):
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
if self.items:
return self.items.pop()
raise Exception('Stack is empty.')
def seeTop(self):
if self.items:
return self.items[-1]
raise Exception('Stack is empty.')
def size(self):
return len(self.items)
def isEmpty(self):
return bool(self.items)
def main():
s1 = StackList()
print(s1.isEmpty())
for i in range(1, 10):
s1.push(i)
print(s1.isEmpty())
print(s1.size())
print(s1.seeTop())
s1.pop()
print(s1.size())
print(s1.seeTop())
s2 = Stack()
print(s2.isEmpty())
for i in range(1, 10):
s2.push(i)
print(s2.isEmpty())
print(s2.size())
print(s2.seeTop())
s2.pop()
print(s2.size())
print(s2.seeTop())
if __name__ == '__main__':
main()

View File

@ -1,55 +1,78 @@
#!/usr/bin/python3
# mari von steinkirch @2013
# steinkirch at gmail
#!/usr/bin/python
class Stack(list):
def push(self, value):
if len(self) > 0:
last = self[-1]
minimum = self._find_minimum(value, last)
else:
minimum = value
self.minimum = minimum
self.append(NodeWithMin(value, minimum))
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
def _find_minimum(self, value, last_value):
if value < last_value.minimum:
return value
return last_value.minimum
''' A stack with a minimum lookup '''
def min(self):
return self.minimum
from stack import Stack
class NodeWithMin(object):
def __init__(self, value, minimum):
def __init__(self, value=None, minimum=None):
self.value = value
self.minimum = minimum
class StackMin(Stack):
def __init__(self):
self.items = []
self.minimum = None
def push(self, value):
if self.isEmpty() or self.minimum > value:
self.minimum = value
self.items.append(NodeWithMin(value, self.minimum))
def peek(self):
return self.items[-1].value
def peekMinimum(self):
return self.items[-1].minimum
def pop(self):
item = self.items.pop()
if item:
if item.value == self.minimum:
self.minimum = self.peekMinimum()
return item.value
else:
print("Stack is empty.")
def __repr__(self):
return str(self.value)
aux = []
for i in self.items:
aux.append(i.value)
return '{}'.format(aux)
def min(self):
return self.minimum
def main():
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
node = stack.pop()
print(node.minimum)
stack.push(0)
stack.push(4)
node = stack.pop()
print(node.min())
print(stack.min())
print(stack)
if __name__ == '__main__':
main()
stack = StackMin()
print("Is the stack empty? ", stack.isEmpty())
print("Adding 0 to 10 in the stack...")
for i in range(10,3, -1):
stack.push(i)
print(stack)
print("Stack size: ", stack.size())
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
print("Pop...", stack.pop())
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
print("Is the stack empty? ", stack.isEmpty())
print(stack)
for i in range(5, 1, -1):
stack.push(i)
print(stack)
print("Stack size: ", stack.size())
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
print("Pop...", stack.pop())
print("Stack peek and peekMinimum : ", stack.peek(), stack.peekMinimum())
print("Is the stack empty? ", stack.isEmpty())
print(stack)

View File

@ -1,16 +1,28 @@
from stack import Stack, Node
#!/usr/bin/python
__author__ = "Mari Wahl"
__email__ = "marina.w4hl@gmail.com"
""" Implement the 'towers of hanoi'"""
from linked_stack import Stack, Node
def moveTop(s1, s3):
s3.append(s1.pop())
def moveDisks(n, s1, s3, s2):
if n < 1: return
if n < 1: return
moveDisks(n - 1, s1, s2, s3)
moveTop(s1, s3)
moveDisks(n -1, s2, s3, s1)
def towersOfHanoi(n):
s1 = [x+1 for x in range(n)]
s2 = []
@ -18,9 +30,12 @@ def towersOfHanoi(n):
print('The first stick is {0} and the third stick has {1}'.format(s1, s3))
moveDisks(n, s1, s3, s2)
print('The first stick is {0} and the third stick has {1}'.format(s1, s3))
return s3
if __name__ == '__main__':
towersOfHanoi(6)
towersOfHanoi(6)