Changed discussions of NULL to None to be more Pythonic.

This commit is contained in:
Donne Martin
2015-07-06 05:44:06 -04:00
parent 29685c7b35
commit 47e6a23dde
11 changed files with 55 additions and 55 deletions

View File

@@ -85,7 +85,7 @@
"\n", "\n",
"* Get hash index for lookup\n", "* Get hash index for lookup\n",
"* If key exists, return value\n", "* If key exists, return value\n",
"* Else, return NULL\n", "* Else, return None\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",
"* Time: O(1) average and best, O(n) worst\n", "* Time: O(1) average and best, O(n) worst\n",

View File

@@ -70,8 +70,8 @@
"We could solve this with an iterative or a recursive algorithm, both are well suited for this exercise. We'll use a recursive algorithm for practice with recursion. Note this takes an extra space of O(m) where m is the recursion depth.\n", "We could solve this with an iterative or a recursive algorithm, both are well suited for this exercise. We'll use a recursive algorithm for practice with recursion. Note this takes an extra space of O(m) where m is the recursion depth.\n",
"\n", "\n",
"* Base case:\n", "* Base case:\n",
" * if first and second lists are NULL AND carry is zero\n", " * if first and second lists are None AND carry is zero\n",
" * Return NULL\n", " * Return None\n",
"* Recursive case:\n", "* Recursive case:\n",
" * Set `value` to `carry`\n", " * Set `value` to `carry`\n",
" * Add both nodes' `data` to `value`\n", " * Add both nodes' `data` to `value`\n",
@@ -87,7 +87,7 @@
"\n", "\n",
"Notes:\n", "Notes:\n",
"* Careful with adding if the lists differ\n", "* Careful with adding if the lists differ\n",
" * Only add if a node is not NULL\n", " * Only add if a node is not None\n",
" * Alternatively, we could add trailing zeroes to the smaller list" " * Alternatively, we could add trailing zeroes to the smaller list"
] ]
}, },

View File

@@ -36,7 +36,7 @@
"\n", "\n",
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n", "\n",
"* What if the final node is being deleted, for example a single node list? Do we make it a dummy with a NULL value?\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",
" * Yes" " * Yes"

View File

@@ -35,7 +35,7 @@
"\n", "\n",
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n", "*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\n",
"\n", "\n",
"* What if the final node is being deleted, for example a single node list? Do we make it a dummy with a NULL value?\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",
" * Yes" " * Yes"

View File

@@ -52,26 +52,26 @@
"\n", "\n",
"### Insert to Front\n", "### Insert to Front\n",
"\n", "\n",
"* Insert a NULL\n", "* Insert a None\n",
"* Insert in an empty list\n", "* Insert in an empty list\n",
"* Insert in a list with one element or more elements\n", "* Insert in a list with one element or more elements\n",
"\n", "\n",
"### Append\n", "### Append\n",
"\n", "\n",
"* Append a NULL\n", "* Append a None\n",
"* Append in an empty list\n", "* Append in an empty list\n",
"* Insert in a list with one element or more elements\n", "* Insert in a list with one element or more elements\n",
"\n", "\n",
"### Find\n", "### Find\n",
"\n", "\n",
"* Find a NULL\n", "* Find a None\n",
"* Find in an empty list\n", "* Find in an empty list\n",
"* Find in a list with one element or more matching elements\n", "* Find in a list with one element or more matching elements\n",
"* Find in a list with no matches\n", "* Find in a list with no matches\n",
"\n", "\n",
"### Delete\n", "### Delete\n",
"\n", "\n",
"* Delete a NULL\n", "* Delete a None\n",
"* Delete in an empty list\n", "* Delete in an empty list\n",
"* Delete in a list with one element or more matching elements\n", "* Delete in a list with one element or more matching elements\n",
"* Delete in a list with no matches\n", "* Delete in a list with no matches\n",
@@ -191,7 +191,7 @@
" linked_list.insert_to_front(10)\n", " linked_list.insert_to_front(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
" print('Test: insert_to_front on a NULL')\n", " print('Test: insert_to_front on a None')\n",
" linked_list.insert_to_front(None)\n", " linked_list.insert_to_front(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
@@ -208,7 +208,7 @@
" linked_list.append(10)\n", " linked_list.append(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
" print('Test: append a NULL')\n", " print('Test: append a None')\n",
" linked_list.append(None)\n", " linked_list.append(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
@@ -225,7 +225,7 @@
" node = linked_list.find('a')\n", " node = linked_list.find('a')\n",
" assert_equal(node, None)\n", " assert_equal(node, None)\n",
"\n", "\n",
" print('Test: find a NULL')\n", " print('Test: find a None')\n",
" head = Node(10)\n", " head = Node(10)\n",
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" node = linked_list.find(None)\n", " node = linked_list.find(None)\n",
@@ -251,7 +251,7 @@
" linked_list.delete('a')\n", " linked_list.delete('a')\n",
" assert_equal(linked_list.get_all_data(), [])\n", " assert_equal(linked_list.get_all_data(), [])\n",
"\n", "\n",
" print('Test: delete a NULL')\n", " print('Test: delete a None')\n",
" head = Node(10)\n", " head = Node(10)\n",
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" linked_list.delete(None)\n", " linked_list.delete(None)\n",

