mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
268 lines
6.3 KiB
Python
268 lines
6.3 KiB
Python
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Solution Notebook"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem: Implement a stack with push, pop, and min methods running O(1) time.\n",
|
|
"\n",
|
|
"* [Constraints](#Constraints)\n",
|
|
"* [Test Cases](#Test-Cases)\n",
|
|
"* [Algorithm](#Algorithm)\n",
|
|
"* [Code](#Code)\n",
|
|
"* [Unit Test](#Unit-Test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Constraints\n",
|
|
"\n",
|
|
"* Can we assume this is a stack of ints?\n",
|
|
" * Yes\n",
|
|
"* Can we assume the input values for push are valid?\n",
|
|
" * Yes\n",
|
|
"* If we call this function on an empty stack, can we return sys.maxsize?\n",
|
|
" * Yes\n",
|
|
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
|
" * Yes\n",
|
|
"* Can we assume this fits memory?\n",
|
|
" * Yes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test Cases\n",
|
|
"\n",
|
|
"* Push/pop on empty stack\n",
|
|
"* Push/pop on non-empty stack\n",
|
|
"* Min on empty stack\n",
|
|
"* Min on non-tempty stack"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"* If the second stack is empty, return an error code (max int value)\n",
|
|
"* 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",
|
|
"* Space: O(1)\n",
|
|
"\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",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%run ../stack/stack.py"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"\n",
|
|
"\n",
|
|
"class StackMin(Stack):\n",
|
|
"\n",
|
|
" def __init__(self, top=None):\n",
|
|
" super(StackMin, self).__init__(top)\n",
|
|
" self.min_vals = Stack()\n",
|
|
"\n",
|
|
" def min(self):\n",
|
|
" if self.min_vals.top is None:\n",
|
|
" return sys.maxsize\n",
|
|
" else:\n",
|
|
" return self.min_vals.peek()\n",
|
|
"\n",
|
|
" def push(self, data):\n",
|
|
" super(StackMin, self).push(data)\n",
|
|
" if data < self.min():\n",
|
|
" self.min_vals.push(data)\n",
|
|
"\n",
|
|
" def pop(self):\n",
|
|
" data = super(StackMin, self).pop()\n",
|
|
" if data == self.min():\n",
|
|
" self.min_vals.pop()\n",
|
|
" return data"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Overwriting test_stack_min.py\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%writefile test_stack_min.py\n",
|
|
"from nose.tools import assert_equal\n",
|
|
"\n",
|
|
"\n",
|
|
"class TestStackMin(object):\n",
|
|
"\n",
|
|
" def test_stack_min(self):\n",
|
|
" print('Test: Push on empty stack, non-empty stack')\n",
|
|
" stack = StackMin()\n",
|
|
" stack.push(5)\n",
|
|
" assert_equal(stack.peek(), 5)\n",
|
|
" assert_equal(stack.min(), 5)\n",
|
|
" stack.push(1)\n",
|
|
" assert_equal(stack.peek(), 1)\n",
|
|
" assert_equal(stack.min(), 1)\n",
|
|
" stack.push(3)\n",
|
|
" assert_equal(stack.peek(), 3)\n",
|
|
" assert_equal(stack.min(), 1)\n",
|
|
" stack.push(0)\n",
|
|
" assert_equal(stack.peek(), 0)\n",
|
|
" assert_equal(stack.min(), 0)\n",
|
|
"\n",
|
|
" print('Test: Pop on non-empty stack')\n",
|
|
" assert_equal(stack.pop(), 0)\n",
|
|
" assert_equal(stack.min(), 1)\n",
|
|
" assert_equal(stack.pop(), 3)\n",
|
|
" assert_equal(stack.min(), 1)\n",
|
|
" assert_equal(stack.pop(), 1)\n",
|
|
" assert_equal(stack.min(), 5)\n",
|
|
" assert_equal(stack.pop(), 5)\n",
|
|
" assert_equal(stack.min(), sys.maxsize)\n",
|
|
"\n",
|
|
" print('Test: Pop empty stack')\n",
|
|
" assert_equal(stack.pop(), None)\n",
|
|
"\n",
|
|
" print('Success: test_stack_min')\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" test = TestStackMin()\n",
|
|
" test.test_stack_min()\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" 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"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.5.0"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|