Skip to content

Instantly share code, notes, and snippets.

@icaoberg
Created June 21, 2012 22:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icaoberg/2969086 to your computer and use it in GitHub Desktop.
Save icaoberg/2969086 to your computer and use it in GitHub Desktop.
Simple stack in Python
# Author: Michelle Mackie
#
# Copyright (C) Summer 2012
# School of Computer Science
# Carnegie Mellon University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# For additional information visit http://www.andrew.cmu.edu/~icaoberg or
# send email to icaoberg@cmu.edu
#This defines a simple stack in Python
#adapted from https://gist.github.com/2871311
class Stack:
def __init__(self):
self.stack = list()
def push(self, value):
if value == None:
return False
else:
self.stack.insert(0, value)
return True
def peek(self):
return self.stack[0]
def pop(self):
if self.stack:
self.stack.pop(0)
return True
else:
return False
def size(self):
return len(self.stack)
def has(self, value):
if value in self.stack:
return True
else:
return False
def clear(self):
self.stack = list()
# Author: Michelle Mackie
#
# Copyright (C) Summer 2012
# School of Computer Science
# Carnegie Mellon University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# For additional information visit http://www.andrew.cmu.edu/~icaoberg or
# send email to icaoberg@cmu.edu
#Simple unit tests for the simple stack
import stack
import unittest
class TestStack(unittest.TestCase):
def testPush(self):
s = stack.Stack()
for value in range(1,101):
self.assertEqual(True, s.push(value))
def testPop(self):
s = stack.Stack()
for value in range(1, 11):
self.assertEqual(True, s.push(value))
self.assertEqual(True, s.pop())
self.assertEqual(False, s.pop())
def testPeek(self):
s = stack.Stack()
for value in range(1, 11):
self.assertEqual(True, s.push(value))
self.assertEqual(value, s.peek())
def testHas(self):
s = stack.Stack()
for value in range(1, 11):
self.assertEqual(True, s.push(value))
self.assertEqual(True, s.has(value))
self.assertEqual(False, s.has(value+1))
def testSize(self):
s = stack.Stack()
for value in range(1, 11):
self.assertEqual(True, s.push(value))
self.assertEqual(value, s.size())
self.assertEqual(False, s.has(value+1))
def testClear(self):
s = stack.Stack()
for value in range(1, 11):
self.assertEqual(True, s.push(value))
self.assertEqual(10, s.size())
s.clear()
self.assertEqual(0, s.size())
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment