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": [
|
"source": [
|
||||||
"## Problem: Implement a queue with enqueue and dequeue methods using a linked list.\n",
|
"## Problem: Implement a queue with enqueue and dequeue methods using a linked list.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* [Clarifying Questions](#Clarifying-Questions)\n",
|
"* [Constraints and Assumptions](#Constraints-and-Assumptions)\n",
|
||||||
"* [Test Cases](#Test-Cases)\n",
|
"* [Test Cases](#Test-Cases)\n",
|
||||||
"* [Algorithm](#Algorithm)\n",
|
"* [Algorithm](#Algorithm)\n",
|
||||||
"* [Code](#Code)\n",
|
"* [Code](#Code)\n",
|
||||||
|
"* [Unit Test](#Unit-Test)\n",
|
||||||
"* [Pythonic-Code](#Pythonic-Code)"
|
"* [Pythonic-Code](#Pythonic-Code)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -24,7 +25,9 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"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",
|
"\n",
|
||||||
"* If there is one item in the list, do you expect the first and last pointers to both point to it?\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",
|
" * Yes\n",
|
||||||
|
@ -91,14 +94,23 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 1,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting queue_list.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"%%writefile queue_list.py\n",
|
"%%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",
|
"\n",
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
|
@ -123,6 +135,7 @@
|
||||||
" # Empty list\n",
|
" # Empty list\n",
|
||||||
" if self.first is None and self.last is None:\n",
|
" if self.first is None and self.last is None:\n",
|
||||||
" return None\n",
|
" return None\n",
|
||||||
|
" \n",
|
||||||
" # Remove only element from a one element list\n",
|
" # Remove only element from a one element list\n",
|
||||||
" elif self.first == self.last:\n",
|
" elif self.first == self.last:\n",
|
||||||
" data = self.first.data\n",
|
" data = self.first.data\n",
|
||||||
|
@ -137,7 +150,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -146,29 +159,67 @@
|
||||||
"%run queue_list.py"
|
"%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",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"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": [
|
"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",
|
" queue = Queue()\n",
|
||||||
"print(queue.dequeue())\n",
|
" assert_equal(queue.dequeue(), None)\n",
|
||||||
"print('Enqueue to an empty queue')\n",
|
"\n",
|
||||||
|
" print('Test: Enqueue to an empty queue')\n",
|
||||||
" queue.enqueue(1)\n",
|
" queue.enqueue(1)\n",
|
||||||
"print('Dequeue a queue with one element')\n",
|
"\n",
|
||||||
"print(queue.dequeue())\n",
|
" print('Test: Dequeue a queue with one element')\n",
|
||||||
"print('Enqueue to a non-empty queue')\n",
|
" assert_equal(queue.dequeue(), 1)\n",
|
||||||
|
"\n",
|
||||||
|
" print('Test: Enqueue to a non-empty queue')\n",
|
||||||
" queue.enqueue(2)\n",
|
" queue.enqueue(2)\n",
|
||||||
" queue.enqueue(3)\n",
|
" queue.enqueue(3)\n",
|
||||||
" queue.enqueue(4)\n",
|
" queue.enqueue(4)\n",
|
||||||
"print('Dequeue a queue with more than one element')\n",
|
"\n",
|
||||||
"print(queue.dequeue())\n",
|
" print('Test: Dequeue a queue with more than one element')\n",
|
||||||
"print(queue.dequeue())\n",
|
" assert_equal(queue.dequeue(), 2)\n",
|
||||||
"print(queue.dequeue())"
|
" 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):
|
class Node(object):
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user