This commit is contained in:
bt3gl 2023-07-30 21:40:09 -07:00
parent 1d44d182e2
commit a85ed914d3
320 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,30 @@
#!/usr/bin/env python
__author__ = "bt3"
'''transform a decimal number to a binary number with a stack '''
from stack import Stack
def dec2bin_with_stack(decnum):
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
if __name__ == '__main__':
decnum = 9
assert(dec2bin_with_stack(decnum) == '1001')

View file

@ -0,0 +1,71 @@
#!/usr/bin/env python
__author__ = "bt3"
""" 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.head = None
def isEmpty(self):
return not bool(self.head)
def push(self, item):
self.head = Node(item, self.head)
def pop(self):
if self.head:
node = self.head
self.head = node.pointer
return node.value
else:
print('Stack is empty.')
def peek(self):
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__':
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

@ -0,0 +1,31 @@
#!/usr/bin/env python
__author__ = "bt3"
''' Uses a stack to reverse a string '''
from stack import Stack
def reverse_string_with_stack(str1):
s = Stack()
revStr = ''
for c in str1:
s.push(c)
while not s.isEmpty():
revStr += s.pop()
return revStr
if __name__ == '__main__':
str1 = 'Buffy is a Slayer!'
print(str1)
print(reverse_string_with_stack(str1))

View file

@ -0,0 +1,56 @@
#!/usr/bin/env python
__author__ = "bt3"
""" define a class for a set of stacks """
from stack import Stack
class SetOfStacks(Stack):
def __init__(self, capacity=4):
self.setofstacks = []
self.items = []
self.capacity = capacity
def push(self, value):
if self.size() >= self.capacity:
self.setofstacks.append(self.items)
self.items = []
self.items.append(value)
def pop(self):
value = self.items.pop()
if self.isEmpty() and self.setofstacks:
self.items = self.setofstacks.pop()
return value
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__':
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

@ -0,0 +1,65 @@
#!/usr/bin/env python
# copy of the class ../Stack.py
__author__ = "bt3"
class Stack(object):
def __init__(self):
self.content = []
self.min_array = []
self.min = float('inf')
def push(self, value):
if value < self.min:
self.min = value
self.content.append(value)
self.min_array.append(self.min)
def pop(self):
if self.content:
value = self.content.pop()
self.min_array.pop()
if self.min_array:
self.min = self.min_array[-1]
return value
else:
return 'Empty List. '
def find_min(self):
if self.min_array:
return self.min_array[-1]
else:
return 'No min value for empty list.'
def size(self):
return len(self.content)
def isEmpty(self):
return not bool(self.content)
def peek(self):
if self.content:
return self.content[-1]
else:
print('Stack is empty.')
def __repr__(self):
return '{}'.format(self.content)
if __name__ == '__main__':
q = Stack()
for i in range(15,20):
q.push(i)
for i in range(10,5,-1):
q.push(i)
for i in range(1, 13):
print q.pop(), q.find_min()

View file

@ -0,0 +1,77 @@
#!/usr/bin/env python
__author__ = "bt3"
''' A stack with a minimum lookup '''
from stack import Stack
class NodeWithMin(object):
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):
aux = []
for i in self.items:
aux.append(i.value)
return '{}'.format(aux)
if __name__ == '__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

@ -0,0 +1,41 @@
#!/usr/bin/env python
__author__ = "bt3"
""" 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
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 = []
s3 = []
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)