Reworked notebook: Added more detail to constraints and test cases. Reworked algorithm discussion and unit test.

This commit is contained in:
Donne Martin 2015-06-26 05:04:54 -04:00
parent 25783b5bbb
commit 27ebbf6581

View File

@ -13,22 +13,29 @@
"source": [ "source": [
"## Problem: Add two numbers whose digits are stored in a linked list in reverse order.\n", "## Problem: Add two numbers whose digits are stored in a linked list in reverse order.\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)" "* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)"
] ]
}, },
{ {
"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",
"* Do you expect the return to be in reverse order too?\n", "* Do you expect the return to be in reverse order too?\n",
" * Yes\n", " * Yes\n",
"* What if one of the inputs is NULL?\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", "\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", "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", "\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", "* Base case:\n",
" * if first and second lists are NULL AND carry is zero\n", " * if first and second lists are NULL AND carry is zero\n",
" * Return NULL\n", " * Return NULL\n",
@ -73,7 +76,12 @@
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(n)\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", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": true "collapsed": true
}, },
@ -96,7 +104,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -114,7 +122,6 @@
" remainder = value % 10\n", " remainder = value % 10\n",
" new_carry = 1 if value >= 10 else 0\n", " new_carry = 1 if value >= 10 else 0\n",
" node = Node(remainder)\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", " 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", " second_node.next if first_node is not None else None, \n",
" new_carry)\n", " new_carry)\n",
@ -127,28 +134,61 @@
" return MyLinkedList(head)" " 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", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"metadata": { "metadata": {
"collapsed": false "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": [ "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 1: 6->5->None\n",
" # Input 2: 9->8->7\n", " # Input 2: 9->8->7\n",
" # Result: 5->4->8\n", " # Result: 5->4->8\n",
"first_head = Node(6)\n", " first_list = MyLinkedList(Node(6))\n",
"first_list = MyLinkedList(first_head)\n",
" first_list.append(5)\n", " first_list.append(5)\n",
"second_head = Node(9)\n", " second_list = MyLinkedList(Node(9))\n",
"second_list = MyLinkedList(second_head)\n",
" second_list.append(8)\n", " second_list.append(8)\n",
" second_list.append(7)\n", " second_list.append(7)\n",
" result = MyLinkedList().add(first_list, second_list)\n", " result = MyLinkedList().add(first_list, second_list)\n",
"result.print_list()\n", " assert_equal(result.get_all_data(), [5, 4, 8])\n",
"print('Add values of same lengths')\n", "\n",
" print('Test: Add values of same lengths')\n",
" # Input 1: 6->5->4\n", " # Input 1: 6->5->4\n",
" # Input 2: 9->8->7\n", " # Input 2: 9->8->7\n",
" # Result: 5->4->2->1\n", " # Result: 5->4->2->1\n",
@ -161,11 +201,13 @@
" second_list.append(8)\n", " second_list.append(8)\n",
" second_list.append(7)\n", " second_list.append(7)\n",
" result = MyLinkedList().add(first_list, second_list)\n", " result = MyLinkedList().add(first_list, second_list)\n",
"result.print_list()\n", " assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
"print('Empty list(s)')\n", " \n",
"result = MyLinkedList().add(None, None)\n", " print('Success: test_add')\n",
"result = MyLinkedList().add(first_head, None)\n", "\n",
"result = MyLinkedList().add(None, second_head)" "if __name__ == '__main__':\n",
" test = Test()\n",
" test.test_add()"
] ]
} }
], ],