mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
fix bug, when multi same min value pushed, should push to second stack also
This commit is contained in:
parent
e0bde79ddd
commit
cb00e2d87b
@ -77,7 +77,7 @@
|
|||||||
"### Push\n",
|
"### Push\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Push the data\n",
|
"* Push the data\n",
|
||||||
"* If the data is less than min\n",
|
"* If the data is equal or less than min\n",
|
||||||
" * Push data to second stack\n",
|
" * Push data to second stack\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
@ -105,7 +105,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
@ -116,7 +116,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
@ -139,7 +139,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def push(self, data):\n",
|
" def push(self, data):\n",
|
||||||
" super(StackMin, self).push(data)\n",
|
" super(StackMin, self).push(data)\n",
|
||||||
" if data < self.minimum():\n",
|
" if data <= self.minimum():\n",
|
||||||
" self.stack_of_mins.push(data)\n",
|
" self.stack_of_mins.push(data)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def pop(self):\n",
|
" def pop(self):\n",
|
||||||
@ -159,19 +159,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"Overwriting test_stack_min.py\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile test_stack_min.py\n",
|
"%%writefile test_stack_min.py\n",
|
||||||
"from nose.tools import assert_equal\n",
|
"from nose.tools import assert_equal\n",
|
||||||
@ -188,6 +180,9 @@
|
|||||||
" stack.push(1)\n",
|
" stack.push(1)\n",
|
||||||
" assert_equal(stack.peek(), 1)\n",
|
" assert_equal(stack.peek(), 1)\n",
|
||||||
" assert_equal(stack.minimum(), 1)\n",
|
" assert_equal(stack.minimum(), 1)\n",
|
||||||
|
" stack.push(1)\n",
|
||||||
|
" assert_equal(stack.peek(), 1)\n",
|
||||||
|
" assert_equal(stack.minimum(), 1)\n",
|
||||||
" stack.push(3)\n",
|
" stack.push(3)\n",
|
||||||
" assert_equal(stack.peek(), 3)\n",
|
" assert_equal(stack.peek(), 3)\n",
|
||||||
" assert_equal(stack.minimum(), 1)\n",
|
" assert_equal(stack.minimum(), 1)\n",
|
||||||
@ -201,6 +196,8 @@
|
|||||||
" assert_equal(stack.pop(), 3)\n",
|
" assert_equal(stack.pop(), 3)\n",
|
||||||
" assert_equal(stack.minimum(), 1)\n",
|
" assert_equal(stack.minimum(), 1)\n",
|
||||||
" assert_equal(stack.pop(), 1)\n",
|
" assert_equal(stack.pop(), 1)\n",
|
||||||
|
" assert_equal(stack.minimum(), 1)\n",
|
||||||
|
" assert_equal(stack.pop(), 1)\n",
|
||||||
" assert_equal(stack.minimum(), 5)\n",
|
" assert_equal(stack.minimum(), 5)\n",
|
||||||
" assert_equal(stack.pop(), 5)\n",
|
" assert_equal(stack.pop(), 5)\n",
|
||||||
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
" assert_equal(stack.minimum(), sys.maxsize)\n",
|
||||||
@ -222,22 +219,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [
|
"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": [
|
"source": [
|
||||||
"run -i test_stack_min.py"
|
"run -i test_stack_min.py"
|
||||||
]
|
]
|
||||||
@ -259,9 +245,9 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.5.0"
|
"version": "3.7.0b4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 0
|
"nbformat_minor": 2
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import sys
|
|
||||||
from nose.tools import assert_equal
|
from nose.tools import assert_equal
|
||||||
|
|
||||||
|
|
||||||
@ -13,6 +12,9 @@ class TestStackMin(object):
|
|||||||
stack.push(1)
|
stack.push(1)
|
||||||
assert_equal(stack.peek(), 1)
|
assert_equal(stack.peek(), 1)
|
||||||
assert_equal(stack.minimum(), 1)
|
assert_equal(stack.minimum(), 1)
|
||||||
|
stack.push(1)
|
||||||
|
assert_equal(stack.peek(), 1)
|
||||||
|
assert_equal(stack.minimum(), 1)
|
||||||
stack.push(3)
|
stack.push(3)
|
||||||
assert_equal(stack.peek(), 3)
|
assert_equal(stack.peek(), 3)
|
||||||
assert_equal(stack.minimum(), 1)
|
assert_equal(stack.minimum(), 1)
|
||||||
@ -26,6 +28,8 @@ class TestStackMin(object):
|
|||||||
assert_equal(stack.pop(), 3)
|
assert_equal(stack.pop(), 3)
|
||||||
assert_equal(stack.minimum(), 1)
|
assert_equal(stack.minimum(), 1)
|
||||||
assert_equal(stack.pop(), 1)
|
assert_equal(stack.pop(), 1)
|
||||||
|
assert_equal(stack.minimum(), 1)
|
||||||
|
assert_equal(stack.pop(), 1)
|
||||||
assert_equal(stack.minimum(), 5)
|
assert_equal(stack.minimum(), 5)
|
||||||
assert_equal(stack.pop(), 5)
|
assert_equal(stack.pop(), 5)
|
||||||
assert_equal(stack.minimum(), sys.maxsize)
|
assert_equal(stack.minimum(), sys.maxsize)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user