mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-03-04 14:48:45 +00:00
Added queue from stacks challenge.
This commit is contained in:
@@ -4,7 +4,14 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
||||
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/coding-challenges).</i></small>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Solution Notebook"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -26,7 +33,7 @@
|
||||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
|
||||
"\n",
|
||||
"* Do you expect the methods to be enqueue and dequeue?\n",
|
||||
" * Yes\n",
|
||||
@@ -99,11 +106,11 @@
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run stack.py"
|
||||
"%run ../stack/stack.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -115,6 +122,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class QueueFromStacks(object):\n",
|
||||
" \n",
|
||||
" def __init__(self):\n",
|
||||
" self.left_stack = Stack()\n",
|
||||
" self.right_stack = Stack()\n",
|
||||
@@ -139,8 +147,7 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test\n",
|
||||
"\n",
|
||||
"*It is important to identify and run through general and edge cases from the [Test Cases](#Test-Cases) section by hand. You generally will not be asked to write a unit test like what is shown below.*"
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -154,22 +161,17 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: Dequeue on empty stack\n",
|
||||
"Test: Enqueue on empty stack\n",
|
||||
"Test: Enqueue on non-empty stack\n",
|
||||
"Test: Multiple enqueue in a row\n",
|
||||
"Test: Dequeue on non-empty stack\n",
|
||||
"Test: Dequeue after an enqueue\n",
|
||||
"Test: Multiple dequeue in a row\n",
|
||||
"Test: Enqueue after a dequeue\n",
|
||||
"Success: test_queue_from_stacks\n"
|
||||
"Overwriting test_queue_from_stacks.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_queue_from_stacks.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
"\n",
|
||||
"class TestQueueFromStacks(object):\n",
|
||||
" \n",
|
||||
" def test_queue_from_stacks(self):\n",
|
||||
" print('Test: Dequeue on empty stack')\n",
|
||||
" queue = QueueFromStacks()\n",
|
||||
@@ -196,9 +198,39 @@
|
||||
"\n",
|
||||
" print('Success: test_queue_from_stacks')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestQueueFromStacks()\n",
|
||||
" test.test_queue_from_stacks()\n",
|
||||
" \n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_queue_from_stacks()"
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: Dequeue on empty stack\n",
|
||||
"Test: Enqueue on empty stack\n",
|
||||
"Test: Enqueue on non-empty stack\n",
|
||||
"Test: Multiple enqueue in a row\n",
|
||||
"Test: Dequeue on non-empty stack\n",
|
||||
"Test: Dequeue after an enqueue\n",
|
||||
"Test: Multiple dequeue in a row\n",
|
||||
"Test: Enqueue after a dequeue\n",
|
||||
"Success: test_queue_from_stacks\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%run -i test_queue_from_stacks.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user