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 algorithm discussion and unit test.
This commit is contained in:
parent
25783b5bbb
commit
27ebbf6581
|
@ -13,22 +13,29 @@
|
|||
"source": [
|
||||
"## Problem: Add two numbers whose digits are stored in a linked list in reverse order.\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 return to be in reverse order too?\n",
|
||||
" * Yes\n",
|
||||
"* What if one of the inputs is NULL?\n",
|
||||
" * Return NULL for an invalid operation"
|
||||
" * Return NULL for an invalid operation\n",
|
||||
"* How large are these numbers--can they fit in memory?\n",
|
||||
" * Yes\n",
|
||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||
" * Yes"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -55,10 +62,6 @@
|
|||
"\n",
|
||||
"We could solve this with an iterative or a recursive algorithm, both are well suited for this exercise. We'll use a recursive algorithm for practice with recursion. Note this takes an extra space of O(m) where m is the recursion depth.\n",
|
||||
"\n",
|
||||
"* Test for error cases\n",
|
||||
"* Careful with adding if the lists differ\n",
|
||||
" * Only add if a node is not NULL\n",
|
||||
" * Alternatively, we could add trailing zeroes to the smaller list\n",
|
||||
"* Base case:\n",
|
||||
" * if first and second lists are NULL AND carry is zero\n",
|
||||
" * Return NULL\n",
|
||||
|
@ -73,7 +76,12 @@
|
|||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
"* Space: O(n), extra space for result and recursion depth"
|
||||
"* Space: O(n), extra space for result and recursion depth\n",
|
||||
"\n",
|
||||
"Notes:\n",
|
||||
"* Careful with adding if the lists differ\n",
|
||||
" * Only add if a node is not NULL\n",
|
||||
" * Alternatively, we could add trailing zeroes to the smaller list"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -85,7 +93,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
|
@ -96,7 +104,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -114,7 +122,6 @@
|
|||
" remainder = value % 10\n",
|
||||
" new_carry = 1 if value >= 10 else 0\n",
|
||||
" node = Node(remainder)\n",
|
||||
" #import pdb; pdb.set_trace()\n",
|
||||
" node.next = self.__add__(first_node.next if first_node is not None else None, \n",
|
||||
" second_node.next if first_node is not None else None, \n",
|
||||
" new_carry)\n",
|
||||
|
@ -127,28 +134,61 @@
|
|||
" return MyLinkedList(head)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"*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: Empty list(s)\n",
|
||||
"Test: Add values of different lengths\n",
|
||||
"Test: Add values of same lengths\n",
|
||||
"Success: test_add\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print('Add values of different lengths')\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
" def test_add(self):\n",
|
||||
" print('Test: Empty list(s)')\n",
|
||||
" assert_equal(MyLinkedList().add(None, None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(Node(5), None), None)\n",
|
||||
" assert_equal(MyLinkedList().add(None, Node(10)), None)\n",
|
||||
"\n",
|
||||
" print('Test: Add values of different lengths')\n",
|
||||
" # Input 1: 6->5->None\n",
|
||||
" # Input 2: 9->8->7\n",
|
||||
" # Result: 5->4->8\n",
|
||||
"first_head = Node(6)\n",
|
||||
"first_list = MyLinkedList(first_head)\n",
|
||||
" first_list = MyLinkedList(Node(6))\n",
|
||||
" first_list.append(5)\n",
|
||||
"second_head = Node(9)\n",
|
||||
"second_list = MyLinkedList(second_head)\n",
|
||||
" second_list = MyLinkedList(Node(9))\n",
|
||||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
"result.print_list()\n",
|
||||
"print('Add values of same lengths')\n",
|
||||
" assert_equal(result.get_all_data(), [5, 4, 8])\n",
|
||||
"\n",
|
||||
" print('Test: Add values of same lengths')\n",
|
||||
" # Input 1: 6->5->4\n",
|
||||
" # Input 2: 9->8->7\n",
|
||||
" # Result: 5->4->2->1\n",
|
||||
|
@ -161,11 +201,13 @@
|
|||
" second_list.append(8)\n",
|
||||
" second_list.append(7)\n",
|
||||
" result = MyLinkedList().add(first_list, second_list)\n",
|
||||
"result.print_list()\n",
|
||||
"print('Empty list(s)')\n",
|
||||
"result = MyLinkedList().add(None, None)\n",
|
||||
"result = MyLinkedList().add(first_head, None)\n",
|
||||
"result = MyLinkedList().add(None, second_head)"
|
||||
" assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
|
||||
" \n",
|
||||
" print('Success: test_add')\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_add()"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue
Block a user