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. Added linebreak to comment about naming clash with queue.
This commit is contained in:
parent
dd0e94fdd1
commit
0c43a34ae1
|
@ -13,10 +13,11 @@
|
|||
"source": [
|
||||
"## Problem: Implement a queue with enqueue and dequeue methods using a linked list.\n",
|
||||
"\n",
|
||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
||||
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
"* [Unit Test](#Unit-Test)\n",
|
||||
"* [Pythonic-Code](#Pythonic-Code)"
|
||||
]
|
||||
},
|
||||
|
@ -24,7 +25,9 @@
|
|||
"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",
|
||||
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\n",
|
||||
" * Yes\n",
|
||||
|
@ -91,14 +94,23 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting queue_list.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile queue_list.py\n",
|
||||
"# Need to avoid naming this queue.py as it will conflict with IPython Notebook\n",
|
||||
"# Need to avoid naming this queue.py as it \n",
|
||||
"# will conflict with IPython Notebook\n",
|
||||
"\n",
|
||||
"class Node(object):\n",
|
||||
" def __init__(self, data):\n",
|
||||
|
@ -123,6 +135,7 @@
|
|||
" # Empty list\n",
|
||||
" if self.first is None and self.last is None:\n",
|
||||
" return None\n",
|
||||
" \n",
|
||||
" # Remove only element from a one element list\n",
|
||||
" elif self.first == self.last:\n",
|
||||
" data = self.first.data\n",
|
||||
|
@ -137,7 +150,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -146,29 +159,67 @@
|
|||
"%run queue_list.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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 an empty queue\n",
|
||||
"Test: Enqueue to an empty queue\n",
|
||||
"Test: Dequeue a queue with one element\n",
|
||||
"Test: Enqueue to a non-empty queue\n",
|
||||
"Test: Dequeue a queue with more than one element\n",
|
||||
"Success: test_end_to_end\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print('Dequeue an empty queue')\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" # TODO: It would be better if we had unit tests for each\n",
|
||||
" # method in addition to the following end-to-end test\n",
|
||||
" def test_end_to_end(self):\n",
|
||||
" print('Test: Dequeue an empty queue')\n",
|
||||
" queue = Queue()\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print('Enqueue to an empty queue')\n",
|
||||
" assert_equal(queue.dequeue(), None)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to an empty queue')\n",
|
||||
" queue.enqueue(1)\n",
|
||||
"print('Dequeue a queue with one element')\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print('Enqueue to a non-empty queue')\n",
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with one element')\n",
|
||||
" assert_equal(queue.dequeue(), 1)\n",
|
||||
"\n",
|
||||
" print('Test: Enqueue to a non-empty queue')\n",
|
||||
" queue.enqueue(2)\n",
|
||||
" queue.enqueue(3)\n",
|
||||
" queue.enqueue(4)\n",
|
||||
"print('Dequeue a queue with more than one element')\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print(queue.dequeue())\n",
|
||||
"print(queue.dequeue())"
|
||||
"\n",
|
||||
" print('Test: Dequeue a queue with more than one element')\n",
|
||||
" assert_equal(queue.dequeue(), 2)\n",
|
||||
" assert_equal(queue.dequeue(), 3)\n",
|
||||
" assert_equal(queue.dequeue(), 4)\n",
|
||||
" \n",
|
||||
" print('Success: test_end_to_end')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_end_to_end()"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# Need to avoid naming this queue.py as it will conflict with IPython Notebook
|
||||
# Need to avoid naming this queue.py as it
|
||||
# will conflict with IPython Notebook
|
||||
|
||||
class Node(object):
|
||||
def __init__(self, data):
|
||||
|
|
Loading…
Reference in New Issue
Block a user