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:
Donne Martin
2015-08-05 06:14:44 -04:00
parent 1c5f34eb3e
commit b109b6f8cc
10 changed files with 52 additions and 13 deletions

View File

@@ -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)

View File

@@ -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
},