mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-05 00:48:03 +00:00
Polish stack min challenge and solution (#72)
Update constraints, algorithm discussion, code, and tests.
This commit is contained in:
@@ -36,9 +36,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"* Can we assume this is a stack of ints?\n",
|
"* Can we assume this is a stack of ints?\n",
|
||||||
" * Yes\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",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -91,7 +95,7 @@
|
|||||||
"import sys\n",
|
"import sys\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class MyStack(Stack):\n",
|
"class StackMin(Stack):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
@@ -137,7 +141,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_stack_min(self):\n",
|
" def test_stack_min(self):\n",
|
||||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||||
" stack = MyStack()\n",
|
" stack = StackMin()\n",
|
||||||
" stack.push(5)\n",
|
" stack.push(5)\n",
|
||||||
" assert_equal(stack.peek(), 5)\n",
|
" assert_equal(stack.peek(), 5)\n",
|
||||||
" assert_equal(stack.min(), 5)\n",
|
" assert_equal(stack.min(), 5)\n",
|
||||||
@@ -188,21 +192,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -35,9 +35,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"* Can we assume this is a stack of ints?\n",
|
"* Can we assume this is a stack of ints?\n",
|
||||||
" * Yes\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",
|
"* If we call this function on an empty stack, can we return sys.maxsize?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we already have a stack class that can be used for this problem?\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"
|
" * Yes"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
@@ -76,7 +80,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(1)\n",
|
"* Time: O(1)\n",
|
||||||
"* Space: O(n)\n",
|
"* Space: O(1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Pop\n",
|
"### Pop\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -119,11 +123,11 @@
|
|||||||
"import sys\n",
|
"import sys\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class MyStack(Stack):\n",
|
"class StackMin(Stack):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, top=None):\n",
|
" def __init__(self, top=None):\n",
|
||||||
|
" super(StackMin, self).__init__(top)\n",
|
||||||
" self.min_vals = Stack()\n",
|
" self.min_vals = Stack()\n",
|
||||||
" super(MyStack, self).__init__(top)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" def min(self):\n",
|
" def min(self):\n",
|
||||||
" if self.min_vals.top is None:\n",
|
" if self.min_vals.top is None:\n",
|
||||||
@@ -132,12 +136,12 @@
|
|||||||
" return self.min_vals.peek()\n",
|
" return self.min_vals.peek()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def push(self, data):\n",
|
" def push(self, data):\n",
|
||||||
" super(MyStack, self).push(data)\n",
|
" super(StackMin, self).push(data)\n",
|
||||||
" if data < self.min():\n",
|
" if data < self.min():\n",
|
||||||
" self.min_vals.push(data)\n",
|
" self.min_vals.push(data)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def pop(self):\n",
|
" def pop(self):\n",
|
||||||
" data = super(MyStack, self).pop()\n",
|
" data = super(StackMin, self).pop()\n",
|
||||||
" if data == self.min():\n",
|
" if data == self.min():\n",
|
||||||
" self.min_vals.pop()\n",
|
" self.min_vals.pop()\n",
|
||||||
" return data"
|
" return data"
|
||||||
@@ -175,7 +179,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def test_stack_min(self):\n",
|
" def test_stack_min(self):\n",
|
||||||
" print('Test: Push on empty stack, non-empty stack')\n",
|
" print('Test: Push on empty stack, non-empty stack')\n",
|
||||||
" stack = MyStack()\n",
|
" stack = StackMin()\n",
|
||||||
" stack.push(5)\n",
|
" stack.push(5)\n",
|
||||||
" assert_equal(stack.peek(), 5)\n",
|
" assert_equal(stack.peek(), 5)\n",
|
||||||
" assert_equal(stack.min(), 5)\n",
|
" assert_equal(stack.min(), 5)\n",
|
||||||
@@ -239,21 +243,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ class TestStackMin(object):
|
|||||||
|
|
||||||
def test_stack_min(self):
|
def test_stack_min(self):
|
||||||
print('Test: Push on empty stack, non-empty stack')
|
print('Test: Push on empty stack, non-empty stack')
|
||||||
stack = MyStack()
|
stack = StackMin()
|
||||||
stack.push(5)
|
stack.push(5)
|
||||||
assert_equal(stack.peek(), 5)
|
assert_equal(stack.peek(), 5)
|
||||||
assert_equal(stack.min(), 5)
|
assert_equal(stack.min(), 5)
|
||||||
|
|||||||
Reference in New Issue
Block a user