Polish stack min challenge and solution (#72)

Update constraints, algorithm discussion, code, and tests.
This commit is contained in:
Donne Martin
2016-06-20 06:54:08 -04:00
committed by GitHub
parent 98ad50edaf
commit e71b280082
3 changed files with 28 additions and 20 deletions

View File

@@ -36,9 +36,13 @@
"\n",
"* Can we assume this is a stack of ints?\n",
" * Yes\n",
"* If we call this function on an empty stack, can we return maxsize?\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"
]
},
@@ -91,7 +95,7 @@
"import sys\n",
"\n",
"\n",
"class MyStack(Stack):\n",
"class StackMin(Stack):\n",
"\n",
" def __init__(self, top=None):\n",
" # TODO: Implement me\n",
@@ -137,7 +141,7 @@
"\n",
" def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n",
" stack = MyStack()\n",
" stack = StackMin()\n",
" stack.push(5)\n",
" assert_equal(stack.peek(), 5)\n",
" assert_equal(stack.min(), 5)\n",
@@ -188,21 +192,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@@ -35,9 +35,13 @@
"\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"
]
},
@@ -76,7 +80,7 @@
"\n",
"Complexity:\n",
"* Time: O(1)\n",
"* Space: O(n)\n",
"* Space: O(1)\n",
"\n",
"### Pop\n",
"\n",
@@ -119,11 +123,11 @@
"import sys\n",
"\n",
"\n",
"class MyStack(Stack):\n",
"class StackMin(Stack):\n",
"\n",
" def __init__(self, top=None):\n",
" super(StackMin, self).__init__(top)\n",
" self.min_vals = Stack()\n",
" super(MyStack, self).__init__(top)\n",
"\n",
" def min(self):\n",
" if self.min_vals.top is None:\n",
@@ -132,12 +136,12 @@
" return self.min_vals.peek()\n",
"\n",
" def push(self, data):\n",
" super(MyStack, self).push(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(MyStack, self).pop()\n",
" data = super(StackMin, self).pop()\n",
" if data == self.min():\n",
" self.min_vals.pop()\n",
" return data"
@@ -175,7 +179,7 @@
"\n",
" def test_stack_min(self):\n",
" print('Test: Push on empty stack, non-empty stack')\n",
" stack = MyStack()\n",
" stack = StackMin()\n",
" stack.push(5)\n",
" assert_equal(stack.peek(), 5)\n",
" assert_equal(stack.min(), 5)\n",
@@ -239,21 +243,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@@ -5,7 +5,7 @@ class TestStackMin(object):
def test_stack_min(self):
print('Test: Push on empty stack, non-empty stack')
stack = MyStack()
stack = StackMin()
stack.push(5)
assert_equal(stack.peek(), 5)
assert_equal(stack.min(), 5)