View File

@@ -51,26 +51,26 @@
"\n", "\n",
"### Insert to Front\n", "### Insert to Front\n",
"\n", "\n",
"* Insert a NULL\n", "* Insert a None\n",
"* Insert in an empty list\n", "* Insert in an empty list\n",
"* Insert in a list with one element or more elements\n", "* Insert in a list with one element or more elements\n",
"\n", "\n",
"### Append\n", "### Append\n",
"\n", "\n",
"* Append a NULL\n", "* Append a None\n",
"* Append in an empty list\n", "* Append in an empty list\n",
"* Insert in a list with one element or more elements\n", "* Insert in a list with one element or more elements\n",
"\n", "\n",
"### Find\n", "### Find\n",
"\n", "\n",
"* Find a NULL\n", "* Find a None\n",
"* Find in an empty list\n", "* Find in an empty list\n",
"* Find in a list with one element or more matching elements\n", "* Find in a list with one element or more matching elements\n",
"* Find in a list with no matches\n", "* Find in a list with no matches\n",
"\n", "\n",
"### Delete\n", "### Delete\n",
"\n", "\n",
"* Delete a NULL\n", "* Delete a None\n",
"* Delete in an empty list\n", "* Delete in an empty list\n",
"* Delete in a list with one element or more matching elements\n", "* Delete in a list with one element or more matching elements\n",
"* Delete in a list with no matches\n", "* Delete in a list with no matches\n",
@@ -93,7 +93,7 @@
"\n", "\n",
"### Insert to Front\n", "### Insert to Front\n",
"\n", "\n",
"* If the data we are inserting is NULL, return\n", "* If the data we are inserting is None, return\n",
"* Create a node with the input data\n", "* Create a node with the input data\n",
"* If this is an empty list\n", "* If this is an empty list\n",
" * Assign the head to the node\n", " * Assign the head to the node\n",
@@ -107,7 +107,7 @@
"\n", "\n",
"### Append\n", "### Append\n",
"\n", "\n",
"* If the data we are inserting is NULL, return\n", "* If the data we are inserting is None, return\n",
"* Create a node with the input data\n", "* Create a node with the input data\n",
"* If this is an empty list\n", "* If this is an empty list\n",
" * Assign the head to the node\n", " * Assign the head to the node\n",
@@ -121,7 +121,7 @@
"\n", "\n",
"### Find\n", "### Find\n",
"\n", "\n",
"* If data we are finding is NULL, return\n", "* If data we are finding is None, return\n",
"* If the list is empty, return\n", "* If the list is empty, return\n",
"* For each node\n", "* For each node\n",
" * If the value is a match, return it\n", " * If the value is a match, return it\n",
@@ -133,7 +133,7 @@
"\n", "\n",
"### Delete\n", "### Delete\n",
"\n", "\n",
"* If data we are deleting is NULL, return\n", "* If data we are deleting is None, return\n",
"* If the list is empty, return\n", "* If the list is empty, return\n",
"* For each node, keep track of previous and current node\n", "* For each node, keep track of previous and current node\n",
" * If the value we are deleting is a match in the current node\n", " * If the value we are deleting is a match in the current node\n",
@@ -323,7 +323,7 @@
" linked_list.insert_to_front(10)\n", " linked_list.insert_to_front(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
" print('Test: insert_to_front on a NULL')\n", " print('Test: insert_to_front on a None')\n",
" linked_list.insert_to_front(None)\n", " linked_list.insert_to_front(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
@@ -340,7 +340,7 @@
" linked_list.append(10)\n", " linked_list.append(10)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
" print('Test: append a NULL')\n", " print('Test: append a None')\n",
" linked_list.append(None)\n", " linked_list.append(None)\n",
" assert_equal(linked_list.get_all_data(), [10])\n", " assert_equal(linked_list.get_all_data(), [10])\n",
"\n", "\n",
@@ -357,7 +357,7 @@
" node = linked_list.find('a')\n", " node = linked_list.find('a')\n",
" assert_equal(node, None)\n", " assert_equal(node, None)\n",
"\n", "\n",
" print('Test: find a NULL')\n", " print('Test: find a None')\n",
" head = Node(10)\n", " head = Node(10)\n",
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" node = linked_list.find(None)\n", " node = linked_list.find(None)\n",
@@ -383,7 +383,7 @@
" linked_list.delete('a')\n", " linked_list.delete('a')\n",
" assert_equal(linked_list.get_all_data(), [])\n", " assert_equal(linked_list.get_all_data(), [])\n",
"\n", "\n",
" print('Test: delete a NULL')\n", " print('Test: delete a None')\n",
" head = Node(10)\n", " head = Node(10)\n",
" linked_list = LinkedList(head)\n", " linked_list = LinkedList(head)\n",
" linked_list.delete(None)\n", " linked_list.delete(None)\n",
@@ -441,23 +441,23 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Test: insert_to_front on an empty list\n", "Test: insert_to_front on an empty list\n",
"Test: insert_to_front on a NULL\n", "Test: insert_to_front on a None\n",
"Test: insert_to_front general case\n", "Test: insert_to_front general case\n",
"Success: test_insert_to_front\n", "Success: test_insert_to_front\n",
"\n", "\n",
"Test: append on an empty list\n", "Test: append on an empty list\n",
"Test: append a NULL\n", "Test: append a None\n",
"Test: append general case\n", "Test: append general case\n",
"Success: test_append\n", "Success: test_append\n",
"\n", "\n",
"Test: find on an empty list\n", "Test: find on an empty list\n",
"Test: find a NULL\n", "Test: find a None\n",
"Test: find general case with matches\n", "Test: find general case with matches\n",
"Test: find general case with no matches\n", "Test: find general case with no matches\n",
"Success: test_find\n", "Success: test_find\n",
"\n", "\n",
"Test: delete on an empty list\n", "Test: delete on an empty list\n",
"Test: delete a NULL\n", "Test: delete a None\n",
"Test: delete general case with matches\n", "Test: delete general case with matches\n",
"Test: delete general case with no matches\n", "Test: delete general case with no matches\n",
"Success: test_delete\n", "Success: test_delete\n",

