Fix #13, PEP8-ify notebooks.

This commit is contained in:
Donne Martin 2015-07-11 15:34:52 -04:00
parent 374d67ff30
commit 04083b2011
25 changed files with 210 additions and 173 deletions

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Do you expect the return to be in reverse order too?\n", "* Do you expect the return to be in reverse order too?\n",
" * Yes\n", " * Yes\n",
"* What if one of the inputs is None?\n", "* What if one of the inputs is None?\n",
@ -98,7 +97,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def add_reverse(self, first_list, second_list):\n", " def add_reverse(self, first_list, second_list):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -133,7 +132,7 @@
"\n", "\n",
"\n", "\n",
"class TestAddReverse(object):\n", "class TestAddReverse(object):\n",
" \n", "\n",
" def test_add_reverse(self):\n", " def test_add_reverse(self):\n",
" print('Test: Empty list(s)')\n", " print('Test: Empty list(s)')\n",
" assert_equal(MyLinkedList().add_reverse(None, None), None)\n", " assert_equal(MyLinkedList().add_reverse(None, None), None)\n",
@ -166,13 +165,15 @@
" second_list.append(7)\n", " second_list.append(7)\n",
" result = MyLinkedList().add_reverse(first_list, second_list)\n", " result = MyLinkedList().add_reverse(first_list, second_list)\n",
" assert_equal(result.get_all_data(), [5, 4, 2, 1])\n", " assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
" \n", "\n",
" print('Success: test_add_reverse')\n", " print('Success: test_add_reverse')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestAddReverse()\n", " test = TestAddReverse()\n",
" test.test_add_reverse()\n", " test.test_add_reverse()\n",
"\n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Do you expect the return to be in reverse order too?\n", "* Do you expect the return to be in reverse order too?\n",
" * Yes\n", " * Yes\n",
"* What if one of the inputs is None?\n", "* What if one of the inputs is None?\n",
@ -117,15 +116,15 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def __add_reverse__(self, first_node, second_node, carry):\n", " def __add_reverse__(self, first_node, second_node, carry):\n",
" if type(carry) != int and carry < 0:\n", " if type(carry) != int and carry < 0:\n",
" raise ValueError('Invalid int argument: carry')\n", " raise ValueError('Invalid int argument: carry')\n",
" \n", "\n",
" # Base case\n", " # Base case\n",
" if first_node is None and second_node is None and carry == 0:\n", " if first_node is None and second_node is None and carry == 0:\n",
" return None\n", " return None\n",
" \n", "\n",
" # Recursive case\n", " # Recursive case\n",
" value = carry\n", " value = carry\n",
" value += first_node.data if first_node is not None else 0\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", " new_carry = 1 if value >= 10 else 0\n",
" remainder = value % 10\n", " remainder = value % 10\n",
" node = Node(remainder)\n", " node = Node(remainder)\n",
" node.next = self.__add_reverse__(first_node.next if first_node is not None else None, \n", " node.next = self.__add_reverse__(\n",
" second_node.next if first_node is not None else None, \n", " first_node.next if first_node is not None else None,\n",
" new_carry)\n", " second_node.next if first_node is not None else None,\n",
" new_carry)\n",
" return node\n", " return node\n",
"\n", "\n",
" def add_reverse(self, first_list, second_list):\n", " def add_reverse(self, first_list, second_list):\n",
@ -173,7 +173,7 @@
"\n", "\n",
"\n", "\n",
"class TestAddReverse(object):\n", "class TestAddReverse(object):\n",
" \n", "\n",
" def test_add_reverse(self):\n", " def test_add_reverse(self):\n",
" print('Test: Empty list(s)')\n", " print('Test: Empty list(s)')\n",
" assert_equal(MyLinkedList().add_reverse(None, None), None)\n", " assert_equal(MyLinkedList().add_reverse(None, None), None)\n",
@ -206,13 +206,15 @@
" second_list.append(7)\n", " second_list.append(7)\n",
" result = MyLinkedList().add_reverse(first_list, second_list)\n", " result = MyLinkedList().add_reverse(first_list, second_list)\n",
" assert_equal(result.get_all_data(), [5, 4, 2, 1])\n", " assert_equal(result.get_all_data(), [5, 4, 2, 1])\n",
" \n", "\n",
" print('Success: test_add_reverse')\n", " print('Success: test_add_reverse')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestAddReverse()\n", " test = TestAddReverse()\n",
" test.test_add_reverse()\n", " test.test_add_reverse()\n",
"\n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestAddReverse(object): class TestAddReverse(object):
def test_add_reverse(self): def test_add_reverse(self):
print('Test: Empty list(s)') print('Test: Empty list(s)')
assert_equal(MyLinkedList().add_reverse(None, None), None) assert_equal(MyLinkedList().add_reverse(None, None), None)
@ -35,12 +35,14 @@ class TestAddReverse(object):
second_list.append(7) second_list.append(7)
result = MyLinkedList().add_reverse(first_list, second_list) result = MyLinkedList().add_reverse(first_list, second_list)
assert_equal(result.get_all_data(), [5, 4, 2, 1]) assert_equal(result.get_all_data(), [5, 4, 2, 1])
print('Success: test_add_reverse') print('Success: test_add_reverse')
def main(): def main():
test = TestAddReverse() test = TestAddReverse()
test.test_add_reverse() test.test_add_reverse()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\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", "* 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", " * Yes\n",
"* Can we assume we already have a linked list class that can be used for this problem?\n", "* Can we assume we already have a linked list class that can be used for this problem?\n",
@ -90,7 +89,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def delete_node(self, node):\n", " def delete_node(self, node):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -125,7 +124,7 @@
"\n", "\n",
"\n", "\n",
"class TestDeleteNode(object):\n", "class TestDeleteNode(object):\n",
" \n", "\n",
" def test_delete_node(self):\n", " def test_delete_node(self):\n",
" print('Test: Empty list, null node to delete')\n", " print('Test: Empty list, null node to delete')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
@ -139,20 +138,22 @@
" assert_equal(linked_list.get_all_data(), [None])\n", " assert_equal(linked_list.get_all_data(), [None])\n",
"\n", "\n",
" print('Test: Multiple nodes')\n", " print('Test: Multiple nodes')\n",
" linked_list = MyLinkedList(None) \n", " linked_list = MyLinkedList(None)\n",
" node0 = linked_list.insert_to_front(1)\n", " node0 = linked_list.insert_to_front(1)\n",
" node1 = linked_list.insert_to_front(3)\n", " node1 = linked_list.insert_to_front(3)\n",
" node2 = linked_list.insert_to_front(4)\n", " node2 = linked_list.insert_to_front(4)\n",
" node3 = linked_list.insert_to_front(1)\n", " node3 = linked_list.insert_to_front(1)\n",
" linked_list.delete_node(node2)\n", " linked_list.delete_node(node2)\n",
" assert_equal(linked_list.get_all_data(), [1, 3, 1])\n", " assert_equal(linked_list.get_all_data(), [1, 3, 1])\n",
" \n", "\n",
" print('Success: test_delete_node')\n", " print('Success: test_delete_node')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestDeleteNode()\n", " test = TestDeleteNode()\n",
" test.test_delete_node()\n", " test.test_delete_node()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\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", "* 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", " * Yes\n",
"* Can we assume we already have a linked list class that can be used for this problem?\n", "* Can we assume we already have a linked list class that can be used for this problem?\n",
@ -95,7 +94,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def delete_node(self, node):\n", " def delete_node(self, node):\n",
" if self.head is None:\n", " if self.head is None:\n",
" return\n", " return\n",
@ -138,7 +137,7 @@
"\n", "\n",
"\n", "\n",
"class TestDeleteNode(object):\n", "class TestDeleteNode(object):\n",
" \n", "\n",
" def test_delete_node(self):\n", " def test_delete_node(self):\n",
" print('Test: Empty list, null node to delete')\n", " print('Test: Empty list, null node to delete')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
@ -152,20 +151,22 @@
" assert_equal(linked_list.get_all_data(), [None])\n", " assert_equal(linked_list.get_all_data(), [None])\n",
"\n", "\n",
" print('Test: Multiple nodes')\n", " print('Test: Multiple nodes')\n",
" linked_list = MyLinkedList(None) \n", " linked_list = MyLinkedList(None)\n",
" node0 = linked_list.insert_to_front(1)\n", " node0 = linked_list.insert_to_front(1)\n",
" node1 = linked_list.insert_to_front(3)\n", " node1 = linked_list.insert_to_front(3)\n",
" node2 = linked_list.insert_to_front(4)\n", " node2 = linked_list.insert_to_front(4)\n",
" node3 = linked_list.insert_to_front(1)\n", " node3 = linked_list.insert_to_front(1)\n",
" linked_list.delete_node(node2)\n", " linked_list.delete_node(node2)\n",
" assert_equal(linked_list.get_all_data(), [1, 3, 1])\n", " assert_equal(linked_list.get_all_data(), [1, 3, 1])\n",
" \n", "\n",
" print('Success: test_delete_node')\n", " print('Success: test_delete_node')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestDeleteNode()\n", " test = TestDeleteNode()\n",
" test.test_delete_node()\n", " test.test_delete_node()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestDeleteNode(object): class TestDeleteNode(object):
def test_delete_node(self): def test_delete_node(self):
print('Test: Empty list, null node to delete') print('Test: Empty list, null node to delete')
linked_list = MyLinkedList(None) linked_list = MyLinkedList(None)
@ -16,19 +16,21 @@ class TestDeleteNode(object):
assert_equal(linked_list.get_all_data(), [None]) assert_equal(linked_list.get_all_data(), [None])
print('Test: Multiple nodes') print('Test: Multiple nodes')
linked_list = MyLinkedList(None) linked_list = MyLinkedList(None)
node0 = linked_list.insert_to_front(1) node0 = linked_list.insert_to_front(1)
node1 = linked_list.insert_to_front(3) node1 = linked_list.insert_to_front(3)
node2 = linked_list.insert_to_front(4) node2 = linked_list.insert_to_front(4)
node3 = linked_list.insert_to_front(1) node3 = linked_list.insert_to_front(1)
linked_list.delete_node(node2) linked_list.delete_node(node2)
assert_equal(linked_list.get_all_data(), [1, 3, 1]) assert_equal(linked_list.get_all_data(), [1, 3, 1])
print('Success: test_delete_node') print('Success: test_delete_node')
def main(): def main():
test = TestDeleteNode() test = TestDeleteNode()
test.test_delete_node() test.test_delete_node()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly linked list?\n", "* Is this a singly linked list?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we are always passed a circular linked list?\n", "* Can we assume we are always passed a circular linked list?\n",
@ -93,7 +92,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def find_loop_start(self):\n", " def find_loop_start(self):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -128,25 +127,25 @@
"\n", "\n",
"\n", "\n",
"class TestFindLoopStart(object):\n", "class TestFindLoopStart(object):\n",
" \n", "\n",
" def test_find_loop_start(self):\n", " def test_find_loop_start(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList()\n", " linked_list = MyLinkedList()\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: One element')\n", " print('Test: Not a circular linked list: One element')\n",
" head = Node(1)\n", " head = Node(1)\n",
" linked_list = MyLinkedList(head)\n", " linked_list = MyLinkedList(head)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: Two elements')\n", " print('Test: Not a circular linked list: Two elements')\n",
" linked_list.append(2)\n", " linked_list.append(2)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: Three or more elements')\n", " print('Test: Not a circular linked list: Three or more elements')\n",
" linked_list.append(3)\n", " linked_list.append(3)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: General case: Circular linked list')\n", " print('Test: General case: Circular linked list')\n",
" node10 = Node(10)\n", " node10 = Node(10)\n",
" node9 = Node(9, node10)\n", " node9 = Node(9, node10)\n",
@ -162,13 +161,15 @@
" node10.next = node3\n", " node10.next = node3\n",
" linked_list = MyLinkedList(node0)\n", " linked_list = MyLinkedList(node0)\n",
" assert_equal(linked_list.find_loop_start(), 3)\n", " assert_equal(linked_list.find_loop_start(), 3)\n",
" \n", "\n",
" print('Success: test_find_loop_start')\n", " print('Success: test_find_loop_start')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestFindLoopStart()\n", " test = TestFindLoopStart()\n",
" test.test_find_loop_start()\n", " test.test_find_loop_start()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly linked list?\n", "* Is this a singly linked list?\n",
" * Yes\n", " * Yes\n",
"* Can we assume we are always passed a circular linked list?\n", "* Can we assume we are always passed a circular linked list?\n",
@ -101,7 +100,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def find_loop_start(self):\n", " def find_loop_start(self):\n",
" if self.head is None or self.head.next is None:\n", " if self.head is None or self.head.next is None:\n",
" return\n", " return\n",
@ -109,7 +108,7 @@
" j = self.head\n", " j = self.head\n",
" i = i.next\n", " i = i.next\n",
" j = j.next.next\n", " j = j.next.next\n",
" \n", "\n",
" # Increment i and j until they meet\n", " # Increment i and j until they meet\n",
" # j is incremented twice as fast as i\n", " # j is incremented twice as fast as i\n",
" while j != i:\n", " while j != i:\n",
@ -117,11 +116,11 @@
" if j is None or j.next is None:\n", " if j is None or j.next is None:\n",
" return\n", " return\n",
" j = j.next.next\n", " j = j.next.next\n",
" \n", "\n",
" # When i and j meet, move j to the head\n", " # When i and j meet, move j to the head\n",
" j = self.head\n", " j = self.head\n",
" \n", "\n",
" # Increment i and j one node at a time until \n", " # Increment i and j one node at a time until\n",
" # they meet, which is the start of the loop\n", " # they meet, which is the start of the loop\n",
" while j != i:\n", " while j != i:\n",
" i = i.next\n", " i = i.next\n",
@ -157,25 +156,25 @@
"\n", "\n",
"\n", "\n",
"class TestFindLoopStart(object):\n", "class TestFindLoopStart(object):\n",
" \n", "\n",
" def test_find_loop_start(self):\n", " def test_find_loop_start(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList()\n", " linked_list = MyLinkedList()\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: One element')\n", " print('Test: Not a circular linked list: One element')\n",
" head = Node(1)\n", " head = Node(1)\n",
" linked_list = MyLinkedList(head)\n", " linked_list = MyLinkedList(head)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: Two elements')\n", " print('Test: Not a circular linked list: Two elements')\n",
" linked_list.append(2)\n", " linked_list.append(2)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: Not a circular linked list: Three or more elements')\n", " print('Test: Not a circular linked list: Three or more elements')\n",
" linked_list.append(3)\n", " linked_list.append(3)\n",
" assert_equal(linked_list.find_loop_start(), None)\n", " assert_equal(linked_list.find_loop_start(), None)\n",
" \n", "\n",
" print('Test: General case: Circular linked list')\n", " print('Test: General case: Circular linked list')\n",
" node10 = Node(10)\n", " node10 = Node(10)\n",
" node9 = Node(9, node10)\n", " node9 = Node(9, node10)\n",
@ -191,13 +190,15 @@
" node10.next = node3\n", " node10.next = node3\n",
" linked_list = MyLinkedList(node0)\n", " linked_list = MyLinkedList(node0)\n",
" assert_equal(linked_list.find_loop_start(), 3)\n", " assert_equal(linked_list.find_loop_start(), 3)\n",
" \n", "\n",
" print('Success: test_find_loop_start')\n", " print('Success: test_find_loop_start')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestFindLoopStart()\n", " test = TestFindLoopStart()\n",
" test.test_find_loop_start()\n", " test.test_find_loop_start()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,25 +2,25 @@ from nose.tools import assert_equal
class TestFindLoopStart(object): class TestFindLoopStart(object):
def test_find_loop_start(self): def test_find_loop_start(self):
print('Test: Empty list') print('Test: Empty list')
linked_list = MyLinkedList() linked_list = MyLinkedList()
assert_equal(linked_list.find_loop_start(), None) assert_equal(linked_list.find_loop_start(), None)
print('Test: Not a circular linked list: One element') print('Test: Not a circular linked list: One element')
head = Node(1) head = Node(1)
linked_list = MyLinkedList(head) linked_list = MyLinkedList(head)
assert_equal(linked_list.find_loop_start(), None) assert_equal(linked_list.find_loop_start(), None)
print('Test: Not a circular linked list: Two elements') print('Test: Not a circular linked list: Two elements')
linked_list.append(2) linked_list.append(2)
assert_equal(linked_list.find_loop_start(), None) assert_equal(linked_list.find_loop_start(), None)
print('Test: Not a circular linked list: Three or more elements') print('Test: Not a circular linked list: Three or more elements')
linked_list.append(3) linked_list.append(3)
assert_equal(linked_list.find_loop_start(), None) assert_equal(linked_list.find_loop_start(), None)
print('Test: General case: Circular linked list') print('Test: General case: Circular linked list')
node10 = Node(10) node10 = Node(10)
node9 = Node(9, node10) node9 = Node(9, node10)
@ -36,12 +36,14 @@ class TestFindLoopStart(object):
node10.next = node3 node10.next = node3
linked_list = MyLinkedList(node0) linked_list = MyLinkedList(node0)
assert_equal(linked_list.find_loop_start(), 3) assert_equal(linked_list.find_loop_start(), 3)
print('Success: test_find_loop_start') print('Success: test_find_loop_start')
def main(): def main():
test = TestFindLoopStart() test = TestFindLoopStart()
test.test_find_loop_start() test.test_find_loop_start()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we assume k is a valid integer?\n", "* Can we assume k is a valid integer?\n",
" * Yes\n", " * Yes\n",
"* If k = 0, does this return the last element?\n", "* If k = 0, does this return the last element?\n",
@ -96,7 +95,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def kth_to_last_elem(self, k):\n", " def kth_to_last_elem(self, k):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -131,33 +130,35 @@
"\n", "\n",
"\n", "\n",
"class Test(object):\n", "class Test(object):\n",
" \n", "\n",
" def test_kth_to_last_elem(self):\n", " def test_kth_to_last_elem(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
" assert_equal(linked_list.kth_to_last_elem(0), None)\n", " assert_equal(linked_list.kth_to_last_elem(0), None)\n",
" \n", "\n",
" print('Test: k >= len(list)')\n", " print('Test: k >= len(list)')\n",
" assert_equal(linked_list.kth_to_last_elem(100), None)\n", " assert_equal(linked_list.kth_to_last_elem(100), None)\n",
" \n", "\n",
" print('Test: One element, k = 0')\n", " print('Test: One element, k = 0')\n",
" head = Node(2)\n", " head = Node(2)\n",
" linked_list = MyLinkedList(head)\n", " linked_list = MyLinkedList(head)\n",
" assert_equal(linked_list.kth_to_last_elem(0), 2)\n", " assert_equal(linked_list.kth_to_last_elem(0), 2)\n",
" \n", "\n",
" print('Test: General case')\n", " print('Test: General case')\n",
" linked_list.insert_to_front(1)\n", " linked_list.insert_to_front(1)\n",
" linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(5)\n", " linked_list.insert_to_front(5)\n",
" linked_list.insert_to_front(7)\n", " linked_list.insert_to_front(7)\n",
" assert_equal(linked_list.kth_to_last_elem(2), 3)\n", " assert_equal(linked_list.kth_to_last_elem(2), 3)\n",
" \n", "\n",
" print('Success: test_kth_to_last_elem')\n", " print('Success: test_kth_to_last_elem')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = Test()\n", " test = Test()\n",
" test.test_kth_to_last_elem()\n", " test.test_kth_to_last_elem()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we assume k is a valid integer?\n", "* Can we assume k is a valid integer?\n",
" * Yes\n", " * Yes\n",
"* If k = 0, does this return the last element?\n", "* If k = 0, does this return the last element?\n",
@ -101,7 +100,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def kth_to_last_elem(self, k):\n", " def kth_to_last_elem(self, k):\n",
" if self.head is None:\n", " if self.head is None:\n",
" return\n", " return\n",
@ -110,15 +109,15 @@
" curr = self.head\n", " curr = self.head\n",
" prev = self.head\n", " prev = self.head\n",
" counter = 0\n", " counter = 0\n",
" \n", "\n",
" # Give current a headstart, incrementing it \n", " # Give current a headstart, incrementing it\n",
" # once for k = 1, twice for k = 2, etc\n", " # once for k = 1, twice for k = 2, etc\n",
" while counter < k:\n", " while counter < k:\n",
" curr = curr.next\n", " curr = curr.next\n",
" counter += 1\n", " counter += 1\n",
" if curr is None:\n", " if curr is None:\n",
" return\n", " return\n",
" \n", "\n",
" # Increment both pointers until current reaches the end\n", " # Increment both pointers until current reaches the end\n",
" while curr.next is not None:\n", " while curr.next is not None:\n",
" curr = curr.next\n", " curr = curr.next\n",
@ -154,33 +153,35 @@
"\n", "\n",
"\n", "\n",
"class Test(object):\n", "class Test(object):\n",
" \n", "\n",
" def test_kth_to_last_elem(self):\n", " def test_kth_to_last_elem(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
" assert_equal(linked_list.kth_to_last_elem(0), None)\n", " assert_equal(linked_list.kth_to_last_elem(0), None)\n",
" \n", "\n",
" print('Test: k >= len(list)')\n", " print('Test: k >= len(list)')\n",
" assert_equal(linked_list.kth_to_last_elem(100), None)\n", " assert_equal(linked_list.kth_to_last_elem(100), None)\n",
" \n", "\n",
" print('Test: One element, k = 0')\n", " print('Test: One element, k = 0')\n",
" head = Node(2)\n", " head = Node(2)\n",
" linked_list = MyLinkedList(head)\n", " linked_list = MyLinkedList(head)\n",
" assert_equal(linked_list.kth_to_last_elem(0), 2)\n", " assert_equal(linked_list.kth_to_last_elem(0), 2)\n",
" \n", "\n",
" print('Test: General case')\n", " print('Test: General case')\n",
" linked_list.insert_to_front(1)\n", " linked_list.insert_to_front(1)\n",
" linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(5)\n", " linked_list.insert_to_front(5)\n",
" linked_list.insert_to_front(7)\n", " linked_list.insert_to_front(7)\n",
" assert_equal(linked_list.kth_to_last_elem(2), 3)\n", " assert_equal(linked_list.kth_to_last_elem(2), 3)\n",
" \n", "\n",
" print('Success: test_kth_to_last_elem')\n", " print('Success: test_kth_to_last_elem')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = Test()\n", " test = Test()\n",
" test.test_kth_to_last_elem()\n", " test.test_kth_to_last_elem()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,32 +2,34 @@ from nose.tools import assert_equal
class Test(object): class Test(object):
def test_kth_to_last_elem(self): def test_kth_to_last_elem(self):
print('Test: Empty list') print('Test: Empty list')
linked_list = MyLinkedList(None) linked_list = MyLinkedList(None)
assert_equal(linked_list.kth_to_last_elem(0), None) assert_equal(linked_list.kth_to_last_elem(0), None)
print('Test: k >= len(list)') print('Test: k >= len(list)')
assert_equal(linked_list.kth_to_last_elem(100), None) assert_equal(linked_list.kth_to_last_elem(100), None)
print('Test: One element, k = 0') print('Test: One element, k = 0')
head = Node(2) head = Node(2)
linked_list = MyLinkedList(head) linked_list = MyLinkedList(head)
assert_equal(linked_list.kth_to_last_elem(0), 2) assert_equal(linked_list.kth_to_last_elem(0), 2)
print('Test: General case') print('Test: General case')
linked_list.insert_to_front(1) linked_list.insert_to_front(1)
linked_list.insert_to_front(3) linked_list.insert_to_front(3)
linked_list.insert_to_front(5) linked_list.insert_to_front(5)
linked_list.insert_to_front(7) linked_list.insert_to_front(7)
assert_equal(linked_list.kth_to_last_elem(2), 3) assert_equal(linked_list.kth_to_last_elem(2), 3)
print('Success: test_kth_to_last_elem') print('Success: test_kth_to_last_elem')
def main(): def main():
test = Test() test = Test()
test.test_kth_to_last_elem() test.test_kth_to_last_elem()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -1,14 +1,15 @@
class Node(object): class Node(object):
def __init__(self, data, next_node=None): def __init__(self, data, next_node=None):
self.next = next_node self.next = next_node
self.data = data self.data = data
def __str__(self): def __str__(self):
return self.data return self.data
class LinkedList(object): class LinkedList(object):
def __init__(self, head=None): def __init__(self, head=None):
self.head = head self.head = head
@ -19,7 +20,7 @@ class LinkedList(object):
counter += 1 counter += 1
curr = curr.next curr = curr.next
return counter return counter
def insert_to_front(self, data): def insert_to_front(self, data):
if data is None: if data is None:
return return
@ -30,7 +31,7 @@ class LinkedList(object):
node.next = self.head node.next = self.head
self.head = node self.head = node
return node return node
def append(self, data, next_node=None): def append(self, data, next_node=None):
if data is None: if data is None:
return return
@ -43,7 +44,7 @@ class LinkedList(object):
curr_node = curr_node.next curr_node = curr_node.next
curr_node.next = node curr_node.next = node
return node return node
def find(self, data): def find(self, data):
if data is None: if data is None:
return return
@ -56,7 +57,7 @@ class LinkedList(object):
else: else:
curr_node = curr_node.next curr_node = curr_node.next
return return
def delete(self, data): def delete(self, data):
if data is None: if data is None:
return return

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly or doubly linked list?\n", "* Is this a singly or doubly linked list?\n",
" * Singly\n", " * Singly\n",
"* Is this a circular list?\n", "* Is this a circular list?\n",
@ -110,17 +109,18 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class Node(object):\n", "class Node(object):\n",
" \n", "\n",
" def __init__(self, data, next_node=None):\n", " def __init__(self, data, next_node=None):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" \n", "\n",
" def __str__(self):\n", " def __str__(self):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
"\n", "\n",
"\n",
"class LinkedList(object):\n", "class LinkedList(object):\n",
" \n", "\n",
" def __init__(self, head=None):\n", " def __init__(self, head=None):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
@ -128,19 +128,19 @@
" def __len__(self):\n", " def __len__(self):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" \n", "\n",
" def insert_to_front(self, data):\n", " def insert_to_front(self, data):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" \n", "\n",
" def append(self, data, next_node=None):\n", " def append(self, data, next_node=None):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" \n", "\n",
" def find(self, data):\n", " def find(self, data):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" \n", "\n",
" def delete(self, data):\n", " def delete(self, data):\n",
" pass\n", " pass\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
@ -183,7 +183,7 @@
"\n", "\n",
"\n", "\n",
"class TestLinkedList(object):\n", "class TestLinkedList(object):\n",
" \n", "\n",
" def test_insert_to_front(self):\n", " def test_insert_to_front(self):\n",
" print('Test: insert_to_front on an empty list')\n", " print('Test: insert_to_front on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -198,9 +198,9 @@
" linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n", " linked_list.insert_to_front('bc')\n",
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n", " assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
" \n", "\n",
" print('Success: test_insert_to_front\\n')\n", " print('Success: test_insert_to_front\\n')\n",
" \n", "\n",
" def test_append(self):\n", " def test_append(self):\n",
" print('Test: append on an empty list')\n", " print('Test: append on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -215,9 +215,9 @@
" linked_list.append('a')\n", " linked_list.append('a')\n",
" linked_list.append('bc')\n", " linked_list.append('bc')\n",
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n", " assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
" \n", "\n",
" print('Success: test_append\\n')\n", " print('Success: test_append\\n')\n",
" \n", "\n",
" def test_find(self):\n", " def test_find(self):\n",
" print('Test: find on an empty list')\n", " print('Test: find on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -241,9 +241,9 @@
" print('Test: find general case with no matches')\n", " print('Test: find general case with no matches')\n",
" node = linked_list.find('aaa')\n", " node = linked_list.find('aaa')\n",
" assert_equal(node, None)\n", " assert_equal(node, None)\n",
" \n", "\n",
" print('Success: test_find\\n')\n", " print('Success: test_find\\n')\n",
" \n", "\n",
" def test_delete(self):\n", " def test_delete(self):\n",
" print('Test: delete on an empty list')\n", " print('Test: delete on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -267,9 +267,9 @@
" print('Test: delete general case with no matches')\n", " print('Test: delete general case with no matches')\n",
" linked_list.delete('aa')\n", " linked_list.delete('aa')\n",
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n", " assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
" \n", "\n",
" print('Success: test_delete\\n')\n", " print('Success: test_delete\\n')\n",
" \n", "\n",
" def test_len(self):\n", " def test_len(self):\n",
" print('Test: len on an empty list')\n", " print('Test: len on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -281,9 +281,10 @@
" linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n", " linked_list.insert_to_front('bc')\n",
" assert_equal(len(linked_list), 3)\n", " assert_equal(len(linked_list), 3)\n",
" \n", "\n",
" print('Success: test_len\\n')\n", " print('Success: test_len\\n')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestLinkedList()\n", " test = TestLinkedList()\n",
" test.test_insert_to_front()\n", " test.test_insert_to_front()\n",
@ -291,7 +292,8 @@
" test.test_find()\n", " test.test_find()\n",
" test.test_delete()\n", " test.test_delete()\n",
" test.test_len()\n", " test.test_len()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly or doubly linked list?\n", "* Is this a singly or doubly linked list?\n",
" * Singly\n", " * Singly\n",
"* Is this a circular list?\n", "* Is this a circular list?\n",
@ -188,16 +187,17 @@
"source": [ "source": [
"%%writefile linked_list.py\n", "%%writefile linked_list.py\n",
"class Node(object):\n", "class Node(object):\n",
" \n", "\n",
" def __init__(self, data, next_node=None):\n", " def __init__(self, data, next_node=None):\n",
" self.next = next_node\n", " self.next = next_node\n",
" self.data = data\n", " self.data = data\n",
" \n", "\n",
" def __str__(self):\n", " def __str__(self):\n",
" return self.data\n", " return self.data\n",
"\n", "\n",
"\n",
"class LinkedList(object):\n", "class LinkedList(object):\n",
" \n", "\n",
" def __init__(self, head=None):\n", " def __init__(self, head=None):\n",
" self.head = head\n", " self.head = head\n",
"\n", "\n",
@ -208,7 +208,7 @@
" counter += 1\n", " counter += 1\n",
" curr = curr.next\n", " curr = curr.next\n",
" return counter\n", " return counter\n",
" \n", "\n",
" def insert_to_front(self, data):\n", " def insert_to_front(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return\n",
@ -219,7 +219,7 @@
" node.next = self.head\n", " node.next = self.head\n",
" self.head = node\n", " self.head = node\n",
" return node\n", " return node\n",
" \n", "\n",
" def append(self, data, next_node=None):\n", " def append(self, data, next_node=None):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return\n",
@ -232,7 +232,7 @@
" curr_node = curr_node.next\n", " curr_node = curr_node.next\n",
" curr_node.next = node\n", " curr_node.next = node\n",
" return node\n", " return node\n",
" \n", "\n",
" def find(self, data):\n", " def find(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return\n",
@ -245,7 +245,7 @@
" else:\n", " else:\n",
" curr_node = curr_node.next\n", " curr_node = curr_node.next\n",
" return\n", " return\n",
" \n", "\n",
" def delete(self, data):\n", " def delete(self, data):\n",
" if data is None:\n", " if data is None:\n",
" return\n", " return\n",
@ -315,7 +315,7 @@
"\n", "\n",
"\n", "\n",
"class TestLinkedList(object):\n", "class TestLinkedList(object):\n",
" \n", "\n",
" def test_insert_to_front(self):\n", " def test_insert_to_front(self):\n",
" print('Test: insert_to_front on an empty list')\n", " print('Test: insert_to_front on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -330,9 +330,9 @@
" linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n", " linked_list.insert_to_front('bc')\n",
" assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n", " assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])\n",
" \n", "\n",
" print('Success: test_insert_to_front\\n')\n", " print('Success: test_insert_to_front\\n')\n",
" \n", "\n",
" def test_append(self):\n", " def test_append(self):\n",
" print('Test: append on an empty list')\n", " print('Test: append on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -347,9 +347,9 @@
" linked_list.append('a')\n", " linked_list.append('a')\n",
" linked_list.append('bc')\n", " linked_list.append('bc')\n",
" assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n", " assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])\n",
" \n", "\n",
" print('Success: test_append\\n')\n", " print('Success: test_append\\n')\n",
" \n", "\n",
" def test_find(self):\n", " def test_find(self):\n",
" print('Test: find on an empty list')\n", " print('Test: find on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -373,9 +373,9 @@
" print('Test: find general case with no matches')\n", " print('Test: find general case with no matches')\n",
" node = linked_list.find('aaa')\n", " node = linked_list.find('aaa')\n",
" assert_equal(node, None)\n", " assert_equal(node, None)\n",
" \n", "\n",
" print('Success: test_find\\n')\n", " print('Success: test_find\\n')\n",
" \n", "\n",
" def test_delete(self):\n", " def test_delete(self):\n",
" print('Test: delete on an empty list')\n", " print('Test: delete on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -399,9 +399,9 @@
" print('Test: delete general case with no matches')\n", " print('Test: delete general case with no matches')\n",
" linked_list.delete('aa')\n", " linked_list.delete('aa')\n",
" assert_equal(linked_list.get_all_data(), ['bc', 10])\n", " assert_equal(linked_list.get_all_data(), ['bc', 10])\n",
" \n", "\n",
" print('Success: test_delete\\n')\n", " print('Success: test_delete\\n')\n",
" \n", "\n",
" def test_len(self):\n", " def test_len(self):\n",
" print('Test: len on an empty list')\n", " print('Test: len on an empty list')\n",
" linked_list = LinkedList(None)\n", " linked_list = LinkedList(None)\n",
@ -413,9 +413,10 @@
" linked_list.insert_to_front('a')\n", " linked_list.insert_to_front('a')\n",
" linked_list.insert_to_front('bc')\n", " linked_list.insert_to_front('bc')\n",
" assert_equal(len(linked_list), 3)\n", " assert_equal(len(linked_list), 3)\n",
" \n", "\n",
" print('Success: test_len\\n')\n", " print('Success: test_len\\n')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestLinkedList()\n", " test = TestLinkedList()\n",
" test.test_insert_to_front()\n", " test.test_insert_to_front()\n",
@ -423,7 +424,8 @@
" test.test_find()\n", " test.test_find()\n",
" test.test_delete()\n", " test.test_delete()\n",
" test.test_len()\n", " test.test_len()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -103,6 +103,7 @@ class TestLinkedList(object):
print('Success: test_len\n') print('Success: test_len\n')
def main(): def main():
test = TestLinkedList() test = TestLinkedList()
test.test_insert_to_front() test.test_insert_to_front()
@ -111,5 +112,6 @@ def main():
test.test_delete() test.test_delete()
test.test_len() test.test_len()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is a single character or number a palindrome?\n", "* Is a single character or number a palindrome?\n",
" * No\n", " * No\n",
"* Can we assume we already have a linked list class that can be used for this problem?\n", "* Can we assume we already have a linked list class that can be used for this problem?\n",
@ -92,7 +91,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def is_palindrome(self):\n", " def is_palindrome(self):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -127,7 +126,7 @@
"\n", "\n",
"\n", "\n",
"class TestPalindrome(object):\n", "class TestPalindrome(object):\n",
" \n", "\n",
" def test_palindrome(self):\n", " def test_palindrome(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList()\n", " linked_list = MyLinkedList()\n",
@ -158,13 +157,15 @@
" linked_list.append(2)\n", " linked_list.append(2)\n",
" linked_list.append(1)\n", " linked_list.append(1)\n",
" assert_equal(linked_list.is_palindrome(), True)\n", " assert_equal(linked_list.is_palindrome(), True)\n",
" \n", "\n",
" print('Success: test_palindrome')\n", " print('Success: test_palindrome')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestPalindrome()\n", " test = TestPalindrome()\n",
" test.test_palindrome()\n", " test.test_palindrome()\n",
"\n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -103,22 +103,22 @@
"\n", "\n",
"\n", "\n",
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def is_palindrome(self):\n", " def is_palindrome(self):\n",
" if self.head is None or self.head.next is None:\n", " if self.head is None or self.head.next is None:\n",
" return False\n", " return False\n",
" curr = self.head\n", " curr = self.head\n",
" reversed_list = MyLinkedList()\n", " reversed_list = MyLinkedList()\n",
" length = 0\n", " length = 0\n",
" \n", "\n",
" # Reverse the linked list\n", " # Reverse the linked list\n",
" while curr is not None:\n", " while curr is not None:\n",
" reversed_list.insert_to_front(curr.data)\n", " reversed_list.insert_to_front(curr.data)\n",
" length += 1\n", " length += 1\n",
" curr = curr.next\n", " curr = curr.next\n",
" \n", "\n",
" # Compare the reversed list with the original list\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", " iterations_to_compare_half = length // 2\n",
" curr = self.head\n", " curr = self.head\n",
" curr_reversed = reversed_list.head\n", " curr_reversed = reversed_list.head\n",
@ -158,7 +158,7 @@
"\n", "\n",
"\n", "\n",
"class TestPalindrome(object):\n", "class TestPalindrome(object):\n",
" \n", "\n",
" def test_palindrome(self):\n", " def test_palindrome(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList()\n", " linked_list = MyLinkedList()\n",
@ -189,13 +189,15 @@
" linked_list.append(2)\n", " linked_list.append(2)\n",
" linked_list.append(1)\n", " linked_list.append(1)\n",
" assert_equal(linked_list.is_palindrome(), True)\n", " assert_equal(linked_list.is_palindrome(), True)\n",
" \n", "\n",
" print('Success: test_palindrome')\n", " print('Success: test_palindrome')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestPalindrome()\n", " test = TestPalindrome()\n",
" test.test_palindrome()\n", " test.test_palindrome()\n",
"\n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestPalindrome(object): class TestPalindrome(object):
def test_palindrome(self): def test_palindrome(self):
print('Test: Empty list') print('Test: Empty list')
linked_list = MyLinkedList() linked_list = MyLinkedList()
@ -33,12 +33,14 @@ class TestPalindrome(object):
linked_list.append(2) linked_list.append(2)
linked_list.append(1) linked_list.append(1)
assert_equal(linked_list.is_palindrome(), True) assert_equal(linked_list.is_palindrome(), True)
print('Success: test_palindrome') print('Success: test_palindrome')
def main(): def main():
test = TestPalindrome() test = TestPalindrome()
test.test_palindrome() test.test_palindrome()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we create additional data structures?\n", "* Can we create additional data structures?\n",
" * Yes\n", " * Yes\n",
"* Do you expect the function to return a new list?\n", "* Do you expect the function to return a new list?\n",
@ -98,7 +97,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def partition(self, data):\n", " def partition(self, data):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -133,7 +132,7 @@
"\n", "\n",
"\n", "\n",
"class TestPartition(object):\n", "class TestPartition(object):\n",
" \n", "\n",
" def test_partition(self):\n", " def test_partition(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
@ -163,15 +162,17 @@
" linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(4)\n", " linked_list.insert_to_front(4)\n",
" partitioned_list = linked_list.partition(10)\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", " [4, 3, 7, 8, 1, 10, 10, 12])\n",
" \n", "\n",
" print('Success: test_partition')\n", " print('Success: test_partition')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestPartition()\n", " test = TestPartition()\n",
" test.test_partition()\n", " test.test_partition()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -33,7 +33,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we create additional data structures?\n", "* Can we create additional data structures?\n",
" * Yes\n", " * Yes\n",
"* Do you expect the function to return a new list?\n", "* Do you expect the function to return a new list?\n",
@ -104,14 +103,14 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def partition(self, data):\n", " def partition(self, data):\n",
" if self.head is None:\n", " if self.head is None:\n",
" return\n", " return\n",
" left = MyLinkedList(None)\n", " left = MyLinkedList(None)\n",
" right = MyLinkedList(None)\n", " right = MyLinkedList(None)\n",
" curr = self.head\n", " curr = self.head\n",
" \n", "\n",
" # Build the left and right lists\n", " # Build the left and right lists\n",
" while curr is not None:\n", " while curr is not None:\n",
" if curr.data < data:\n", " if curr.data < data:\n",
@ -158,7 +157,7 @@
"\n", "\n",
"\n", "\n",
"class TestPartition(object):\n", "class TestPartition(object):\n",
" \n", "\n",
" def test_partition(self):\n", " def test_partition(self):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
@ -188,15 +187,17 @@
" linked_list.insert_to_front(3)\n", " linked_list.insert_to_front(3)\n",
" linked_list.insert_to_front(4)\n", " linked_list.insert_to_front(4)\n",
" partitioned_list = linked_list.partition(10)\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", " [4, 3, 7, 8, 1, 10, 10, 12])\n",
" \n", "\n",
" print('Success: test_partition')\n", " print('Success: test_partition')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestPartition()\n", " test = TestPartition()\n",
" test.test_partition()\n", " test.test_partition()\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestPartition(object): class TestPartition(object):
def test_partition(self): def test_partition(self):
print('Test: Empty list') print('Test: Empty list')
linked_list = MyLinkedList(None) linked_list = MyLinkedList(None)
@ -32,14 +32,16 @@ class TestPartition(object):
linked_list.insert_to_front(3) linked_list.insert_to_front(3)
linked_list.insert_to_front(4) linked_list.insert_to_front(4)
partitioned_list = linked_list.partition(10) 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]) [4, 3, 7, 8, 1, 10, 10, 12])
print('Success: test_partition') print('Success: test_partition')
def main(): def main():
test = TestPartition() test = TestPartition()
test.test_partition() test.test_partition()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly or doubly linked list?\n", "* Is this a singly or doubly linked list?\n",
" * Singly\n", " * Singly\n",
"* Can you insert None values in the list?\n", "* Can you insert None values in the list?\n",
@ -94,7 +93,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def remove_dupes(self):\n", " def remove_dupes(self):\n",
" # TODO: Implement me\n", " # TODO: Implement me\n",
" pass" " pass"
@ -129,7 +128,7 @@
"\n", "\n",
"\n", "\n",
"class TestRemoveDupes(object):\n", "class TestRemoveDupes(object):\n",
" \n", "\n",
" def test_remove_dupes(self, linked_list):\n", " def test_remove_dupes(self, linked_list):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list.remove_dupes()\n", " linked_list.remove_dupes()\n",
@ -151,14 +150,16 @@
" print('Test: General case, no duplicates')\n", " print('Test: General case, no duplicates')\n",
" linked_list.remove_dupes()\n", " linked_list.remove_dupes()\n",
" assert_equal(linked_list.get_all_data(), [1, 3, 2])\n", " assert_equal(linked_list.get_all_data(), [1, 3, 2])\n",
" \n", "\n",
" print('Success: test_remove_dupes\\n')\n", " print('Success: test_remove_dupes\\n')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestRemoveDupes()\n", " test = TestRemoveDupes()\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
" test.test_remove_dupes(linked_list)\n", " test.test_remove_dupes(linked_list)\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -34,7 +34,6 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Is this a singly or doubly linked list?\n", "* Is this a singly or doubly linked list?\n",
" * Singly\n", " * Singly\n",
"* Can you insert None values in the list?\n", "* Can you insert None values in the list?\n",
@ -126,7 +125,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"class MyLinkedList(LinkedList):\n", "class MyLinkedList(LinkedList):\n",
" \n", "\n",
" def remove_dupes(self):\n", " def remove_dupes(self):\n",
" seen_data = set()\n", " seen_data = set()\n",
" curr = self.head\n", " curr = self.head\n",
@ -138,7 +137,7 @@
" seen_data.add(curr.data)\n", " seen_data.add(curr.data)\n",
" prev = curr\n", " prev = curr\n",
" curr = curr.next\n", " curr = curr.next\n",
" \n", "\n",
" def remove_dupes_in_place(self):\n", " def remove_dupes_in_place(self):\n",
" curr = self.head\n", " curr = self.head\n",
" while curr is not None:\n", " while curr is not None:\n",
@ -179,7 +178,7 @@
"\n", "\n",
"\n", "\n",
"class TestRemoveDupes(object):\n", "class TestRemoveDupes(object):\n",
" \n", "\n",
" def test_remove_dupes(self, linked_list):\n", " def test_remove_dupes(self, linked_list):\n",
" print('Test: Empty list')\n", " print('Test: Empty list')\n",
" linked_list.remove_dupes()\n", " linked_list.remove_dupes()\n",
@ -201,14 +200,16 @@
" print('Test: General case, no duplicates')\n", " print('Test: General case, no duplicates')\n",
" linked_list.remove_dupes()\n", " linked_list.remove_dupes()\n",
" assert_equal(linked_list.get_all_data(), [1, 3, 2])\n", " assert_equal(linked_list.get_all_data(), [1, 3, 2])\n",
" \n", "\n",
" print('Success: test_remove_dupes\\n')\n", " print('Success: test_remove_dupes\\n')\n",
"\n", "\n",
"\n",
"def main():\n", "def main():\n",
" test = TestRemoveDupes()\n", " test = TestRemoveDupes()\n",
" linked_list = MyLinkedList(None)\n", " linked_list = MyLinkedList(None)\n",
" test.test_remove_dupes(linked_list)\n", " test.test_remove_dupes(linked_list)\n",
" \n", "\n",
"\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
" main()" " main()"
] ]

View File

@ -2,7 +2,7 @@ from nose.tools import assert_equal
class TestRemoveDupes(object): class TestRemoveDupes(object):
def test_remove_dupes(self, linked_list): def test_remove_dupes(self, linked_list):
print('Test: Empty list') print('Test: Empty list')
linked_list.remove_dupes() linked_list.remove_dupes()
@ -24,13 +24,15 @@ class TestRemoveDupes(object):
print('Test: General case, no duplicates') print('Test: General case, no duplicates')
linked_list.remove_dupes() linked_list.remove_dupes()
assert_equal(linked_list.get_all_data(), [1, 3, 2]) assert_equal(linked_list.get_all_data(), [1, 3, 2])
print('Success: test_remove_dupes\n') print('Success: test_remove_dupes\n')
def main(): def main():
test = TestRemoveDupes() test = TestRemoveDupes()
linked_list = MyLinkedList(None) linked_list = MyLinkedList(None)
test.test_remove_dupes(linked_list) test.test_remove_dupes(linked_list)
if __name__ == '__main__': if __name__ == '__main__':
main() main()