mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
236 lines
7.2 KiB
Plaintext
236 lines
7.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<small><i>This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://bit.ly/code-notes).</i></small>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem: Add two numbers whose digits are stored in a linked list in reverse order.\n",
|
|
"\n",
|
|
"* [Constraints](#Constraints)\n",
|
|
"* [Test Cases](#Test-Cases)\n",
|
|
"* [Algorithm](#Algorithm)\n",
|
|
"* [Code](#Code)\n",
|
|
"* [Unit Test](#Unit-Test)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Constraints\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\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test Cases\n",
|
|
"\n",
|
|
"* Empty list(s)\n",
|
|
"* Add values of different lengths\n",
|
|
" * Input 1: 6->5->None\n",
|
|
" * Input 2: 9->8->7\n",
|
|
" * Result: 5->4->8\n",
|
|
"* Add values of same lengths\n",
|
|
" * Exercised from values of different lengths\n",
|
|
" * Done here for completeness"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm\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",
|
|
"\n",
|
|
"* Base case:\n",
|
|
" * if first and second lists are NULL AND carry is zero\n",
|
|
" * Return NULL\n",
|
|
"* Recursive case:\n",
|
|
" * value = carry\n",
|
|
" * value += first.data + second.data\n",
|
|
" * remainder = value % 10\n",
|
|
" * new_carry = 1 if value >= 10, else 0\n",
|
|
" * Create a node with the remainder\n",
|
|
" * node.next = self.add(first.next, second.next, new_carry)\n",
|
|
" * Return node\n",
|
|
"\n",
|
|
"Complexity:\n",
|
|
"* Time: O(n)\n",
|
|
"* 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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"%run linked_list.py"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"class MyLinkedList(LinkedList):\n",
|
|
" def __add__(self, first_node, second_node, carry):\n",
|
|
" if type(carry) != int and carry < 0:\n",
|
|
" raise ValueError('Invalid int argument: carry')\n",
|
|
" if first_node is None and second_node is None and carry == 0:\n",
|
|
" return None\n",
|
|
" value = carry\n",
|
|
" value += first_node.data if first_node is not None else 0\n",
|
|
" value += second_node.data if second_node is not None else 0\n",
|
|
" remainder = value % 10\n",
|
|
" new_carry = 1 if value >= 10 else 0\n",
|
|
" node = Node(remainder)\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",
|
|
" return node\n",
|
|
"\n",
|
|
" def add(self, first_list, second_list):\n",
|
|
" if first_list is None or second_list is None:\n",
|
|
" return None\n",
|
|
" head = self.__add__(first_list.head, second_list.head, 0)\n",
|
|
" 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": 3,
|
|
"metadata": {
|
|
"collapsed": false
|
|
},
|
|
"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": [
|
|
"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_list = MyLinkedList(Node(6))\n",
|
|
" first_list.append(5)\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",
|
|
" 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",
|
|
" first_head = Node(6)\n",
|
|
" first_list = MyLinkedList(first_head)\n",
|
|
" first_list.append(5)\n",
|
|
" first_list.append(4)\n",
|
|
" second_head = Node(9)\n",
|
|
" second_list = MyLinkedList(second_head)\n",
|
|
" second_list.append(8)\n",
|
|
" second_list.append(7)\n",
|
|
" result = MyLinkedList().add(first_list, second_list)\n",
|
|
" 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()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 2",
|
|
"language": "python",
|
|
"name": "python2"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|