From 37cf2028e69bc6abdaa0b38bda7517c92fec542b Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 18 Jun 2016 11:46:00 -0400 Subject: [PATCH] Polish delete mid node challenge and solution Update edge cases and tests. --- .../delete_mid/delete_mid_challenge.ipynb | 10 ++++---- .../delete_mid/delete_mid_solution.ipynb | 25 +++++++++++++------ linked_lists/delete_mid/test_delete_mid.py | 17 ++++++++++--- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/linked_lists/delete_mid/delete_mid_challenge.ipynb b/linked_lists/delete_mid/delete_mid_challenge.ipynb index 19ea34c..6034abe 100644 --- a/linked_lists/delete_mid/delete_mid_challenge.ipynb +++ b/linked_lists/delete_mid/delete_mid_challenge.ipynb @@ -170,21 +170,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", - "version": 2 + "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", - "pygments_lexer": "ipython2", - "version": "2.7.10" + "pygments_lexer": "ipython3", + "version": "3.5.0" } }, "nbformat": 4, diff --git a/linked_lists/delete_mid/delete_mid_solution.ipynb b/linked_lists/delete_mid/delete_mid_solution.ipynb index 71c7427..38b5364 100644 --- a/linked_lists/delete_mid/delete_mid_solution.ipynb +++ b/linked_lists/delete_mid/delete_mid_solution.ipynb @@ -96,9 +96,10 @@ "class MyLinkedList(LinkedList):\n", "\n", " def delete_node(self, node):\n", - " if self.head is None:\n", + " if self.head is None or node is None:\n", " return\n", - " if node is None:\n", + " if self.head == node:\n", + " self.head = None\n", " return\n", " if node.next is None:\n", " node.data = None\n", @@ -147,16 +148,25 @@ " head = Node(2)\n", " linked_list = MyLinkedList(head)\n", " linked_list.delete_node(head)\n", - " assert_equal(linked_list.get_all_data(), [None])\n", + " assert_equal(linked_list.get_all_data(), [])\n", "\n", " print('Test: Multiple nodes')\n", " linked_list = MyLinkedList(None)\n", - " node0 = linked_list.insert_to_front(1)\n", + " node0 = linked_list.insert_to_front(2)\n", " node1 = linked_list.insert_to_front(3)\n", " node2 = linked_list.insert_to_front(4)\n", " node3 = linked_list.insert_to_front(1)\n", - " linked_list.delete_node(node2)\n", - " assert_equal(linked_list.get_all_data(), [1, 3, 1])\n", + " linked_list.delete_node(node1)\n", + " assert_equal(linked_list.get_all_data(), [1, 4, 2])\n", + "\n", + " print('Test: Multiple nodes, delete last element')\n", + " linked_list = MyLinkedList(None)\n", + " node0 = linked_list.insert_to_front(2)\n", + " node1 = linked_list.insert_to_front(3)\n", + " node2 = linked_list.insert_to_front(4)\n", + " node3 = linked_list.insert_to_front(1)\n", + " linked_list.delete_node(node0)\n", + " assert_equal(linked_list.get_all_data(), [1, 4, 3, None])\n", "\n", " print('Success: test_delete_node')\n", "\n", @@ -184,6 +194,7 @@ "Test: Empty list, null node to delete\n", "Test: One node\n", "Test: Multiple nodes\n", + "Test: Multiple nodes, delete last element\n", "Success: test_delete_node\n" ] } @@ -209,7 +220,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.4.3" + "version": "3.5.0" } }, "nbformat": 4, diff --git a/linked_lists/delete_mid/test_delete_mid.py b/linked_lists/delete_mid/test_delete_mid.py index ad19a34..5131c36 100644 --- a/linked_lists/delete_mid/test_delete_mid.py +++ b/linked_lists/delete_mid/test_delete_mid.py @@ -13,16 +13,25 @@ class TestDeleteNode(object): head = Node(2) linked_list = MyLinkedList(head) linked_list.delete_node(head) - assert_equal(linked_list.get_all_data(), [None]) + assert_equal(linked_list.get_all_data(), []) print('Test: Multiple nodes') linked_list = MyLinkedList(None) - node0 = linked_list.insert_to_front(1) + node0 = linked_list.insert_to_front(2) node1 = linked_list.insert_to_front(3) node2 = linked_list.insert_to_front(4) node3 = linked_list.insert_to_front(1) - linked_list.delete_node(node2) - assert_equal(linked_list.get_all_data(), [1, 3, 1]) + linked_list.delete_node(node1) + assert_equal(linked_list.get_all_data(), [1, 4, 2]) + + print('Test: Multiple nodes, delete last element') + linked_list = MyLinkedList(None) + node0 = linked_list.insert_to_front(2) + node1 = linked_list.insert_to_front(3) + node2 = linked_list.insert_to_front(4) + node3 = linked_list.insert_to_front(1) + linked_list.delete_node(node0) + assert_equal(linked_list.get_all_data(), [1, 4, 3, None]) print('Success: test_delete_node')