mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-10 03:18:02 +00:00
Added add_reverse challenge.
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* Empty list(s)\n",
|
||||
"* Empty list(s) -> None\n",
|
||||
"* Add values of different lengths\n",
|
||||
" * Input 1: 6->5->None\n",
|
||||
" * Input 2: 9->8->7\n",
|
||||
@@ -99,7 +99,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run linked_list.py"
|
||||
"%run ../linked_list/linked_list.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -111,11 +111,16 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class MyLinkedList(LinkedList):\n",
|
||||
" \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",
|
||||
" \n",
|
||||
" # Base case\n",
|
||||
" if first_node is None and second_node is None and carry == 0:\n",
|
||||
" return None\n",
|
||||
" \n",
|
||||
" # Recursive case\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",
|
||||
@@ -159,17 +164,17 @@
|
||||
"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"
|
||||
"Overwriting test_add_reverse.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile test_add_reverse.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class Test(object):\n",
|
||||
"\n",
|
||||
"class TestAddReverse(object):\n",
|
||||
" \n",
|
||||
" def test_add(self):\n",
|
||||
" print('Test: Empty list(s)')\n",
|
||||
" assert_equal(MyLinkedList().add(None, None), None)\n",
|
||||
@@ -205,9 +210,34 @@
|
||||
" \n",
|
||||
" print('Success: test_add')\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestAddReverse()\n",
|
||||
" test.test_add()\n",
|
||||
"\n",
|
||||
"if __name__ == '__main__':\n",
|
||||
" test = Test()\n",
|
||||
" test.test_add()"
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"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": [
|
||||
"%run -i test_add_reverse.py"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user