View File

@@ -2,14 +2,14 @@ from nose.tools import assert_equal
class TestLinkedList(object): class TestLinkedList(object):
def test_insert_to_front(self): def test_insert_to_front(self):
print('Test: insert_to_front on an empty list') print('Test: insert_to_front on an empty list')
linked_list = LinkedList(None) linked_list = LinkedList(None)
linked_list.insert_to_front(10) linked_list.insert_to_front(10)
assert_equal(linked_list.get_all_data(), [10]) assert_equal(linked_list.get_all_data(), [10])
print('Test: insert_to_front on a NULL') print('Test: insert_to_front on a None')
linked_list.insert_to_front(None) linked_list.insert_to_front(None)
assert_equal(linked_list.get_all_data(), [10]) assert_equal(linked_list.get_all_data(), [10])
@@ -17,16 +17,16 @@ class TestLinkedList(object):
linked_list.insert_to_front('a') linked_list.insert_to_front('a')
linked_list.insert_to_front('bc') linked_list.insert_to_front('bc')
assert_equal(linked_list.get_all_data(), ['bc', 'a', 10]) assert_equal(linked_list.get_all_data(), ['bc', 'a', 10])
print('Success: test_insert_to_front\n') print('Success: test_insert_to_front\n')
def test_append(self): def test_append(self):
print('Test: append on an empty list') print('Test: append on an empty list')
linked_list = LinkedList(None) linked_list = LinkedList(None)
linked_list.append(10) linked_list.append(10)
assert_equal(linked_list.get_all_data(), [10]) assert_equal(linked_list.get_all_data(), [10])
print('Test: append a NULL') print('Test: append a None')
linked_list.append(None) linked_list.append(None)
assert_equal(linked_list.get_all_data(), [10]) assert_equal(linked_list.get_all_data(), [10])
@@ -34,16 +34,16 @@ class TestLinkedList(object):
linked_list.append('a') linked_list.append('a')
linked_list.append('bc') linked_list.append('bc')
assert_equal(linked_list.get_all_data(), [10, 'a', 'bc']) assert_equal(linked_list.get_all_data(), [10, 'a', 'bc'])
print('Success: test_append\n') print('Success: test_append\n')
def test_find(self): def test_find(self):
print('Test: find on an empty list') print('Test: find on an empty list')
linked_list = LinkedList(None) linked_list = LinkedList(None)
node = linked_list.find('a') node = linked_list.find('a')
assert_equal(node, None) assert_equal(node, None)
print('Test: find a NULL') print('Test: find a None')
head = Node(10) head = Node(10)
linked_list = LinkedList(head) linked_list = LinkedList(head)
node = linked_list.find(None) node = linked_list.find(None)
@@ -60,16 +60,16 @@ class TestLinkedList(object):
print('Test: find general case with no matches') print('Test: find general case with no matches')
node = linked_list.find('aaa') node = linked_list.find('aaa')
assert_equal(node, None) assert_equal(node, None)
print('Success: test_find\n') print('Success: test_find\n')
def test_delete(self): def test_delete(self):
print('Test: delete on an empty list') print('Test: delete on an empty list')
linked_list = LinkedList(None) linked_list = LinkedList(None)
linked_list.delete('a') linked_list.delete('a')
assert_equal(linked_list.get_all_data(), []) assert_equal(linked_list.get_all_data(), [])
print('Test: delete a NULL') print('Test: delete a None')
head = Node(10) head = Node(10)
linked_list = LinkedList(head) linked_list = LinkedList(head)
linked_list.delete(None) linked_list.delete(None)
@@ -86,9 +86,9 @@ class TestLinkedList(object):
print('Test: delete general case with no matches') print('Test: delete general case with no matches')
linked_list.delete('aa') linked_list.delete('aa')
assert_equal(linked_list.get_all_data(), ['bc', 10]) assert_equal(linked_list.get_all_data(), ['bc', 10])
print('Success: test_delete\n') print('Success: test_delete\n')
def test_len(self): def test_len(self):
print('Test: len on an empty list') print('Test: len on an empty list')
linked_list = LinkedList(None) linked_list = LinkedList(None)
@@ -100,7 +100,7 @@ class TestLinkedList(object):
linked_list.insert_to_front('a') linked_list.insert_to_front('a')
linked_list.insert_to_front('bc') linked_list.insert_to_front('bc')
assert_equal(len(linked_list), 3) assert_equal(len(linked_list), 3)
print('Success: test_len\n') print('Success: test_len\n')
def main(): def main():
@@ -110,6 +110,6 @@ def main():
test.test_find() test.test_find()
test.test_delete() test.test_delete()
test.test_len() test.test_len()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -46,7 +46,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* NULL tower(s)\n", "* None tower(s)\n",
"* 0 disks\n", "* 0 disks\n",
"* 1 disk\n", "* 1 disk\n",
"* 2 or more disks" "* 2 or more disks"
@@ -124,7 +124,7 @@
" buff = Stack()\n", " buff = Stack()\n",
" dest = Stack()\n", " dest = Stack()\n",
"\n", "\n",
" print('Test: NULL towers')\n", " print('Test: None towers')\n",
" hanoi(num_disks, None, None, None)\n", " hanoi(num_disks, None, None, None)\n",
"\n", "\n",
" print('Test: 0 disks')\n", " print('Test: 0 disks')\n",

