mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-09-01 19:11:47 +00:00
Rename variable min_vals to stack_of_mins to improve clarity. Rename method min to minimum to avoid using the same name as the built-in min.
5.5 KiB
5.5 KiB
In [ ]:
%run ../stack/stack.py
%load ../stack/stack.pyIn [ ]:
import sys
class StackMin(Stack):
def __init__(self, top=None):
# TODO: Implement me
pass
def minimum(self):
# TODO: Implement me
pass
def push(self, data):
# TODO: Implement me
pass
def pop(self):
# TODO: Implement me
passIn [ ]:
# %load test_stack_min.py
from nose.tools import assert_equal
class TestStackMin(object):
def test_stack_min(self):
print('Test: Push on empty stack, non-empty stack')
stack = StackMin()
stack.push(5)
assert_equal(stack.peek(), 5)
assert_equal(stack.minimum(), 5)
stack.push(1)
assert_equal(stack.peek(), 1)
assert_equal(stack.minimum(), 1)
stack.push(3)
assert_equal(stack.peek(), 3)
assert_equal(stack.minimum(), 1)
stack.push(0)
assert_equal(stack.peek(), 0)
assert_equal(stack.minimum(), 0)
print('Test: Pop on non-empty stack')
assert_equal(stack.pop(), 0)
assert_equal(stack.minimum(), 1)
assert_equal(stack.pop(), 3)
assert_equal(stack.minimum(), 1)
assert_equal(stack.pop(), 1)
assert_equal(stack.minimum(), 5)
assert_equal(stack.pop(), 5)
assert_equal(stack.minimum(), sys.maxsize)
print('Test: Pop empty stack')
assert_equal(stack.pop(), None)
print('Success: test_stack_min')
def main():
test = TestStackMin()
test.test_stack_min()
if __name__ == '__main__':
main()