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

@@ -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

View File

@@ -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()"
]

View File

@@ -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()"
]

View File

@@ -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()