diff --git a/graphs_trees/bst/bst.py b/graphs_trees/bst/bst.py index ebda949..38d6814 100644 --- a/graphs_trees/bst/bst.py +++ b/graphs_trees/bst/bst.py @@ -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) \ No newline at end of file diff --git a/graphs_trees/bst/bst_solution.ipynb b/graphs_trees/bst/bst_solution.ipynb index 6b471b5..183b1be 100644 --- a/graphs_trees/bst/bst_solution.ipynb +++ b/graphs_trees/bst/bst_solution.ipynb @@ -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 }, diff --git a/graphs_trees/graph/graph.py b/graphs_trees/graph/graph.py index cf0cc67..5379682 100644 --- a/graphs_trees/graph/graph.py +++ b/graphs_trees/graph/graph.py @@ -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: diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index eb26b03..6bb84bf 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -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", diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index 25a5d38..22a2a5a 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -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", diff --git a/graphs_trees/graph_bfs/bfs_solution.ipynb b/graphs_trees/graph_bfs/bfs_solution.ipynb index 94fb659..1825f0b 100644 --- a/graphs_trees/graph_bfs/bfs_solution.ipynb +++ b/graphs_trees/graph_bfs/bfs_solution.ipynb @@ -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", diff --git a/graphs_trees/graph_dfs/dfs_solution.ipynb b/graphs_trees/graph_dfs/dfs_solution.ipynb index ff497d5..3f7f59b 100644 --- a/graphs_trees/graph_dfs/dfs_solution.ipynb +++ b/graphs_trees/graph_dfs/dfs_solution.ipynb @@ -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", diff --git a/graphs_trees/tree_bfs/bfs_solution.ipynb b/graphs_trees/tree_bfs/bfs_solution.ipynb index 6fdf041..6fd38db 100644 --- a/graphs_trees/tree_bfs/bfs_solution.ipynb +++ b/graphs_trees/tree_bfs/bfs_solution.ipynb @@ -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", diff --git a/graphs_trees/tree_dfs/dfs_solution.ipynb b/graphs_trees/tree_dfs/dfs_solution.ipynb index 22ff845..1384bad 100644 --- a/graphs_trees/tree_dfs/dfs_solution.ipynb +++ b/graphs_trees/tree_dfs/dfs_solution.ipynb @@ -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)" ] }, { diff --git a/graphs_trees/utils/results.py b/graphs_trees/utils/results.py index 3ee7465..d570127 100644 --- a/graphs_trees/utils/results.py +++ b/graphs_trees/utils/results.py @@ -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 = []