mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Fix typos (#191)
This commit is contained in:
parent
cd9e9654bc
commit
9f89a51aba
|
@ -51,7 +51,7 @@ Refer to [Accessing the Challenges](https://github.com/donnemartin/interactive-c
|
|||
### Adding New Solutions to Existing Challenges
|
||||
|
||||
Challenges have multiple solutions. If adding new solutions to existing challenges:
|
||||
* Add your algorithm discusion and code solution to the existing notebook
|
||||
* Add your algorithm discussion and code solution to the existing notebook
|
||||
* Update the unit test to include your solution
|
||||
* Verify your code passes the unit tests
|
||||
|
||||
|
|
|
@ -443,7 +443,7 @@ interactive-coding-challenges # Repo
|
|||
├─ ...
|
||||
```
|
||||
|
||||
<i>\*The notebooks (.pynb) read/write the associated unit test (.py) file.</i>
|
||||
<i>\*The notebooks (.ipynb) read/write the associated unit test (.py) file.</i>
|
||||
|
||||
|
||||
## Notebook Installation
|
||||
|
|
|
@ -164,7 +164,7 @@
|
|||
"* Scan each character\n",
|
||||
"* For each character:\n",
|
||||
" * Scan all [other] characters in the array\n",
|
||||
" * Exluding the current character from the scan is rather tricky in Python and results in a non-Pythonic solution\n",
|
||||
" * Excluding the current character from the scan is rather tricky in Python and results in a non-Pythonic solution\n",
|
||||
" * If there is a match, return False\n",
|
||||
"* Return True\n",
|
||||
"\n",
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
"source": [
|
||||
"## Algorithm\n",
|
||||
"\n",
|
||||
"We'll use a recursive solution that valides left <= current < right, passing down the min and max values as we do a depth-first traversal.\n",
|
||||
"We'll use a recursive solution that validates left <= current < right, passing down the min and max values as we do a depth-first traversal.\n",
|
||||
"\n",
|
||||
"* If the node is None, return True\n",
|
||||
"* If min is set and the node's value <= min, return False\n",
|
||||
|
@ -123,12 +123,12 @@
|
|||
" raise TypeError('No root node')\n",
|
||||
" return self._validate(self.root)\n",
|
||||
"\n",
|
||||
" def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
|
||||
" def _validate(self, node, minimum=-sys.maxsize, maximum=sys.maxsize):\n",
|
||||
" if node is None:\n",
|
||||
" return True\n",
|
||||
" if node.data <= mininum or node.data > maximum:\n",
|
||||
" if node.data <= minimum or node.data > maximum:\n",
|
||||
" return False\n",
|
||||
" if not self._validate(node.left, mininum, node.data):\n",
|
||||
" if not self._validate(node.left, minimum, node.data):\n",
|
||||
" return False\n",
|
||||
" if not self._validate(node.right, node.data, maximum):\n",
|
||||
" return False\n",
|
||||
|
|
|
@ -93,17 +93,17 @@
|
|||
"* incoming edge count (useful for algorithms such as topological sort)\n",
|
||||
"* adjacent nodes and edge weights\n",
|
||||
"\n",
|
||||
"#### add_neighhbor\n",
|
||||
"#### add_neighbor\n",
|
||||
"\n",
|
||||
"* If the neighbor doesn't already exist as an adjacent node\n",
|
||||
" * Update the adjancet nodes and edge weights\n",
|
||||
" * Update the adjacent nodes and edge weights\n",
|
||||
" * Increment the neighbor's incoming edge count\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(1)\n",
|
||||
"* Space: O(1)\n",
|
||||
"\n",
|
||||
"#### remove_neighhbor\n",
|
||||
"#### remove_neighbor\n",
|
||||
"\n",
|
||||
"* If the neighbor exists as an adjacent node\n",
|
||||
" * Decrement the neighbor's incoming edge count\n",
|
||||
|
|
|
@ -67,7 +67,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"The constaints state we don't have to check for negative edges, so we test with the general case.\n",
|
||||
"The constraints state we don't have to check for negative edges, so we test with the general case.\n",
|
||||
"\n",
|
||||
"<pre>\n",
|
||||
"graph.add_edge('a', 'b', weight=5)\n",
|
||||
|
|
|
@ -66,7 +66,7 @@
|
|||
"source": [
|
||||
"## Test Cases\n",
|
||||
"\n",
|
||||
"The constaints state we don't have to check for negative edges, so we test with the general case.\n",
|
||||
"The constraints state we don't have to check for negative edges, so we test with the general case.\n",
|
||||
"\n",
|
||||
"<pre>\n",
|
||||
"graph.add_edge('a', 'b', weight=5)\n",
|
||||
|
|
|
@ -112,7 +112,7 @@
|
|||
" self.mean = None\n",
|
||||
" # Mode\n",
|
||||
" self.array = [0] * (upper_limit + 1)\n",
|
||||
" self.mode_ocurrences = 0\n",
|
||||
" self.mode_occurrences = 0\n",
|
||||
" self.mode = None\n",
|
||||
"\n",
|
||||
" def insert(self, val):\n",
|
||||
|
@ -128,8 +128,8 @@
|
|||
" self.mean = self.running_sum / self.num_items\n",
|
||||
" # Calculate the mode\n",
|
||||
" self.array[val] += 1\n",
|
||||
" if self.array[val] > self.mode_ocurrences:\n",
|
||||
" self.mode_ocurrences = self.array[val]\n",
|
||||
" if self.array[val] > self.mode_occurrences:\n",
|
||||
" self.mode_occurrences = self.array[val]\n",
|
||||
" self.mode = val"
|
||||
]
|
||||
},
|
||||
|
|
|
@ -79,7 +79,7 @@
|
|||
"\n",
|
||||
"borrow = (~a&b) << 1 = 0010\n",
|
||||
"\n",
|
||||
"If the borrow is not zero, we'll need to subtract the borrow from the result. Recusively call the function, passing in result and borrow.\n",
|
||||
"If the borrow is not zero, we'll need to subtract the borrow from the result. Recursively call the function, passing in result and borrow.\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(b), where b is the number of bits\n",
|
||||
|
|
|
@ -74,7 +74,7 @@
|
|||
"\n",
|
||||
"carry = (a&b) << 1 = 1010\n",
|
||||
"\n",
|
||||
"If the carry is not zero, we'll need to add the carry to the result. Recusively call the function, passing in result and carry.\n",
|
||||
"If the carry is not zero, we'll need to add the carry to the result. Recursively call the function, passing in result and carry.\n",
|
||||
"\n",
|
||||
"Below are the values of a, b, and the carry of a = 7 and b = 5, producing the result of 12.\n",
|
||||
"\n",
|
||||
|
|
|
@ -100,7 +100,7 @@
|
|||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestMaximingXor(object):\n",
|
||||
"class TestMaximizingXor(object):\n",
|
||||
"\n",
|
||||
" def test_maximizing_xor(self):\n",
|
||||
" solution = Solution()\n",
|
||||
|
@ -109,7 +109,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestMaximingXor()\n",
|
||||
" test = TestMaximizingXor()\n",
|
||||
" test.test_maximizing_xor()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
|
|
@ -122,7 +122,7 @@
|
|||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class TestMaximingXor(object):\n",
|
||||
"class TestMaximizingXor(object):\n",
|
||||
"\n",
|
||||
" def test_maximizing_xor(self):\n",
|
||||
" solution = Solution()\n",
|
||||
|
@ -131,7 +131,7 @@
|
|||
"\n",
|
||||
"\n",
|
||||
"def main():\n",
|
||||
" test = TestMaximingXor()\n",
|
||||
" test = TestMaximizingXor()\n",
|
||||
" test.test_maximizing_xor()\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from nose.tools import assert_equal
|
||||
|
||||
|
||||
class TestMaximingXor(object):
|
||||
class TestMaximizingXor(object):
|
||||
|
||||
def test_maximizing_xor(self):
|
||||
solution = Solution()
|
||||
|
@ -10,7 +10,7 @@ class TestMaximingXor(object):
|
|||
|
||||
|
||||
def main():
|
||||
test = TestMaximingXor()
|
||||
test = TestMaximizingXor()
|
||||
test.test_maximizing_xor()
|
||||
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
" * Yes\n",
|
||||
"* Can we get negative inputs?\n",
|
||||
" * Yes\n",
|
||||
"* Can there be duplicate entires in the input?\n",
|
||||
"* Can there be duplicate entries in the input?\n",
|
||||
" * Yes\n",
|
||||
"* Will there always be at least three integers?\n",
|
||||
" * No\n",
|
||||
|
|
|
@ -37,7 +37,7 @@
|
|||
" * Yes\n",
|
||||
"* Can we get negative inputs?\n",
|
||||
" * Yes\n",
|
||||
"* Can there be duplicate entires in the input?\n",
|
||||
"* Can there be duplicate entries in the input?\n",
|
||||
" * Yes\n",
|
||||
"* Will there always be at least three integers?\n",
|
||||
" * No\n",
|
||||
|
|
|
@ -83,7 +83,7 @@
|
|||
"\n",
|
||||
"borrow = (~a&b) << 1 = 0010\n",
|
||||
"\n",
|
||||
"If the borrow is not zero, we'll need to subtract the borrow from the result. Recusively call the function, passing in result and borrow.\n",
|
||||
"If the borrow is not zero, we'll need to subtract the borrow from the result. Recursively call the function, passing in result and borrow.\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(b), where b is the number of bits\n",
|
||||
|
|
|
@ -78,7 +78,7 @@
|
|||
"\n",
|
||||
"carry = (a&b) << 1 = 1010\n",
|
||||
"\n",
|
||||
"If the carry is not zero, we'll need to add the carry to the result. Recusively call the function, passing in result and carry.\n",
|
||||
"If the carry is not zero, we'll need to add the carry to the result. Recursively call the function, passing in result and carry.\n",
|
||||
"\n",
|
||||
"Below are the values of a, b, and the carry of a = 7 and b = 5, producing the result of 12.\n",
|
||||
"\n",
|
||||
|
|
|
@ -77,7 +77,7 @@
|
|||
" * Else, recurse\n",
|
||||
" * Backtrack by:\n",
|
||||
" * Removing the just added current char from the current results\n",
|
||||
" * Incrementing the current char's acount in the dictionary\n",
|
||||
" * Incrementing the current char's count in the dictionary\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n!)\n",
|
||||
|
|
|
@ -82,7 +82,7 @@
|
|||
" * This avoids duplicate results such as 'ab' and 'bc'\n",
|
||||
" * Backtrack by:\n",
|
||||
" * Removing the just added current char from the current results\n",
|
||||
" * Incrementing the current char's acount in the dictionary\n",
|
||||
" * Incrementing the current char's count in the dictionary\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(2^n)\n",
|
||||
|
|
|
@ -72,7 +72,7 @@
|
|||
" * Break\n",
|
||||
"\n",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n^2) avarage, worst. O(1) best if input is already sorted\n",
|
||||
"* Time: O(n^2) average, worst. O(1) best if input is already sorted\n",
|
||||
"* Space: O(1) for the iterative solution\n",
|
||||
"\n",
|
||||
"Misc: \n",
|
||||
|
|
|
@ -69,7 +69,7 @@
|
|||
"* Merge split arrays\n",
|
||||
" * Using two pointers, one for each half starting at index 0\n",
|
||||
" * Add the smaller element to the result array\n",
|
||||
" * Inrement pointer where smaller element exists\n",
|
||||
" * Increment pointer where smaller element exists\n",
|
||||
" * Copy remaining elements to the result array\n",
|
||||
" * Return result array\n",
|
||||
"\n",
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
"* Is a naive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
" * Yes\n",
|
||||
"* Are duplicates allowed?\n",
|
||||
" * Yes\n",
|
||||
|
|
|
@ -33,7 +33,7 @@
|
|||
"source": [
|
||||
"## Constraints\n",
|
||||
"\n",
|
||||
"* Is a naiive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
"* Is a naive solution sufficient (ie not stable, not based on a heap)?\n",
|
||||
" * Yes\n",
|
||||
"* Are duplicates allowed?\n",
|
||||
" * Yes\n",
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
"* Push/pop on empty stack\n",
|
||||
"* Push/pop on non-empty stack\n",
|
||||
"* Min on empty stack\n",
|
||||
"* Min on non-tempty stack"
|
||||
"* Min on non-empty stack"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -54,7 +54,7 @@
|
|||
"* Push/pop on empty stack\n",
|
||||
"* Push/pop on non-empty stack\n",
|
||||
"* Min on empty stack\n",
|
||||
"* Min on non-tempty stack"
|
||||
"* Min on non-empty stack"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"* [Constraits](#Constraint)\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"* [Constraits](#Constraint)\n",
|
||||
"* [Constraints](#Constraints)\n",
|
||||
"* [Test Cases](#Test-Cases)\n",
|
||||
"* [Algorithm](#Algorithm)\n",
|
||||
"* [Code](#Code)\n",
|
||||
|
|
|
@ -75,25 +75,25 @@ class BinaryTree (object):
|
|||
parent.rightChild = current.rightChild
|
||||
|
||||
else:
|
||||
succesor = current.rightChild
|
||||
succesorParent = current
|
||||
successor = current.rightChild
|
||||
successorParent = current
|
||||
|
||||
while succesor.leftChild is not None:
|
||||
succesorParent = succesor
|
||||
succesor = succesor.leftChild
|
||||
while successor.leftChild is not None:
|
||||
successorParent = successor
|
||||
successor = successor.leftChild
|
||||
|
||||
if current is self.root:
|
||||
self.root = succesor
|
||||
self.root = successor
|
||||
elif isLeft:
|
||||
parent.leftChild = succesor
|
||||
parent.leftChild = successor
|
||||
else:
|
||||
parent.rightChild = succesor
|
||||
parent.rightChild = successor
|
||||
|
||||
succesor.leftChild = current.leftChild
|
||||
successor.leftChild = current.leftChild
|
||||
|
||||
if succesor is not current.rightChild:
|
||||
succesorParent.leftChild = succesor.rightChild
|
||||
succesor.rightChild = current.rightChild
|
||||
if successor is not current.rightChild:
|
||||
successorParent.leftChild = successor.rightChild
|
||||
successor.rightChild = current.rightChild
|
||||
|
||||
return True
|
||||
|
||||
|
|
|
@ -63,12 +63,12 @@
|
|||
"* Here are some basic [instructions](http://www.algolist.net/Data_structures/Binary_search_tree/Removal)\n",
|
||||
"* If the value to delete isn't on the tree return False\n",
|
||||
"\n",
|
||||
"### Traverals \n",
|
||||
"### Traversals \n",
|
||||
"\n",
|
||||
"* In order traversal - left, center, right\n",
|
||||
"* Pre order traversal - center, left, right\n",
|
||||
"* Post order traversal - left, right, center\n",
|
||||
"* Return list for all traverals \n",
|
||||
"* Return list for all traversals \n",
|
||||
"\n",
|
||||
"### Max & Min\n",
|
||||
"* Find the max node in the binary search tree\n",
|
||||
|
|
|
@ -64,12 +64,12 @@
|
|||
"* If the value to delete isn't on the tree return False\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"### Traverals \n",
|
||||
"### Traversals \n",
|
||||
"\n",
|
||||
"* In order traversal - left, center, right\n",
|
||||
"* Pre order traversal - center, left, right\n",
|
||||
"* Post order traversal - left, right, center\n",
|
||||
"* Return list for all traverals \n",
|
||||
"* Return list for all traversals \n",
|
||||
"\n",
|
||||
"### Max & Min\n",
|
||||
"* Find the max node in the binary search tree\n",
|
||||
|
@ -152,7 +152,7 @@
|
|||
" * If node is left child\n",
|
||||
" * Find the biggest value in all the node's children and replace it with it\n",
|
||||
" * If node is right child\n",
|
||||
" * Find the smalles value in all the node's children and replace it with it\n",
|
||||
" * Find the smallest value in all the node's children and replace it with it\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"* Time complexity: O(log(n))\n",
|
||||
|
@ -261,25 +261,25 @@
|
|||
"\t\t\t\tparent.rightChild = current.rightChild\n",
|
||||
"\n",
|
||||
"\t\telse:\n",
|
||||
"\t\t\tsuccesor = current.rightChild\n",
|
||||
"\t\t\tsuccesorParent = current\n",
|
||||
"\t\t\tsuccessor = current.rightChild\n",
|
||||
"\t\t\tsuccessorParent = current\n",
|
||||
"\n",
|
||||
"\t\t\twhile succesor.leftChild is not None:\n",
|
||||
"\t\t\t\tsuccesorParent = succesor\n",
|
||||
"\t\t\t\tsuccesor = succesor.leftChild\n",
|
||||
"\t\t\twhile successor.leftChild is not None:\n",
|
||||
"\t\t\t\tsuccessorParent = successor\n",
|
||||
"\t\t\t\tsuccessor = successor.leftChild\n",
|
||||
"\n",
|
||||
"\t\t\tif current is self.root:\n",
|
||||
"\t\t\t\tself.root = succesor\n",
|
||||
"\t\t\t\tself.root = successor\n",
|
||||
"\t\t\telif isLeft:\n",
|
||||
"\t\t\t\tparent.leftChild = succesor\n",
|
||||
"\t\t\t\tparent.leftChild = successor\n",
|
||||
"\t\t\telse:\n",
|
||||
"\t\t\t\tparent.rightChild = succesor\n",
|
||||
"\t\t\t\tparent.rightChild = successor\n",
|
||||
"\n",
|
||||
"\t\t\tsuccesor.leftChild = current.leftChild\n",
|
||||
"\t\t\tsuccessor.leftChild = current.leftChild\n",
|
||||
"\n",
|
||||
"\t\t\tif succesor is not current.rightChild:\n",
|
||||
"\t\t\t\tsuccesorParent.leftChild = succesor.rightChild\n",
|
||||
"\t\t\t\tsuccesor.rightChild = current.rightChild\n",
|
||||
"\t\t\tif successor is not current.rightChild:\n",
|
||||
"\t\t\t\tsuccessorParent.leftChild = successor.rightChild\n",
|
||||
"\t\t\t\tsuccessor.rightChild = current.rightChild\n",
|
||||
"\n",
|
||||
"\t\treturn True \n",
|
||||
"\n",
|
||||
|
|
|
@ -59,7 +59,7 @@
|
|||
"source": [
|
||||
"## Algorithm: Modified Selection Sort\n",
|
||||
"\n",
|
||||
"* Save the relative position of the first-occurence of each item in a list.\n",
|
||||
"* Save the relative position of the first-occurrence of each item in a list.\n",
|
||||
"* Iterate through list of unique items.\n",
|
||||
" * Keep an outer index; scan rest of list, swapping matching items with outer index and incrementing outer index each time. \n",
|
||||
" \n",
|
||||
|
|
Loading…
Reference in New Issue
Block a user