Skip to content

[fix bug] Sort_min: the condition to check push second stack has bug #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 15 additions & 29 deletions stacks_queues/stack_min/stack_min_solution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"### Push\n",
"\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",
"\n",
"Complexity:\n",
Expand Down Expand Up @@ -105,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"collapsed": true
},
Expand All @@ -116,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"collapsed": true
},
Expand All @@ -139,7 +139,7 @@
"\n",
" def push(self, 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",
"\n",
" def pop(self):\n",
Expand All @@ -159,19 +159,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_stack_min.py\n"
]
}
],
"outputs": [],
"source": [
"%%writefile test_stack_min.py\n",
"from nose.tools import assert_equal\n",
Expand All @@ -188,6 +180,9 @@
" stack.push(1)\n",
" assert_equal(stack.peek(), 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",
" assert_equal(stack.peek(), 3)\n",
" assert_equal(stack.minimum(), 1)\n",
Expand All @@ -201,6 +196,8 @@
" assert_equal(stack.pop(), 3)\n",
" assert_equal(stack.minimum(), 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.pop(), 5)\n",
" assert_equal(stack.minimum(), sys.maxsize)\n",
Expand All @@ -222,22 +219,11 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"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"
]
}
],
"outputs": [],
"source": [
"run -i test_stack_min.py"
]
Expand All @@ -259,9 +245,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
"version": "3.7.0b4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 2
}
6 changes: 5 additions & 1 deletion stacks_queues/stack_min/test_stack_min.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from nose.tools import assert_equal


Expand All @@ -13,6 +12,9 @@ def test_stack_min(self):
stack.push(1)
assert_equal(stack.peek(), 1)
assert_equal(stack.minimum(), 1)
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)
Expand All @@ -26,6 +28,8 @@ def test_stack_min(self):
assert_equal(stack.pop(), 3)
assert_equal(stack.minimum(), 1)
assert_equal(stack.pop(), 1)
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)
Expand Down