From 04083b2011ad4c98244666c7bf5ae83311b4666b Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Sat, 11 Jul 2015 15:34:52 -0400 Subject: [PATCH] Fix #13, PEP8-ify notebooks. --- .../add_reverse/add_reverse_challenge.ipynb | 9 +++-- .../add_reverse/add_reverse_solution.ipynb | 20 +++++----- linked_lists/add_reverse/test_add_reverse.py | 6 ++- .../delete_mid/delete_mid_challenge.ipynb | 13 +++--- .../delete_mid/delete_mid_solution.ipynb | 13 +++--- linked_lists/delete_mid/test_delete_mid.py | 10 +++-- .../find_loop_start_challenge.ipynb | 19 ++++----- .../find_loop_start_solution.ipynb | 27 +++++++------ .../find_loop_start/test_find_loop_start.py | 16 ++++---- .../kth_to_last_elem_challenge.ipynb | 17 ++++---- .../kth_to_last_elem_solution.ipynb | 23 ++++++----- .../kth_to_last_elem/test_kth_to_last_elem.py | 14 ++++--- linked_lists/linked_list/linked_list.py | 15 +++---- .../linked_list/linked_list_challenge.ipynb | 40 ++++++++++--------- .../linked_list/linked_list_solution.ipynb | 40 ++++++++++--------- linked_lists/linked_list/test_linked_list.py | 2 + .../palindrome/palindrome_challenge.ipynb | 9 +++-- .../palindrome/palindrome_solution.ipynb | 14 ++++--- linked_lists/palindrome/test_palindrome.py | 6 ++- .../partition/partition_challenge.ipynb | 13 +++--- .../partition/partition_solution.ipynb | 15 +++---- linked_lists/partition/test_partition.py | 10 +++-- .../remove_duplicates_challenge.ipynb | 11 ++--- .../remove_duplicates_solution.ipynb | 13 +++--- .../test_remove_duplicates.py | 8 ++-- 25 files changed, 210 insertions(+), 173 deletions(-) diff --git a/linked_lists/add_reverse/add_reverse_challenge.ipynb b/linked_lists/add_reverse/add_reverse_challenge.ipynb index b031efa..5c9a8f0 100644 --- a/linked_lists/add_reverse/add_reverse_challenge.ipynb +++ b/linked_lists/add_reverse/add_reverse_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Do you expect the return to be in reverse order too?\n", " * Yes\n", "* What if one of the inputs is None?\n", @@ -98,7 +97,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def add_reverse(self, first_list, second_list):\n", " # TODO: Implement me\n", " pass" @@ -133,7 +132,7 @@ "\n", "\n", "class TestAddReverse(object):\n", - " \n", + "\n", " def test_add_reverse(self):\n", " print('Test: Empty list(s)')\n", " assert_equal(MyLinkedList().add_reverse(None, None), None)\n", @@ -166,13 +165,15 @@ " second_list.append(7)\n", " result = MyLinkedList().add_reverse(first_list, second_list)\n", " assert_equal(result.get_all_data(), [5, 4, 2, 1])\n", - " \n", + "\n", " print('Success: test_add_reverse')\n", "\n", + "\n", "def main():\n", " test = TestAddReverse()\n", " test.test_add_reverse()\n", "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/add_reverse/add_reverse_solution.ipynb b/linked_lists/add_reverse/add_reverse_solution.ipynb index a53d366..f3f185f 100644 --- a/linked_lists/add_reverse/add_reverse_solution.ipynb +++ b/linked_lists/add_reverse/add_reverse_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Do you expect the return to be in reverse order too?\n", " * Yes\n", "* What if one of the inputs is None?\n", @@ -117,15 +116,15 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def __add_reverse__(self, first_node, second_node, carry):\n", " if type(carry) != int and carry < 0:\n", " raise ValueError('Invalid int argument: carry')\n", - " \n", + "\n", " # Base case\n", " if first_node is None and second_node is None and carry == 0:\n", " return None\n", - " \n", + "\n", " # Recursive case\n", " value = carry\n", " value += first_node.data if first_node is not None else 0\n", @@ -133,9 +132,10 @@ " new_carry = 1 if value >= 10 else 0\n", " remainder = value % 10\n", " node = Node(remainder)\n", - " node.next = self.__add_reverse__(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", + " node.next = self.__add_reverse__(\n", + " 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_reverse(self, first_list, second_list):\n", @@ -173,7 +173,7 @@ "\n", "\n", "class TestAddReverse(object):\n", - " \n", + "\n", " def test_add_reverse(self):\n", " print('Test: Empty list(s)')\n", " assert_equal(MyLinkedList().add_reverse(None, None), None)\n", @@ -206,13 +206,15 @@ " second_list.append(7)\n", " result = MyLinkedList().add_reverse(first_list, second_list)\n", " assert_equal(result.get_all_data(), [5, 4, 2, 1])\n", - " \n", + "\n", " print('Success: test_add_reverse')\n", "\n", + "\n", "def main():\n", " test = TestAddReverse()\n", " test.test_add_reverse()\n", "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/add_reverse/test_add_reverse.py b/linked_lists/add_reverse/test_add_reverse.py index 29ea8f9..30799f7 100644 --- a/linked_lists/add_reverse/test_add_reverse.py +++ b/linked_lists/add_reverse/test_add_reverse.py @@ -2,7 +2,7 @@ from nose.tools import assert_equal class TestAddReverse(object): - + def test_add_reverse(self): print('Test: Empty list(s)') assert_equal(MyLinkedList().add_reverse(None, None), None) @@ -35,12 +35,14 @@ class TestAddReverse(object): second_list.append(7) result = MyLinkedList().add_reverse(first_list, second_list) assert_equal(result.get_all_data(), [5, 4, 2, 1]) - + print('Success: test_add_reverse') + def main(): test = TestAddReverse() test.test_add_reverse() + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/delete_mid/delete_mid_challenge.ipynb b/linked_lists/delete_mid/delete_mid_challenge.ipynb index 4954b13..19ea34c 100644 --- a/linked_lists/delete_mid/delete_mid_challenge.ipynb +++ b/linked_lists/delete_mid/delete_mid_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* What if the final node is being deleted, for example a single node list? Do we make it a dummy with value None?\n", " * Yes\n", "* Can we assume we already have a linked list class that can be used for this problem?\n", @@ -90,7 +89,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def delete_node(self, node):\n", " # TODO: Implement me\n", " pass" @@ -125,7 +124,7 @@ "\n", "\n", "class TestDeleteNode(object):\n", - " \n", + "\n", " def test_delete_node(self):\n", " print('Test: Empty list, null node to delete')\n", " linked_list = MyLinkedList(None)\n", @@ -139,20 +138,22 @@ " assert_equal(linked_list.get_all_data(), [None])\n", "\n", " print('Test: Multiple nodes')\n", - " linked_list = MyLinkedList(None) \n", + " linked_list = MyLinkedList(None)\n", " node0 = linked_list.insert_to_front(1)\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", - " \n", + "\n", " print('Success: test_delete_node')\n", "\n", + "\n", "def main():\n", " test = TestDeleteNode()\n", " test.test_delete_node()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/delete_mid/delete_mid_solution.ipynb b/linked_lists/delete_mid/delete_mid_solution.ipynb index 45e9bac..027c32e 100644 --- a/linked_lists/delete_mid/delete_mid_solution.ipynb +++ b/linked_lists/delete_mid/delete_mid_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* What if the final node is being deleted, for example a single node list? Do we make it a dummy with value None?\n", " * Yes\n", "* Can we assume we already have a linked list class that can be used for this problem?\n", @@ -95,7 +94,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def delete_node(self, node):\n", " if self.head is None:\n", " return\n", @@ -138,7 +137,7 @@ "\n", "\n", "class TestDeleteNode(object):\n", - " \n", + "\n", " def test_delete_node(self):\n", " print('Test: Empty list, null node to delete')\n", " linked_list = MyLinkedList(None)\n", @@ -152,20 +151,22 @@ " assert_equal(linked_list.get_all_data(), [None])\n", "\n", " print('Test: Multiple nodes')\n", - " linked_list = MyLinkedList(None) \n", + " linked_list = MyLinkedList(None)\n", " node0 = linked_list.insert_to_front(1)\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", - " \n", + "\n", " print('Success: test_delete_node')\n", "\n", + "\n", "def main():\n", " test = TestDeleteNode()\n", " test.test_delete_node()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/delete_mid/test_delete_mid.py b/linked_lists/delete_mid/test_delete_mid.py index 7820e8f..ad19a34 100644 --- a/linked_lists/delete_mid/test_delete_mid.py +++ b/linked_lists/delete_mid/test_delete_mid.py @@ -2,7 +2,7 @@ from nose.tools import assert_equal class TestDeleteNode(object): - + def test_delete_node(self): print('Test: Empty list, null node to delete') linked_list = MyLinkedList(None) @@ -16,19 +16,21 @@ class TestDeleteNode(object): assert_equal(linked_list.get_all_data(), [None]) print('Test: Multiple nodes') - linked_list = MyLinkedList(None) + linked_list = MyLinkedList(None) node0 = linked_list.insert_to_front(1) 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]) - + print('Success: test_delete_node') + def main(): test = TestDeleteNode() test.test_delete_node() - + + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/find_loop_start/find_loop_start_challenge.ipynb b/linked_lists/find_loop_start/find_loop_start_challenge.ipynb index 14f2df1..d6065fa 100644 --- a/linked_lists/find_loop_start/find_loop_start_challenge.ipynb +++ b/linked_lists/find_loop_start/find_loop_start_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly linked list?\n", " * Yes\n", "* Can we assume we are always passed a circular linked list?\n", @@ -93,7 +92,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def find_loop_start(self):\n", " # TODO: Implement me\n", " pass" @@ -128,25 +127,25 @@ "\n", "\n", "class TestFindLoopStart(object):\n", - " \n", + "\n", " def test_find_loop_start(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList()\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: One element')\n", " head = Node(1)\n", " linked_list = MyLinkedList(head)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: Two elements')\n", " linked_list.append(2)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: Three or more elements')\n", " linked_list.append(3)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: General case: Circular linked list')\n", " node10 = Node(10)\n", " node9 = Node(9, node10)\n", @@ -162,13 +161,15 @@ " node10.next = node3\n", " linked_list = MyLinkedList(node0)\n", " assert_equal(linked_list.find_loop_start(), 3)\n", - " \n", + "\n", " print('Success: test_find_loop_start')\n", "\n", + "\n", "def main():\n", " test = TestFindLoopStart()\n", " test.test_find_loop_start()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/find_loop_start/find_loop_start_solution.ipynb b/linked_lists/find_loop_start/find_loop_start_solution.ipynb index 02573c7..b687bb7 100644 --- a/linked_lists/find_loop_start/find_loop_start_solution.ipynb +++ b/linked_lists/find_loop_start/find_loop_start_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly linked list?\n", " * Yes\n", "* Can we assume we are always passed a circular linked list?\n", @@ -101,7 +100,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def find_loop_start(self):\n", " if self.head is None or self.head.next is None:\n", " return\n", @@ -109,7 +108,7 @@ " j = self.head\n", " i = i.next\n", " j = j.next.next\n", - " \n", + "\n", " # Increment i and j until they meet\n", " # j is incremented twice as fast as i\n", " while j != i:\n", @@ -117,11 +116,11 @@ " if j is None or j.next is None:\n", " return\n", " j = j.next.next\n", - " \n", + "\n", " # When i and j meet, move j to the head\n", " j = self.head\n", - " \n", - " # Increment i and j one node at a time until \n", + "\n", + " # Increment i and j one node at a time until\n", " # they meet, which is the start of the loop\n", " while j != i:\n", " i = i.next\n", @@ -157,25 +156,25 @@ "\n", "\n", "class TestFindLoopStart(object):\n", - " \n", + "\n", " def test_find_loop_start(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList()\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: One element')\n", " head = Node(1)\n", " linked_list = MyLinkedList(head)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: Two elements')\n", " linked_list.append(2)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: Not a circular linked list: Three or more elements')\n", " linked_list.append(3)\n", " assert_equal(linked_list.find_loop_start(), None)\n", - " \n", + "\n", " print('Test: General case: Circular linked list')\n", " node10 = Node(10)\n", " node9 = Node(9, node10)\n", @@ -191,13 +190,15 @@ " node10.next = node3\n", " linked_list = MyLinkedList(node0)\n", " assert_equal(linked_list.find_loop_start(), 3)\n", - " \n", + "\n", " print('Success: test_find_loop_start')\n", "\n", + "\n", "def main():\n", " test = TestFindLoopStart()\n", " test.test_find_loop_start()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/find_loop_start/test_find_loop_start.py b/linked_lists/find_loop_start/test_find_loop_start.py index c113480..8ced37d 100644 --- a/linked_lists/find_loop_start/test_find_loop_start.py +++ b/linked_lists/find_loop_start/test_find_loop_start.py @@ -2,25 +2,25 @@ from nose.tools import assert_equal class TestFindLoopStart(object): - + def test_find_loop_start(self): print('Test: Empty list') linked_list = MyLinkedList() assert_equal(linked_list.find_loop_start(), None) - + print('Test: Not a circular linked list: One element') head = Node(1) linked_list = MyLinkedList(head) assert_equal(linked_list.find_loop_start(), None) - + print('Test: Not a circular linked list: Two elements') linked_list.append(2) assert_equal(linked_list.find_loop_start(), None) - + print('Test: Not a circular linked list: Three or more elements') linked_list.append(3) assert_equal(linked_list.find_loop_start(), None) - + print('Test: General case: Circular linked list') node10 = Node(10) node9 = Node(9, node10) @@ -36,12 +36,14 @@ class TestFindLoopStart(object): node10.next = node3 linked_list = MyLinkedList(node0) assert_equal(linked_list.find_loop_start(), 3) - + print('Success: test_find_loop_start') + def main(): test = TestFindLoopStart() test.test_find_loop_start() - + + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/kth_to_last_elem/kth_to_last_elem_challenge.ipynb b/linked_lists/kth_to_last_elem/kth_to_last_elem_challenge.ipynb index bcd43f6..2d8d398 100644 --- a/linked_lists/kth_to_last_elem/kth_to_last_elem_challenge.ipynb +++ b/linked_lists/kth_to_last_elem/kth_to_last_elem_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we assume k is a valid integer?\n", " * Yes\n", "* If k = 0, does this return the last element?\n", @@ -96,7 +95,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def kth_to_last_elem(self, k):\n", " # TODO: Implement me\n", " pass" @@ -131,33 +130,35 @@ "\n", "\n", "class Test(object):\n", - " \n", + "\n", " def test_kth_to_last_elem(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList(None)\n", " assert_equal(linked_list.kth_to_last_elem(0), None)\n", - " \n", + "\n", " print('Test: k >= len(list)')\n", " assert_equal(linked_list.kth_to_last_elem(100), None)\n", - " \n", + "\n", " print('Test: One element, k = 0')\n", " head = Node(2)\n", " linked_list = MyLinkedList(head)\n", " assert_equal(linked_list.kth_to_last_elem(0), 2)\n", - " \n", + "\n", " print('Test: General case')\n", " linked_list.insert_to_front(1)\n", " linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(5)\n", " linked_list.insert_to_front(7)\n", " assert_equal(linked_list.kth_to_last_elem(2), 3)\n", - " \n", + "\n", " print('Success: test_kth_to_last_elem')\n", "\n", + "\n", "def main():\n", " test = Test()\n", " test.test_kth_to_last_elem()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/kth_to_last_elem/kth_to_last_elem_solution.ipynb b/linked_lists/kth_to_last_elem/kth_to_last_elem_solution.ipynb index 8a29f03..57b05b6 100644 --- a/linked_lists/kth_to_last_elem/kth_to_last_elem_solution.ipynb +++ b/linked_lists/kth_to_last_elem/kth_to_last_elem_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we assume k is a valid integer?\n", " * Yes\n", "* If k = 0, does this return the last element?\n", @@ -101,7 +100,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def kth_to_last_elem(self, k):\n", " if self.head is None:\n", " return\n", @@ -110,15 +109,15 @@ " curr = self.head\n", " prev = self.head\n", " counter = 0\n", - " \n", - " # Give current a headstart, incrementing it \n", + "\n", + " # Give current a headstart, incrementing it\n", " # once for k = 1, twice for k = 2, etc\n", " while counter < k:\n", " curr = curr.next\n", " counter += 1\n", " if curr is None:\n", " return\n", - " \n", + "\n", " # Increment both pointers until current reaches the end\n", " while curr.next is not None:\n", " curr = curr.next\n", @@ -154,33 +153,35 @@ "\n", "\n", "class Test(object):\n", - " \n", + "\n", " def test_kth_to_last_elem(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList(None)\n", " assert_equal(linked_list.kth_to_last_elem(0), None)\n", - " \n", + "\n", " print('Test: k >= len(list)')\n", " assert_equal(linked_list.kth_to_last_elem(100), None)\n", - " \n", + "\n", " print('Test: One element, k = 0')\n", " head = Node(2)\n", " linked_list = MyLinkedList(head)\n", " assert_equal(linked_list.kth_to_last_elem(0), 2)\n", - " \n", + "\n", " print('Test: General case')\n", " linked_list.insert_to_front(1)\n", " linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(5)\n", " linked_list.insert_to_front(7)\n", " assert_equal(linked_list.kth_to_last_elem(2), 3)\n", - " \n", + "\n", " print('Success: test_kth_to_last_elem')\n", "\n", + "\n", "def main():\n", " test = Test()\n", " test.test_kth_to_last_elem()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/kth_to_last_elem/test_kth_to_last_elem.py b/linked_lists/kth_to_last_elem/test_kth_to_last_elem.py index 18e3d14..40f76ae 100644 --- a/linked_lists/kth_to_last_elem/test_kth_to_last_elem.py +++ b/linked_lists/kth_to_last_elem/test_kth_to_last_elem.py @@ -2,32 +2,34 @@ from nose.tools import assert_equal class Test(object): - + def test_kth_to_last_elem(self): print('Test: Empty list') linked_list = MyLinkedList(None) assert_equal(linked_list.kth_to_last_elem(0), None) - + print('Test: k >= len(list)') assert_equal(linked_list.kth_to_last_elem(100), None) - + print('Test: One element, k = 0') head = Node(2) linked_list = MyLinkedList(head) assert_equal(linked_list.kth_to_last_elem(0), 2) - + print('Test: General case') linked_list.insert_to_front(1) linked_list.insert_to_front(3) linked_list.insert_to_front(5) linked_list.insert_to_front(7) assert_equal(linked_list.kth_to_last_elem(2), 3) - + print('Success: test_kth_to_last_elem') + def main(): test = Test() test.test_kth_to_last_elem() - + + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/linked_list/linked_list.py b/linked_lists/linked_list/linked_list.py index d888321..77b73cb 100644 --- a/linked_lists/linked_list/linked_list.py +++ b/linked_lists/linked_list/linked_list.py @@ -1,14 +1,15 @@ class Node(object): - + def __init__(self, data, next_node=None): self.next = next_node self.data = data - + def __str__(self): return self.data + class LinkedList(object): - + def __init__(self, head=None): self.head = head @@ -19,7 +20,7 @@ class LinkedList(object): counter += 1 curr = curr.next return counter - + def insert_to_front(self, data): if data is None: return @@ -30,7 +31,7 @@ class LinkedList(object): node.next = self.head self.head = node return node - + def append(self, data, next_node=None): if data is None: return @@ -43,7 +44,7 @@ class LinkedList(object): curr_node = curr_node.next curr_node.next = node return node - + def find(self, data): if data is None: return @@ -56,7 +57,7 @@ class LinkedList(object): else: curr_node = curr_node.next return - + def delete(self, data): if data is None: return diff --git a/linked_lists/linked_list/linked_list_challenge.ipynb b/linked_lists/linked_list/linked_list_challenge.ipynb index 4761bfd..1128c72 100644 --- a/linked_lists/linked_list/linked_list_challenge.ipynb +++ b/linked_lists/linked_list/linked_list_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly or doubly linked list?\n", " * Singly\n", "* Is this a circular list?\n", @@ -110,17 +109,18 @@ "outputs": [], "source": [ "class Node(object):\n", - " \n", + "\n", " def __init__(self, data, next_node=None):\n", " pass\n", " # TODO: Implement me\n", - " \n", + "\n", " def __str__(self):\n", " pass\n", " # TODO: Implement me\n", "\n", + "\n", "class LinkedList(object):\n", - " \n", + "\n", " def __init__(self, head=None):\n", " pass\n", " # TODO: Implement me\n", @@ -128,19 +128,19 @@ " def __len__(self):\n", " pass\n", " # TODO: Implement me\n", - " \n", + "\n", " def insert_to_front(self, data):\n", " pass\n", " # TODO: Implement me\n", - " \n", + "\n", " def append(self, data, next_node=None):\n", " pass\n", " # TODO: Implement me\n", - " \n", + "\n", " def find(self, data):\n", " pass\n", " # TODO: Implement me\n", - " \n", + "\n", " def delete(self, data):\n", " pass\n", " # TODO: Implement me\n", @@ -183,7 +183,7 @@ "\n", "\n", "class TestLinkedList(object):\n", - " \n", + "\n", " def test_insert_to_front(self):\n", " print('Test: insert_to_front on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -198,9 +198,9 @@ " linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('bc')\n", " assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n", - " \n", + "\n", " print('Success: test_insert_to_front\\n')\n", - " \n", + "\n", " def test_append(self):\n", " print('Test: append on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -215,9 +215,9 @@ " linked_list.append('a')\n", " linked_list.append('bc')\n", " assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n", - " \n", + "\n", " print('Success: test_append\\n')\n", - " \n", + "\n", " def test_find(self):\n", " print('Test: find on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -241,9 +241,9 @@ " print('Test: find general case with no matches')\n", " node = linked_list.find('aaa')\n", " assert_equal(node, None)\n", - " \n", + "\n", " print('Success: test_find\\n')\n", - " \n", + "\n", " def test_delete(self):\n", " print('Test: delete on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -267,9 +267,9 @@ " print('Test: delete general case with no matches')\n", " linked_list.delete('aa')\n", " assert_equal(linked_list.get_all_data(), ['bc', 10])\n", - " \n", + "\n", " print('Success: test_delete\\n')\n", - " \n", + "\n", " def test_len(self):\n", " print('Test: len on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -281,9 +281,10 @@ " linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('bc')\n", " assert_equal(len(linked_list), 3)\n", - " \n", + "\n", " print('Success: test_len\\n')\n", "\n", + "\n", "def main():\n", " test = TestLinkedList()\n", " test.test_insert_to_front()\n", @@ -291,7 +292,8 @@ " test.test_find()\n", " test.test_delete()\n", " test.test_len()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/linked_list/linked_list_solution.ipynb b/linked_lists/linked_list/linked_list_solution.ipynb index df3f261..00f8f9f 100644 --- a/linked_lists/linked_list/linked_list_solution.ipynb +++ b/linked_lists/linked_list/linked_list_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly or doubly linked list?\n", " * Singly\n", "* Is this a circular list?\n", @@ -188,16 +187,17 @@ "source": [ "%%writefile linked_list.py\n", "class Node(object):\n", - " \n", + "\n", " def __init__(self, data, next_node=None):\n", " self.next = next_node\n", " self.data = data\n", - " \n", + "\n", " def __str__(self):\n", " return self.data\n", "\n", + "\n", "class LinkedList(object):\n", - " \n", + "\n", " def __init__(self, head=None):\n", " self.head = head\n", "\n", @@ -208,7 +208,7 @@ " counter += 1\n", " curr = curr.next\n", " return counter\n", - " \n", + "\n", " def insert_to_front(self, data):\n", " if data is None:\n", " return\n", @@ -219,7 +219,7 @@ " node.next = self.head\n", " self.head = node\n", " return node\n", - " \n", + "\n", " def append(self, data, next_node=None):\n", " if data is None:\n", " return\n", @@ -232,7 +232,7 @@ " curr_node = curr_node.next\n", " curr_node.next = node\n", " return node\n", - " \n", + "\n", " def find(self, data):\n", " if data is None:\n", " return\n", @@ -245,7 +245,7 @@ " else:\n", " curr_node = curr_node.next\n", " return\n", - " \n", + "\n", " def delete(self, data):\n", " if data is None:\n", " return\n", @@ -315,7 +315,7 @@ "\n", "\n", "class TestLinkedList(object):\n", - " \n", + "\n", " def test_insert_to_front(self):\n", " print('Test: insert_to_front on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -330,9 +330,9 @@ " linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('bc')\n", " assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n", - " \n", + "\n", " print('Success: test_insert_to_front\\n')\n", - " \n", + "\n", " def test_append(self):\n", " print('Test: append on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -347,9 +347,9 @@ " linked_list.append('a')\n", " linked_list.append('bc')\n", " assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n", - " \n", + "\n", " print('Success: test_append\\n')\n", - " \n", + "\n", " def test_find(self):\n", " print('Test: find on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -373,9 +373,9 @@ " print('Test: find general case with no matches')\n", " node = linked_list.find('aaa')\n", " assert_equal(node, None)\n", - " \n", + "\n", " print('Success: test_find\\n')\n", - " \n", + "\n", " def test_delete(self):\n", " print('Test: delete on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -399,9 +399,9 @@ " print('Test: delete general case with no matches')\n", " linked_list.delete('aa')\n", " assert_equal(linked_list.get_all_data(), ['bc', 10])\n", - " \n", + "\n", " print('Success: test_delete\\n')\n", - " \n", + "\n", " def test_len(self):\n", " print('Test: len on an empty list')\n", " linked_list = LinkedList(None)\n", @@ -413,9 +413,10 @@ " linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('bc')\n", " assert_equal(len(linked_list), 3)\n", - " \n", + "\n", " print('Success: test_len\\n')\n", "\n", + "\n", "def main():\n", " test = TestLinkedList()\n", " test.test_insert_to_front()\n", @@ -423,7 +424,8 @@ " test.test_find()\n", " test.test_delete()\n", " test.test_len()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/linked_list/test_linked_list.py b/linked_lists/linked_list/test_linked_list.py index 794697d..0362d4d 100644 --- a/linked_lists/linked_list/test_linked_list.py +++ b/linked_lists/linked_list/test_linked_list.py @@ -103,6 +103,7 @@ class TestLinkedList(object): print('Success: test_len\n') + def main(): test = TestLinkedList() test.test_insert_to_front() @@ -111,5 +112,6 @@ def main(): test.test_delete() test.test_len() + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/palindrome/palindrome_challenge.ipynb b/linked_lists/palindrome/palindrome_challenge.ipynb index 596e03f..2e9e854 100644 --- a/linked_lists/palindrome/palindrome_challenge.ipynb +++ b/linked_lists/palindrome/palindrome_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is a single character or number a palindrome?\n", " * No\n", "* Can we assume we already have a linked list class that can be used for this problem?\n", @@ -92,7 +91,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def is_palindrome(self):\n", " # TODO: Implement me\n", " pass" @@ -127,7 +126,7 @@ "\n", "\n", "class TestPalindrome(object):\n", - " \n", + "\n", " def test_palindrome(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList()\n", @@ -158,13 +157,15 @@ " linked_list.append(2)\n", " linked_list.append(1)\n", " assert_equal(linked_list.is_palindrome(), True)\n", - " \n", + "\n", " print('Success: test_palindrome')\n", "\n", + "\n", "def main():\n", " test = TestPalindrome()\n", " test.test_palindrome()\n", "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/palindrome/palindrome_solution.ipynb b/linked_lists/palindrome/palindrome_solution.ipynb index 199809b..06681eb 100644 --- a/linked_lists/palindrome/palindrome_solution.ipynb +++ b/linked_lists/palindrome/palindrome_solution.ipynb @@ -103,22 +103,22 @@ "\n", "\n", "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def is_palindrome(self):\n", " if self.head is None or self.head.next is None:\n", " return False\n", " curr = self.head\n", " reversed_list = MyLinkedList()\n", " length = 0\n", - " \n", + "\n", " # Reverse the linked list\n", " while curr is not None:\n", " reversed_list.insert_to_front(curr.data)\n", " length += 1\n", " curr = curr.next\n", - " \n", + "\n", " # Compare the reversed list with the original list\n", - " # Only need to compare the first half \n", + " # Only need to compare the first half\n", " iterations_to_compare_half = length // 2\n", " curr = self.head\n", " curr_reversed = reversed_list.head\n", @@ -158,7 +158,7 @@ "\n", "\n", "class TestPalindrome(object):\n", - " \n", + "\n", " def test_palindrome(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList()\n", @@ -189,13 +189,15 @@ " linked_list.append(2)\n", " linked_list.append(1)\n", " assert_equal(linked_list.is_palindrome(), True)\n", - " \n", + "\n", " print('Success: test_palindrome')\n", "\n", + "\n", "def main():\n", " test = TestPalindrome()\n", " test.test_palindrome()\n", "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/palindrome/test_palindrome.py b/linked_lists/palindrome/test_palindrome.py index c4f3093..6bc078e 100644 --- a/linked_lists/palindrome/test_palindrome.py +++ b/linked_lists/palindrome/test_palindrome.py @@ -2,7 +2,7 @@ from nose.tools import assert_equal class TestPalindrome(object): - + def test_palindrome(self): print('Test: Empty list') linked_list = MyLinkedList() @@ -33,12 +33,14 @@ class TestPalindrome(object): linked_list.append(2) linked_list.append(1) assert_equal(linked_list.is_palindrome(), True) - + print('Success: test_palindrome') + def main(): test = TestPalindrome() test.test_palindrome() + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/partition/partition_challenge.ipynb b/linked_lists/partition/partition_challenge.ipynb index 699f15b..a8c2d11 100644 --- a/linked_lists/partition/partition_challenge.ipynb +++ b/linked_lists/partition/partition_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we create additional data structures?\n", " * Yes\n", "* Do you expect the function to return a new list?\n", @@ -98,7 +97,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def partition(self, data):\n", " # TODO: Implement me\n", " pass" @@ -133,7 +132,7 @@ "\n", "\n", "class TestPartition(object):\n", - " \n", + "\n", " def test_partition(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList(None)\n", @@ -163,15 +162,17 @@ " linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(4)\n", " partitioned_list = linked_list.partition(10)\n", - " assert_equal(partitioned_list.get_all_data(), \n", + " assert_equal(partitioned_list.get_all_data(),\n", " [4, 3, 7, 8, 1, 10, 10, 12])\n", - " \n", + "\n", " print('Success: test_partition')\n", "\n", + "\n", "def main():\n", " test = TestPartition()\n", " test.test_partition()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/partition/partition_solution.ipynb b/linked_lists/partition/partition_solution.ipynb index d6debd2..f6e3904 100644 --- a/linked_lists/partition/partition_solution.ipynb +++ b/linked_lists/partition/partition_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we create additional data structures?\n", " * Yes\n", "* Do you expect the function to return a new list?\n", @@ -104,14 +103,14 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def partition(self, data):\n", " if self.head is None:\n", " return\n", " left = MyLinkedList(None)\n", " right = MyLinkedList(None)\n", " curr = self.head\n", - " \n", + "\n", " # Build the left and right lists\n", " while curr is not None:\n", " if curr.data < data:\n", @@ -158,7 +157,7 @@ "\n", "\n", "class TestPartition(object):\n", - " \n", + "\n", " def test_partition(self):\n", " print('Test: Empty list')\n", " linked_list = MyLinkedList(None)\n", @@ -188,15 +187,17 @@ " linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(4)\n", " partitioned_list = linked_list.partition(10)\n", - " assert_equal(partitioned_list.get_all_data(), \n", + " assert_equal(partitioned_list.get_all_data(),\n", " [4, 3, 7, 8, 1, 10, 10, 12])\n", - " \n", + "\n", " print('Success: test_partition')\n", "\n", + "\n", "def main():\n", " test = TestPartition()\n", " test.test_partition()\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/partition/test_partition.py b/linked_lists/partition/test_partition.py index c3d17e6..99e82d5 100644 --- a/linked_lists/partition/test_partition.py +++ b/linked_lists/partition/test_partition.py @@ -2,7 +2,7 @@ from nose.tools import assert_equal class TestPartition(object): - + def test_partition(self): print('Test: Empty list') linked_list = MyLinkedList(None) @@ -32,14 +32,16 @@ class TestPartition(object): linked_list.insert_to_front(3) linked_list.insert_to_front(4) partitioned_list = linked_list.partition(10) - assert_equal(partitioned_list.get_all_data(), + assert_equal(partitioned_list.get_all_data(), [4, 3, 7, 8, 1, 10, 10, 12]) - + print('Success: test_partition') + def main(): test = TestPartition() test.test_partition() - + + if __name__ == '__main__': main() \ No newline at end of file diff --git a/linked_lists/remove_duplicates/remove_duplicates_challenge.ipynb b/linked_lists/remove_duplicates/remove_duplicates_challenge.ipynb index f734ea8..81641a5 100644 --- a/linked_lists/remove_duplicates/remove_duplicates_challenge.ipynb +++ b/linked_lists/remove_duplicates/remove_duplicates_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly or doubly linked list?\n", " * Singly\n", "* Can you insert None values in the list?\n", @@ -94,7 +93,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def remove_dupes(self):\n", " # TODO: Implement me\n", " pass" @@ -129,7 +128,7 @@ "\n", "\n", "class TestRemoveDupes(object):\n", - " \n", + "\n", " def test_remove_dupes(self, linked_list):\n", " print('Test: Empty list')\n", " linked_list.remove_dupes()\n", @@ -151,14 +150,16 @@ " print('Test: General case, no duplicates')\n", " linked_list.remove_dupes()\n", " assert_equal(linked_list.get_all_data(), [1, 3, 2])\n", - " \n", + "\n", " print('Success: test_remove_dupes\\n')\n", "\n", + "\n", "def main():\n", " test = TestRemoveDupes()\n", " linked_list = MyLinkedList(None)\n", " test.test_remove_dupes(linked_list)\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb b/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb index fa28341..43a0163 100644 --- a/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb +++ b/linked_lists/remove_duplicates/remove_duplicates_solution.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is this a singly or doubly linked list?\n", " * Singly\n", "* Can you insert None values in the list?\n", @@ -126,7 +125,7 @@ "outputs": [], "source": [ "class MyLinkedList(LinkedList):\n", - " \n", + "\n", " def remove_dupes(self):\n", " seen_data = set()\n", " curr = self.head\n", @@ -138,7 +137,7 @@ " seen_data.add(curr.data)\n", " prev = curr\n", " curr = curr.next\n", - " \n", + "\n", " def remove_dupes_in_place(self):\n", " curr = self.head\n", " while curr is not None:\n", @@ -179,7 +178,7 @@ "\n", "\n", "class TestRemoveDupes(object):\n", - " \n", + "\n", " def test_remove_dupes(self, linked_list):\n", " print('Test: Empty list')\n", " linked_list.remove_dupes()\n", @@ -201,14 +200,16 @@ " print('Test: General case, no duplicates')\n", " linked_list.remove_dupes()\n", " assert_equal(linked_list.get_all_data(), [1, 3, 2])\n", - " \n", + "\n", " print('Success: test_remove_dupes\\n')\n", "\n", + "\n", "def main():\n", " test = TestRemoveDupes()\n", " linked_list = MyLinkedList(None)\n", " test.test_remove_dupes(linked_list)\n", - " \n", + "\n", + "\n", "if __name__ == '__main__':\n", " main()" ] diff --git a/linked_lists/remove_duplicates/test_remove_duplicates.py b/linked_lists/remove_duplicates/test_remove_duplicates.py index 33344d1..34a9085 100644 --- a/linked_lists/remove_duplicates/test_remove_duplicates.py +++ b/linked_lists/remove_duplicates/test_remove_duplicates.py @@ -2,7 +2,7 @@ from nose.tools import assert_equal class TestRemoveDupes(object): - + def test_remove_dupes(self, linked_list): print('Test: Empty list') linked_list.remove_dupes() @@ -24,13 +24,15 @@ class TestRemoveDupes(object): print('Test: General case, no duplicates') linked_list.remove_dupes() assert_equal(linked_list.get_all_data(), [1, 3, 2]) - + print('Success: test_remove_dupes\n') + def main(): test = TestRemoveDupes() linked_list = MyLinkedList(None) test.test_remove_dupes(linked_list) - + + if __name__ == '__main__': main() \ No newline at end of file