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.left = None
|
||||||
self.right = None
|
self.right = None
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.data)
|
||||||
|
|
||||||
|
|
||||||
def insert(root, data):
|
def insert(root, data):
|
||||||
if data <= root.data:
|
if data <= root.data:
|
||||||
if root.left is None:
|
if root.left is None:
|
||||||
|
|
|
@ -91,8 +91,17 @@
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Overwriting bst.py\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"%%writefile bst.py\n",
|
||||||
"class Node(object):\n",
|
"class Node(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self, data):\n",
|
" def __init__(self, data):\n",
|
||||||
|
@ -100,6 +109,9 @@
|
||||||
" self.left = None\n",
|
" self.left = None\n",
|
||||||
" self.right = None\n",
|
" self.right = None\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" def __str__(self):\n",
|
||||||
|
" return str(self.data)\n",
|
||||||
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def insert(root, data):\n",
|
"def insert(root, data):\n",
|
||||||
" if data <= root.data:\n",
|
" if data <= root.data:\n",
|
||||||
|
@ -114,6 +126,17 @@
|
||||||
" insert(root.right, data)"
|
" insert(root.right, data)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run bst.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -123,7 +146,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": true
|
"collapsed": true
|
||||||
},
|
},
|
||||||
|
@ -134,7 +157,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -145,7 +168,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
@ -200,7 +223,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 6,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,6 +8,9 @@ class Node:
|
||||||
self.visited = False
|
self.visited = False
|
||||||
self.adjacent = OrderedDict() # key = node, val = weight
|
self.adjacent = OrderedDict() # key = node, val = weight
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return str(self.id)
|
||||||
|
|
||||||
|
|
||||||
class Graph:
|
class Graph:
|
||||||
|
|
||||||
|
|
|
@ -102,6 +102,9 @@
|
||||||
" # TODO: Implement me\n",
|
" # TODO: Implement me\n",
|
||||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" def __str__(self):\n",
|
||||||
|
" return str(self.id)\n",
|
||||||
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Graph:\n",
|
"class Graph:\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
|
@ -144,6 +144,9 @@
|
||||||
" self.visited = False\n",
|
" self.visited = False\n",
|
||||||
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
" self.adjacent = OrderedDict() # key = node, val = weight\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
" def __str__(self):\n",
|
||||||
|
" return str(self.id)\n",
|
||||||
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Graph:\n",
|
"class Graph:\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
|
@ -119,7 +119,7 @@
|
||||||
" root.visited = True\n",
|
" root.visited = True\n",
|
||||||
" while queue:\n",
|
" while queue:\n",
|
||||||
" node = queue.popleft()\n",
|
" node = queue.popleft()\n",
|
||||||
" visit_func(node.id)\n",
|
" visit_func(node)\n",
|
||||||
" for adjacent_node in node.adjacent:\n",
|
" for adjacent_node in node.adjacent:\n",
|
||||||
" if not adjacent_node.visited:\n",
|
" if not adjacent_node.visited:\n",
|
||||||
" queue.append(adjacent_node)\n",
|
" queue.append(adjacent_node)\n",
|
||||||
|
|
|
@ -109,7 +109,7 @@
|
||||||
"def dfs(root, visit_func):\n",
|
"def dfs(root, visit_func):\n",
|
||||||
" if root is None:\n",
|
" if root is None:\n",
|
||||||
" return\n",
|
" return\n",
|
||||||
" visit_func(root.id)\n",
|
" visit_func(root)\n",
|
||||||
" root.visited = True\n",
|
" root.visited = True\n",
|
||||||
" for node in root.adjacent:\n",
|
" for node in root.adjacent:\n",
|
||||||
" if not node.visited:\n",
|
" if not node.visited:\n",
|
||||||
|
|
|
@ -98,7 +98,7 @@
|
||||||
" queue.append(root)\n",
|
" queue.append(root)\n",
|
||||||
" while queue:\n",
|
" while queue:\n",
|
||||||
" node = queue.popleft()\n",
|
" node = queue.popleft()\n",
|
||||||
" visit_func(node.data)\n",
|
" visit_func(node)\n",
|
||||||
" if node.left is not None:\n",
|
" if node.left is not None:\n",
|
||||||
" queue.append(node.left)\n",
|
" queue.append(node.left)\n",
|
||||||
" if node.right is not None:\n",
|
" if node.right is not None:\n",
|
||||||
|
|
|
@ -136,12 +136,12 @@
|
||||||
"def in_order_traversal(node, visit_func):\n",
|
"def in_order_traversal(node, visit_func):\n",
|
||||||
" if node is not None:\n",
|
" if node is not None:\n",
|
||||||
" in_order_traversal(node.left, visit_func)\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",
|
" in_order_traversal(node.right, visit_func)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def pre_order_traversal(node, visit_func):\n",
|
"def pre_order_traversal(node, visit_func):\n",
|
||||||
" if node is not None:\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.left, visit_func)\n",
|
||||||
" pre_order_traversal(node.right, visit_func)\n",
|
" pre_order_traversal(node.right, visit_func)\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -149,7 +149,7 @@
|
||||||
" if node is not None:\n",
|
" if node is not None:\n",
|
||||||
" post_order_traversal(node.left, visit_func)\n",
|
" post_order_traversal(node.left, visit_func)\n",
|
||||||
" post_order_traversal(node.right, 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 = []
|
self.results = []
|
||||||
|
|
||||||
def add_result(self, result):
|
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):
|
def clear_results(self):
|
||||||
self.results = []
|
self.results = []
|
||||||
|
|
Loading…
Reference in New Issue
Block a user