mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-04 00:18:02 +00:00
Changed discussions of NULL to None to be more Pythonic.
This commit is contained in:
@@ -85,7 +85,7 @@
|
||||
"\n",
|
||||
"* Get hash index for lookup\n",
|
||||
"* If key exists, return value\n",
|
||||
"* Else, return NULL\n",
|
||||
"* Else, return None\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1) average and best, O(n) worst\n",
|
||||
|
||||
@@ -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",
|
||||
"\n",
|
||||
"* Base case:\n",
|
||||
" * if first and second lists are NULL AND carry is zero\n",
|
||||
" * Return NULL\n",
|
||||
" * if first and second lists are None AND carry is zero\n",
|
||||
" * Return None\n",
|
||||
"* Recursive case:\n",
|
||||
" * Set `value` to `carry`\n",
|
||||
" * Add both nodes' `data` to `value`\n",
|
||||
@@ -87,7 +87,7 @@
|
||||
"\n",
|
||||
"Notes:\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"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
"\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\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",
|
||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||
" * Yes"
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
"\n",
|
||||
"*Problem statements are sometimes ambiguous. Identifying constraints and stating assumptions can help to ensure you code the intended solution.*\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",
|
||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||
" * Yes"
|
||||
|
||||
@@ -52,26 +52,26 @@
|
||||
"\n",
|
||||
"### Insert to Front\n",
|
||||
"\n",
|
||||
"* Insert a NULL\n",
|
||||
"* Insert a None\n",
|
||||
"* Insert in an empty list\n",
|
||||
"* Insert in a list with one element or more elements\n",
|
||||
"\n",
|
||||
"### Append\n",
|
||||
"\n",
|
||||
"* Append a NULL\n",
|
||||
"* Append a None\n",
|
||||
"* Append in an empty list\n",
|
||||
"* Insert in a list with one element or more elements\n",
|
||||
"\n",
|
||||
"### Find\n",
|
||||
"\n",
|
||||
"* Find a NULL\n",
|
||||
"* Find a None\n",
|
||||
"* Find in an empty list\n",
|
||||
"* Find in a list with one element or more matching elements\n",
|
||||
"* Find in a list with no matches\n",
|
||||
"\n",
|
||||
"### Delete\n",
|
||||
"\n",
|
||||
"* Delete a NULL\n",
|
||||
"* Delete a None\n",
|
||||
"* Delete in an empty list\n",
|
||||
"* Delete in a list with one element or more matching elements\n",
|
||||
"* Delete in a list with no matches\n",
|
||||
@@ -191,7 +191,7 @@
|
||||
" linked_list.insert_to_front(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\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",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
@@ -208,7 +208,7 @@
|
||||
" linked_list.append(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: append a NULL')\n",
|
||||
" print('Test: append a None')\n",
|
||||
" linked_list.append(None)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
@@ -225,7 +225,7 @@
|
||||
" node = linked_list.find('a')\n",
|
||||
" assert_equal(node, None)\n",
|
||||
"\n",
|
||||
" print('Test: find a NULL')\n",
|
||||
" print('Test: find a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" node = linked_list.find(None)\n",
|
||||
@@ -251,7 +251,7 @@
|
||||
" linked_list.delete('a')\n",
|
||||
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||
"\n",
|
||||
" print('Test: delete a NULL')\n",
|
||||
" print('Test: delete a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" linked_list.delete(None)\n",
|
||||
|
||||
@@ -51,26 +51,26 @@
|
||||
"\n",
|
||||
"### Insert to Front\n",
|
||||
"\n",
|
||||
"* Insert a NULL\n",
|
||||
"* Insert a None\n",
|
||||
"* Insert in an empty list\n",
|
||||
"* Insert in a list with one element or more elements\n",
|
||||
"\n",
|
||||
"### Append\n",
|
||||
"\n",
|
||||
"* Append a NULL\n",
|
||||
"* Append a None\n",
|
||||
"* Append in an empty list\n",
|
||||
"* Insert in a list with one element or more elements\n",
|
||||
"\n",
|
||||
"### Find\n",
|
||||
"\n",
|
||||
"* Find a NULL\n",
|
||||
"* Find a None\n",
|
||||
"* Find in an empty list\n",
|
||||
"* Find in a list with one element or more matching elements\n",
|
||||
"* Find in a list with no matches\n",
|
||||
"\n",
|
||||
"### Delete\n",
|
||||
"\n",
|
||||
"* Delete a NULL\n",
|
||||
"* Delete a None\n",
|
||||
"* Delete in an empty list\n",
|
||||
"* Delete in a list with one element or more matching elements\n",
|
||||
"* Delete in a list with no matches\n",
|
||||
@@ -93,7 +93,7 @@
|
||||
"\n",
|
||||
"### Insert to Front\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",
|
||||
"* If this is an empty list\n",
|
||||
" * Assign the head to the node\n",
|
||||
@@ -107,7 +107,7 @@
|
||||
"\n",
|
||||
"### Append\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",
|
||||
"* If this is an empty list\n",
|
||||
" * Assign the head to the node\n",
|
||||
@@ -121,7 +121,7 @@
|
||||
"\n",
|
||||
"### Find\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",
|
||||
"* For each node\n",
|
||||
" * If the value is a match, return it\n",
|
||||
@@ -133,7 +133,7 @@
|
||||
"\n",
|
||||
"### Delete\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",
|
||||
"* 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",
|
||||
@@ -323,7 +323,7 @@
|
||||
" linked_list.insert_to_front(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\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",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
@@ -340,7 +340,7 @@
|
||||
" linked_list.append(10)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
" print('Test: append a NULL')\n",
|
||||
" print('Test: append a None')\n",
|
||||
" linked_list.append(None)\n",
|
||||
" assert_equal(linked_list.get_all_data(), [10])\n",
|
||||
"\n",
|
||||
@@ -357,7 +357,7 @@
|
||||
" node = linked_list.find('a')\n",
|
||||
" assert_equal(node, None)\n",
|
||||
"\n",
|
||||
" print('Test: find a NULL')\n",
|
||||
" print('Test: find a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" node = linked_list.find(None)\n",
|
||||
@@ -383,7 +383,7 @@
|
||||
" linked_list.delete('a')\n",
|
||||
" assert_equal(linked_list.get_all_data(), [])\n",
|
||||
"\n",
|
||||
" print('Test: delete a NULL')\n",
|
||||
" print('Test: delete a None')\n",
|
||||
" head = Node(10)\n",
|
||||
" linked_list = LinkedList(head)\n",
|
||||
" linked_list.delete(None)\n",
|
||||
@@ -441,23 +441,23 @@
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"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",
|
||||
"Success: test_insert_to_front\n",
|
||||
"\n",
|
||||
"Test: append on an empty list\n",
|
||||
"Test: append a NULL\n",
|
||||
"Test: append a None\n",
|
||||
"Test: append general case\n",
|
||||
"Success: test_append\n",
|
||||
"\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 no matches\n",
|
||||
"Success: test_find\n",
|
||||
"\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 no matches\n",
|
||||
"Success: test_delete\n",
|
||||
|
||||
@@ -9,7 +9,7 @@ class TestLinkedList(object):
|
||||
linked_list.insert_to_front(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)
|
||||
assert_equal(linked_list.get_all_data(), [10])
|
||||
|
||||
@@ -26,7 +26,7 @@ class TestLinkedList(object):
|
||||
linked_list.append(10)
|
||||
assert_equal(linked_list.get_all_data(), [10])
|
||||
|
||||
print('Test: append a NULL')
|
||||
print('Test: append a None')
|
||||
linked_list.append(None)
|
||||
assert_equal(linked_list.get_all_data(), [10])
|
||||
|
||||
@@ -43,7 +43,7 @@ class TestLinkedList(object):
|
||||
node = linked_list.find('a')
|
||||
assert_equal(node, None)
|
||||
|
||||
print('Test: find a NULL')
|
||||
print('Test: find a None')
|
||||
head = Node(10)
|
||||
linked_list = LinkedList(head)
|
||||
node = linked_list.find(None)
|
||||
@@ -69,7 +69,7 @@ class TestLinkedList(object):
|
||||
linked_list.delete('a')
|
||||
assert_equal(linked_list.get_all_data(), [])
|
||||
|
||||
print('Test: delete a NULL')
|
||||
print('Test: delete a None')
|
||||
head = Node(10)
|
||||
linked_list = LinkedList(head)
|
||||
linked_list.delete(None)
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* NULL tower(s)\n",
|
||||
"* None tower(s)\n",
|
||||
"* 0 disks\n",
|
||||
"* 1 disk\n",
|
||||
"* 2 or more disks"
|
||||
@@ -124,7 +124,7 @@
|
||||
" buff = Stack()\n",
|
||||
" dest = Stack()\n",
|
||||
"\n",
|
||||
" print('Test: NULL towers')\n",
|
||||
" print('Test: None towers')\n",
|
||||
" hanoi(num_disks, None, None, None)\n",
|
||||
"\n",
|
||||
" print('Test: 0 disks')\n",
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"* NULL tower(s)\n",
|
||||
"* None tower(s)\n",
|
||||
"* 0 disks\n",
|
||||
"* 1 disk\n",
|
||||
"* 2 or more disks"
|
||||
@@ -141,7 +141,7 @@
|
||||
" buff = Stack()\n",
|
||||
" dest = Stack()\n",
|
||||
"\n",
|
||||
" print('Test: NULL towers')\n",
|
||||
" print('Test: None towers')\n",
|
||||
" hanoi(num_disks, None, None, None)\n",
|
||||
"\n",
|
||||
" print('Test: 0 disks')\n",
|
||||
@@ -181,7 +181,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Test: NULL towers\n",
|
||||
"Test: None towers\n",
|
||||
"Test: 0 disks\n",
|
||||
"Test: 1 disk\n",
|
||||
"Test: 2 or more disks\n",
|
||||
|
||||
@@ -9,7 +9,7 @@ class TestHanoi(object):
|
||||
buff = Stack()
|
||||
dest = Stack()
|
||||
|
||||
print('Test: NULL towers')
|
||||
print('Test: None towers')
|
||||
hanoi(num_disks, None, None, None)
|
||||
|
||||
print('Test: 0 disks')
|
||||
|
||||
@@ -85,7 +85,7 @@
|
||||
"\n",
|
||||
"### Pop\n",
|
||||
"\n",
|
||||
"* If stack is empty, return NULL\n",
|
||||
"* If stack is empty, return None\n",
|
||||
"* Else \n",
|
||||
" * Save top's value\n",
|
||||
" * Set top to top.next\n",
|
||||
@@ -97,7 +97,7 @@
|
||||
"\n",
|
||||
"### Peek\n",
|
||||
"\n",
|
||||
"* If stack is empty, return NULL\n",
|
||||
"* If stack is empty, return None\n",
|
||||
"* Else return top's value\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
|
||||
Reference in New Issue
Block a user