mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Reworked notebook: Added more detail to constraints and test cases. Reworked unit test.
This commit is contained in:
parent
582e3c0181
commit
d6a8ff37e1
|
@ -13,19 +13,24 @@
|
|||
"source": [
|
||||
"## Problem: Implement a queue using two stacks.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)"
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Clarifying Questions\n",
|
||||
"## Constraints and Assumptions\n",
|
||||
"\n",
|
||||
"*Problem statements are often intentionally 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",
|
||||
"* Can we assume we already have a stack class that can be used for this problem?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
|
@ -92,7 +97,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
|
@ -103,7 +108,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -129,32 +134,71 @@
|
|||
" return self.right_stack.pop()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"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.*"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"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": [
|
||||
"print('Dequeue on empty stack')\n",
|
||||
"queue = QueueFromStacks()\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print('Enqueue on empty stack')\n",
|
||||
"print('Enqueue on non-empty stack')\n",
|
||||
"print('Multiple enqueue in a row')\n",
|
||||
"num_items = 3\n",
|
||||
"for i in range (0, num_items):\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" def test_queue_from_stacks(self):\n",
|
||||
" print('Test: Dequeue on empty stack')\n",
|
||||
" queue = QueueFromStacks()\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue on empty stack')\n",
|
||||
" print('Test: Enqueue on non-empty stack')\n",
|
||||
" print('Test: Multiple enqueue in a row')\n",
|
||||
" num_items = 3\n",
|
||||
" for i in range (0, num_items):\n",
|
||||
" queue.enqueue(i)\n",
|
||||
"print('Dequeue on non-empty stack')\n",
|
||||
"print('Dequeue after an enqueue')\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print('Multiple dequeue in a row')\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print('Enqueue after a dequeue')\n",
|
||||
"queue.enqueue(5)\n",
|
||||
"print(queue.dequeue())"
|
||||
"\n",
|
||||
" print('Test: Dequeue on non-empty stack')\n",
|
||||
" print('Test: Dequeue after an enqueue')\n",
|
||||
" assert_equal(queue.dequeue(), 0)\n",
|
||||
"\n",
|
||||
" print('Test: Multiple dequeue in a row')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue after a dequeue')\n",
|
||||
" queue.enqueue(5)\n",
|
||||
" assert_equal(queue.dequeue(), 5)\n",
|
||||
"\n",
|
||||
" print('Success: test_queue_from_stacks')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_queue_from_stacks()"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user