From 9ab9d70229cf6ea092f0356a7515f7616aca68ac Mon Sep 17 00:00:00 2001 From: Rick Date: Sat, 27 Jan 2018 00:23:13 -0500 Subject: [PATCH 1/2] add test case for properly deleting the head node of a linked list --- .../linked_list/linked_list_challenge.ipynb | 30 +++++++++++-------- .../linked_list/linked_list_solution.ipynb | 28 ++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/linked_lists/linked_list/linked_list_challenge.ipynb b/linked_lists/linked_list/linked_list_challenge.ipynb index 5ebf5766..ec5034c7 100644 --- a/linked_lists/linked_list/linked_list_challenge.ipynb +++ b/linked_lists/linked_list/linked_list_challenge.ipynb @@ -103,9 +103,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "class Node(object):\n", @@ -173,9 +171,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "# %load test_linked_list.py\n", @@ -255,7 +251,15 @@ " linked_list = LinkedList(head)\n", " linked_list.delete(None)\n", " assert_equal(linked_list.get_all_data(), [10])\n", - "\n", + " \n", + " print('Test: delete general case with match at the head')\n", + " head = Node(10)\n", + " linked_list = LinkedList(head)\n", + " linked_list.insert_to_front('a')\n", + " linked_list.insert_to_front('bc')\n", + " linked_list.delete('bc')\n", + " assert_equal(linked_list.get_all_data(), ['a', 10])\n", + " \n", " print('Test: delete general case with matches')\n", " head = Node(10)\n", " linked_list = LinkedList(head)\n", @@ -310,23 +314,23 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 2", "language": "python", - "name": "python3" + "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 3 + "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.5.0" + "pygments_lexer": "ipython2", + "version": "2.7.13" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } diff --git a/linked_lists/linked_list/linked_list_solution.ipynb b/linked_lists/linked_list/linked_list_solution.ipynb index d3c05e60..15c24753 100644 --- a/linked_lists/linked_list/linked_list_solution.ipynb +++ b/linked_lists/linked_list/linked_list_solution.ipynb @@ -171,9 +171,7 @@ { "cell_type": "code", "execution_count": 1, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -288,9 +286,7 @@ { "cell_type": "code", "execution_count": 2, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [], "source": [ "%run linked_list.py" @@ -306,9 +302,7 @@ { "cell_type": "code", "execution_count": 3, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -396,6 +390,14 @@ " linked_list = LinkedList(head)\n", " linked_list.delete(None)\n", " assert_equal(linked_list.get_all_data(), [10])\n", + " \n", + " print('Test: delete general case with match at the head')\n", + " head = Node(10)\n", + " linked_list = LinkedList(head)\n", + " linked_list.insert_to_front('a')\n", + " linked_list.insert_to_front('bc')\n", + " linked_list.delete('bc')\n", + " assert_equal(linked_list.get_all_data(), ['a', 10])\n", "\n", " print('Test: delete general case with matches')\n", " head = Node(10)\n", @@ -442,9 +444,7 @@ { "cell_type": "code", "execution_count": 4, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "name": "stdout", @@ -501,9 +501,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.12" + "version": "2.7.13" } }, "nbformat": 4, - "nbformat_minor": 0 + "nbformat_minor": 1 } From f6c38841bbb7ed228afaa160d09abd8c36433738 Mon Sep 17 00:00:00 2001 From: Rick Date: Sat, 27 Jan 2018 01:09:45 -0500 Subject: [PATCH 2/2] fixes the alternative delete method (alt_delete) so that it properly handles the deletion of the head node --- .../linked_list/linked_list_solution.ipynb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/linked_lists/linked_list/linked_list_solution.ipynb b/linked_lists/linked_list/linked_list_solution.ipynb index 15c24753..b2b598ee 100644 --- a/linked_lists/linked_list/linked_list_solution.ipynb +++ b/linked_lists/linked_list/linked_list_solution.ipynb @@ -170,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -258,10 +258,10 @@ " return\n", " if self.head is None:\n", " return\n", - " curr_node = self.head\n", - " if curr_node.data == data:\n", - " curr_node = curr_node.next\n", + " if self.head.data == data:\n", + " self.head = self.head.next\n", " return\n", + " curr_node = self.head\n", " while curr_node.next is not None:\n", " if curr_node.next.data == data:\n", " curr_node.next = curr_node.next.next\n", @@ -285,7 +285,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -301,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -443,7 +443,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -468,6 +468,7 @@ "\n", "Test: delete on an empty list\n", "Test: delete a None\n", + "Test: delete general case with match at the head\n", "Test: delete general case with matches\n", "Test: delete general case with no matches\n", "Success: test_delete\n",