diff options
Diffstat (limited to 'challenge-095/roger-bell-west/python/ch-2.py')
| -rwxr-xr-x | challenge-095/roger-bell-west/python/ch-2.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-095/roger-bell-west/python/ch-2.py b/challenge-095/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..8b3b63b97f --- /dev/null +++ b/challenge-095/roger-bell-west/python/ch-2.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 +import unittest + +class Stack: + + def __init__(self): + self.stack=list() + + def push(self,op): + self.stack.append(op) + + def pop(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for pop") + return self.stack.pop() + + def top(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for top") + return self.stack[len(self.stack)-1] + + def min(self): + if len(self.stack)==0: + raise Exception("Insufficient elements for min") + return min(self.stack) + +class TestStack(unittest.TestCase): + + def test_ex1(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + self.assertEqual(stack.pop(),0,'example 1') + + def test_ex2(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + self.assertEqual(stack.top(),-1,'example 2') + + def test_ex3(self): + stack=Stack() + stack.push(2) + stack.push(-1) + stack.push(0) + stack.pop() + stack.push(0) + self.assertEqual(stack.min(),-1,'example 3') + +unittest.main() |
