2015-05-17 17:33:48 +08:00
{
" cells " : [
2015-06-18 04:39:43 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2016-07-31 20:11:18 +08:00
" This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). "
2015-07-03 11:06:09 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" # Solution Notebook "
2015-06-18 04:39:43 +08:00
]
} ,
2015-05-17 17:33:48 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Problem: Implement a stack with push, pop, and min methods running O(1) time. \n " ,
" \n " ,
2015-06-30 17:55:58 +08:00
" * [Constraints](#Constraints) \n " ,
2015-05-17 17:33:48 +08:00
" * [Test Cases](#Test-Cases) \n " ,
" * [Algorithm](#Algorithm) \n " ,
2015-06-27 18:34:57 +08:00
" * [Code](#Code) \n " ,
" * [Unit Test](#Unit-Test) "
2015-05-17 17:33:48 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
2015-06-28 19:13:27 +08:00
" ## Constraints \n " ,
2015-05-17 17:33:48 +08:00
" \n " ,
2015-06-27 18:34:57 +08:00
" * Can we assume this is a stack of ints? \n " ,
" * Yes \n " ,
2016-06-20 18:54:08 +08:00
" * Can we assume the input values for push are valid? \n " ,
" * Yes \n " ,
2015-07-06 18:35:46 +08:00
" * If we call this function on an empty stack, can we return sys.maxsize? \n " ,
2015-07-03 11:06:09 +08:00
" * Yes \n " ,
2015-06-27 18:34:57 +08:00
" * Can we assume we already have a stack class that can be used for this problem? \n " ,
2016-06-20 18:54:08 +08:00
" * Yes \n " ,
" * Can we assume this fits memory? \n " ,
2015-05-17 17:33:48 +08:00
" * Yes "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Test Cases \n " ,
" \n " ,
" * Push/pop on empty stack \n " ,
2016-10-29 21:08:32 +08:00
" * Push/pop on non-empty stack \n " ,
" * Min on empty stack \n " ,
" * Min on non-tempty stack "
2015-05-17 17:33:48 +08:00
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Algorithm \n " ,
" \n " ,
" We ' ll use a second stack to keep track of the minimum values. \n " ,
" \n " ,
" ### Min \n " ,
" \n " ,
2015-06-27 18:34:57 +08:00
" * If the second stack is empty, return an error code (max int value) \n " ,
2015-05-17 17:33:48 +08:00
" * Else, return the top of the stack, without popping it \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) \n " ,
" \n " ,
" ### Push \n " ,
" \n " ,
" * Push the data \n " ,
" * If the data is less than min \n " ,
" * Push data to second stack \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
2016-06-20 18:54:08 +08:00
" * Space: O(1) \n " ,
2015-05-17 17:33:48 +08:00
" \n " ,
" ### Pop \n " ,
" \n " ,
" * Pop the data \n " ,
" * If the data is equal to min \n " ,
" * Pop the top of the second stack \n " ,
" * Return the data \n " ,
" \n " ,
" Complexity: \n " ,
" * Time: O(1) \n " ,
" * Space: O(1) "
]
} ,
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Code "
]
} ,
{
" cell_type " : " code " ,
2015-06-27 18:34:57 +08:00
" execution_count " : 1 ,
2015-05-17 17:33:48 +08:00
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
2015-07-03 11:06:09 +08:00
" %r un ../stack/stack.py "
2015-05-17 17:33:48 +08:00
]
} ,
{
" cell_type " : " code " ,
2015-06-27 18:34:57 +08:00
" execution_count " : 2 ,
2015-05-17 17:33:48 +08:00
" metadata " : {
" collapsed " : true
} ,
" outputs " : [ ] ,
" source " : [
" import sys \n " ,
" \n " ,
2015-07-03 11:06:09 +08:00
" \n " ,
2016-06-20 18:54:08 +08:00
" class StackMin(Stack): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-05-17 17:33:48 +08:00
" def __init__(self, top=None): \n " ,
2016-06-20 18:54:08 +08:00
" super(StackMin, self).__init__(top) \n " ,
2016-12-08 18:32:13 +08:00
" self.stack_of_mins = Stack() \n " ,
2015-05-17 17:33:48 +08:00
" \n " ,
2016-12-08 18:32:13 +08:00
" def minimum(self): \n " ,
" if self.stack_of_mins.top is None: \n " ,
2015-07-06 18:35:46 +08:00
" return sys.maxsize \n " ,
2015-05-17 17:33:48 +08:00
" else: \n " ,
2016-12-08 18:32:13 +08:00
" return self.stack_of_mins.peek() \n " ,
2015-05-17 17:33:48 +08:00
" \n " ,
" def push(self, data): \n " ,
2016-06-20 18:54:08 +08:00
" super(StackMin, self).push(data) \n " ,
2016-12-08 18:32:13 +08:00
" if data < self.minimum(): \n " ,
" self.stack_of_mins.push(data) \n " ,
2015-05-17 17:33:48 +08:00
" \n " ,
" def pop(self): \n " ,
2016-06-20 18:54:08 +08:00
" data = super(StackMin, self).pop() \n " ,
2016-12-08 18:32:13 +08:00
" if data == self.minimum(): \n " ,
" self.stack_of_mins.pop() \n " ,
2015-05-17 17:33:48 +08:00
" return data "
]
} ,
2015-06-27 18:34:57 +08:00
{
" cell_type " : " markdown " ,
" metadata " : { } ,
" source " : [
" ## Unit Test \n " ,
2015-07-03 11:06:09 +08:00
" \n "
2015-06-27 18:34:57 +08:00
]
} ,
2015-05-17 17:33:48 +08:00
{
" cell_type " : " code " ,
2015-06-27 18:34:57 +08:00
" execution_count " : 3 ,
2015-05-17 17:33:48 +08:00
" metadata " : {
" collapsed " : false
} ,
2015-06-27 18:34:57 +08:00
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
2015-07-03 11:06:09 +08:00
" Overwriting test_stack_min.py \n "
2015-06-27 18:34:57 +08:00
]
}
] ,
2015-05-17 17:33:48 +08:00
" source " : [
2015-07-03 11:06:09 +08:00
" %% writefile test_stack_min.py \n " ,
2015-06-27 18:34:57 +08:00
" from nose.tools import assert_equal \n " ,
" \n " ,
2015-07-03 11:06:09 +08:00
" \n " ,
" class TestStackMin(object): \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:34:57 +08:00
" def test_stack_min(self): \n " ,
" print( ' Test: Push on empty stack, non-empty stack ' ) \n " ,
2016-06-20 18:54:08 +08:00
" stack = StackMin() \n " ,
2015-06-27 18:34:57 +08:00
" stack.push(5) \n " ,
" assert_equal(stack.peek(), 5) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 5) \n " ,
2015-06-27 18:34:57 +08:00
" stack.push(1) \n " ,
" assert_equal(stack.peek(), 1) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 1) \n " ,
2015-06-27 18:34:57 +08:00
" stack.push(3) \n " ,
" assert_equal(stack.peek(), 3) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 1) \n " ,
2015-06-27 18:34:57 +08:00
" stack.push(0) \n " ,
" assert_equal(stack.peek(), 0) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 0) \n " ,
2015-06-27 18:34:57 +08:00
" \n " ,
" print( ' Test: Pop on non-empty stack ' ) \n " ,
" assert_equal(stack.pop(), 0) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 1) \n " ,
2015-06-27 18:34:57 +08:00
" assert_equal(stack.pop(), 3) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 1) \n " ,
2015-06-27 18:34:57 +08:00
" assert_equal(stack.pop(), 1) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), 5) \n " ,
2015-06-27 18:34:57 +08:00
" assert_equal(stack.pop(), 5) \n " ,
2016-12-08 18:32:13 +08:00
" assert_equal(stack.minimum(), sys.maxsize) \n " ,
2015-06-27 18:34:57 +08:00
" \n " ,
" print( ' Test: Pop empty stack ' ) \n " ,
" assert_equal(stack.pop(), None) \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:34:57 +08:00
" print( ' Success: test_stack_min ' ) \n " ,
" \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-07-03 11:06:09 +08:00
" def main(): \n " ,
" test = TestStackMin() \n " ,
" test.test_stack_min() \n " ,
" \n " ,
2015-07-12 03:39:59 +08:00
" \n " ,
2015-06-27 18:34:57 +08:00
" if __name__ == ' __main__ ' : \n " ,
2015-07-03 11:06:09 +08:00
" main() "
]
} ,
{
" cell_type " : " code " ,
" execution_count " : 4 ,
" metadata " : {
" collapsed " : false
} ,
" outputs " : [
{
" name " : " stdout " ,
" output_type " : " stream " ,
" text " : [
" Test: Push on empty stack, non-empty stack \n " ,
" Test: Pop on non-empty stack \n " ,
" Test: Pop empty stack \n " ,
" Success: test_stack_min \n "
]
}
] ,
" source " : [
" run -i test_stack_min.py "
2015-05-17 17:33:48 +08:00
]
}
] ,
" metadata " : {
" kernelspec " : {
2016-06-20 18:54:08 +08:00
" display_name " : " Python 3 " ,
2015-05-17 17:33:48 +08:00
" language " : " python " ,
2016-06-20 18:54:08 +08:00
" name " : " python3 "
2015-05-17 17:33:48 +08:00
} ,
" language_info " : {
" codemirror_mode " : {
" name " : " ipython " ,
2016-06-20 18:54:08 +08:00
" version " : 3
2015-05-17 17:33:48 +08:00
} ,
" file_extension " : " .py " ,
" mimetype " : " text/x-python " ,
" name " : " python " ,
" nbconvert_exporter " : " python " ,
2016-06-20 18:54:08 +08:00
" pygments_lexer " : " ipython3 " ,
" version " : " 3.5.0 "
2015-05-17 17:33:48 +08:00
}
} ,
" nbformat " : 4 ,
" nbformat_minor " : 0
}