mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Simplified challenge coding and unit tests by working with the node directly as opposed to node.id or node.data, which is more natural when writing coding challenges.
This commit is contained in:
parent
1c5f34eb3e
commit
b109b6f8cc
|
@ -5,6 +5,10 @@ class Node(object):
|
|||
self.left = None
|
||||
self.right = None
|
||||
|
||||
def __str__(self):
|
||||
return str(self.data)
|
||||
|
||||
|
||||
def insert(root, data):
|
||||
if data <= root.data:
|
||||
if root.left is None:
|
||||
|
@ -15,4 +19,4 @@ def insert(root, data):
|
|||
if root.right is None:
|
||||
root.right = Node(data)
|
||||
else:
|
||||
insert(root.right, data)
|
||||
insert(root.right, data)
|
|
@ -91,8 +91,17 @@
|
|||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Overwriting bst.py\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%%writefile bst.py\n",
|
||||
"class Node(object):\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
|
@ -100,6 +109,9 @@
|
|||
" self.left = None\n",
|
||||
" self.right = None\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" return str(self.data)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def insert(root, data):\n",
|
||||
" if data <= root.data:\n",
|
||||
|
@ -114,6 +126,17 @@
|
|||
" insert(root.right, data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run bst.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
@ -123,7 +146,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
|
@ -134,7 +157,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -145,7 +168,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -200,7 +223,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
|
|
@ -8,6 +8,9 @@ class Node:
|
|||
self.visited = False
|
||||
self.adjacent = OrderedDict() # key = node, val = weight
|
||||
|
||||
def __str__(self):
|
||||
return str(self.id)
|
||||
|
||||
|
||||
class Graph:
|
||||
|
||||
|
|
|
@ -102,6 +102,9 @@
|
|||
" # TODO: Implement me\n",
|
||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" return str(self.id)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Graph:\n",
|
||||
"\n",
|
||||
|
|
|
@ -144,6 +144,9 @@
|
|||
" self.visited = False\n",
|
||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||
"\n",
|
||||
" def __str__(self):\n",
|
||||
" return str(self.id)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Graph:\n",
|
||||
"\n",
|
||||
|
|
|
@ -119,7 +119,7 @@
|
|||
" root.visited = True\n",
|
||||
" while queue:\n",
|
||||
" node = queue.popleft()\n",
|
||||
" visit_func(node.id)\n",
|
||||
" visit_func(node)\n",
|
||||
" for adjacent_node in node.adjacent:\n",
|
||||
" if not adjacent_node.visited:\n",
|
||||
" queue.append(adjacent_node)\n",
|
||||
|
|
|
@ -109,7 +109,7 @@
|
|||
"def dfs(root, visit_func):\n",
|
||||
" if root is None:\n",
|
||||
" return\n",
|
||||
" visit_func(root.id)\n",
|
||||
" visit_func(root)\n",
|
||||
" root.visited = True\n",
|
||||
" for node in root.adjacent:\n",
|
||||
" if not node.visited:\n",
|
||||
|
|
|
@ -98,7 +98,7 @@
|
|||
" queue.append(root)\n",
|
||||
" while queue:\n",
|
||||
" node = queue.popleft()\n",
|
||||
" visit_func(node.data)\n",
|
||||
" visit_func(node)\n",
|
||||
" if node.left is not None:\n",
|
||||
" queue.append(node.left)\n",
|
||||
" if node.right is not None:\n",
|
||||
|
|
|
@ -136,12 +136,12 @@
|
|||
"def in_order_traversal(node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" in_order_traversal(node.left, visit_func)\n",
|
||||
" visit_func(node.data)\n",
|
||||
" visit_func(node)\n",
|
||||
" in_order_traversal(node.right, visit_func)\n",
|
||||
"\n",
|
||||
"def pre_order_traversal(node, visit_func):\n",
|
||||
" if node is not None:\n",
|
||||
" visit_func(node.data)\n",
|
||||
" visit_func(node)\n",
|
||||
" pre_order_traversal(node.left, visit_func)\n",
|
||||
" pre_order_traversal(node.right, visit_func)\n",
|
||||
"\n",
|
||||
|
@ -149,7 +149,7 @@
|
|||
" if node is not None:\n",
|
||||
" post_order_traversal(node.left, visit_func)\n",
|
||||
" post_order_traversal(node.right, visit_func)\n",
|
||||
" visit_func(node.data)"
|
||||
" visit_func(node)"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -4,7 +4,10 @@ class Results(object):
|
|||
self.results = []
|
||||
|
||||
def add_result(self, result):
|
||||
self.results.append(result)
|
||||
# TODO: Clean this up
|
||||
# Simplifies challenge coding and unit testing
|
||||
# but makes this function look less appealing
|
||||
self.results.append(int(str(result)))
|
||||
|
||||
def clear_results(self):
|
||||
self.results = []
|
||||
|
|
Loading…
Reference in New Issue
Block a user