View File

@@ -45,7 +45,7 @@
"source": [ "source": [
"## Test Cases\n", "## Test Cases\n",
"\n", "\n",
"* NULL tower(s)\n", "* None tower(s)\n",
"* 0 disks\n", "* 0 disks\n",
"* 1 disk\n", "* 1 disk\n",
"* 2 or more disks" "* 2 or more disks"
@@ -141,7 +141,7 @@
" buff = Stack()\n", " buff = Stack()\n",
" dest = Stack()\n", " dest = Stack()\n",
"\n", "\n",
" print('Test: NULL towers')\n", " print('Test: None towers')\n",
" hanoi(num_disks, None, None, None)\n", " hanoi(num_disks, None, None, None)\n",
"\n", "\n",
" print('Test: 0 disks')\n", " print('Test: 0 disks')\n",
@@ -181,7 +181,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Test: NULL towers\n", "Test: None towers\n",
"Test: 0 disks\n", "Test: 0 disks\n",
"Test: 1 disk\n", "Test: 1 disk\n",
"Test: 2 or more disks\n", "Test: 2 or more disks\n",

View File

@@ -2,14 +2,14 @@ from nose.tools import assert_equal
class TestHanoi(object): class TestHanoi(object):
def test_hanoi(self): def test_hanoi(self):
num_disks = 3 num_disks = 3
src = Stack() src = Stack()
buff = Stack() buff = Stack()
dest = Stack() dest = Stack()
print('Test: NULL towers') print('Test: None towers')
hanoi(num_disks, None, None, None) hanoi(num_disks, None, None, None)
print('Test: 0 disks') print('Test: 0 disks')
@@ -33,6 +33,6 @@ class TestHanoi(object):
def main(): def main():
test = TestHanoi() test = TestHanoi()
test.test_hanoi() test.test_hanoi()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@@ -85,7 +85,7 @@
"\n", "\n",
"### Pop\n", "### Pop\n",
"\n", "\n",
"* If stack is empty, return NULL\n", "* If stack is empty, return None\n",
"* Else \n", "* Else \n",
" * Save top's value\n", " * Save top's value\n",
" * Set top to top.next\n", " * Set top to top.next\n",
@@ -97,7 +97,7 @@
"\n", "\n",
"### Peek\n", "### Peek\n",
"\n", "\n",
"* If stack is empty, return NULL\n", "* If stack is empty, return None\n",
"* Else return top's value\n", "* Else return top's value\n",
"\n", "\n",
"Complexity:\n", "Complexity:\n",