mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-09 02:48:02 +00:00
Simpilfied breadth first search challenge.
This commit is contained in:
@@ -63,6 +63,18 @@
|
|||||||
"## Code"
|
"## Code"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../bst/bst.py\n",
|
||||||
|
"%load ../bst/bst.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
@@ -71,26 +83,7 @@
|
|||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Node(object):\n",
|
"def bfs(self, visit_func):\n",
|
||||||
"\n",
|
|
||||||
" def __init__(self, data):\n",
|
|
||||||
" self.data = data\n",
|
|
||||||
" self.left = None\n",
|
|
||||||
" self.right = None\n",
|
|
||||||
"\n",
|
|
||||||
" def insert(self, data):\n",
|
|
||||||
" if data <= self.data:\n",
|
|
||||||
" if self.left is None:\n",
|
|
||||||
" self.left = Node(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" self.left.insert(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" if self.right is None:\n",
|
|
||||||
" self.right = Node(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" self.right.insert(data)\n",
|
|
||||||
"\n",
|
|
||||||
" def bfs(self, visit_func):\n",
|
|
||||||
" # TODO: Implement me"
|
" # TODO: Implement me"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -76,6 +76,17 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"%run ../bst/bst.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -84,28 +95,9 @@
|
|||||||
"from collections import deque\n",
|
"from collections import deque\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Node(object):\n",
|
"def bfs(root, visit_func):\n",
|
||||||
"\n",
|
|
||||||
" def __init__(self, data):\n",
|
|
||||||
" self.data = data\n",
|
|
||||||
" self.left = None\n",
|
|
||||||
" self.right = None\n",
|
|
||||||
"\n",
|
|
||||||
" def insert(self, data):\n",
|
|
||||||
" if data <= self.data:\n",
|
|
||||||
" if self.left is None:\n",
|
|
||||||
" self.left = Node(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" self.left.insert(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" if self.right is None:\n",
|
|
||||||
" self.right = Node(data)\n",
|
|
||||||
" else:\n",
|
|
||||||
" self.right.insert(data)\n",
|
|
||||||
"\n",
|
|
||||||
" def bfs(self, visit_func):\n",
|
|
||||||
" queue = deque()\n",
|
" queue = deque()\n",
|
||||||
" queue.append(self)\n",
|
" queue.append(root)\n",
|
||||||
" while len(queue) > 0:\n",
|
" while len(queue) > 0:\n",
|
||||||
" node = queue.popleft()\n",
|
" node = queue.popleft()\n",
|
||||||
" visit_func(node.data)\n",
|
" visit_func(node.data)\n",
|
||||||
@@ -124,7 +116,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"execution_count": 3,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -135,7 +127,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 3,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -157,14 +149,14 @@
|
|||||||
"class TestBfs(object):\n",
|
"class TestBfs(object):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def test_bfs(self):\n",
|
" def test_bfs(self):\n",
|
||||||
" root = Node(5)\n",
|
" node = Node(5)\n",
|
||||||
" root.insert(2)\n",
|
" insert(node, 2)\n",
|
||||||
" root.insert(8)\n",
|
" insert(node, 8)\n",
|
||||||
" root.insert(1)\n",
|
" insert(node, 1)\n",
|
||||||
" root.insert(3)\n",
|
" insert(node, 3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with captured_output() as (out, err):\n",
|
" with captured_output() as (out, err):\n",
|
||||||
" root.bfs(sys.stdout.write)\n",
|
" bfs(node, sys.stdout.write)\n",
|
||||||
" assert_equal(out.getvalue().strip(), '52813')\n",
|
" assert_equal(out.getvalue().strip(), '52813')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_bfs')\n",
|
" print('Success: test_bfs')\n",
|
||||||
@@ -181,7 +173,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 5,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ from nose.tools import assert_equal
|
|||||||
class TestBfs(object):
|
class TestBfs(object):
|
||||||
|
|
||||||
def test_bfs(self):
|
def test_bfs(self):
|
||||||
root = Node(5)
|
node = Node(5)
|
||||||
root.insert(2)
|
insert(node, 2)
|
||||||
root.insert(8)
|
insert(node, 8)
|
||||||
root.insert(1)
|
insert(node, 1)
|
||||||
root.insert(3)
|
insert(node, 3)
|
||||||
|
|
||||||
with captured_output() as (out, err):
|
with captured_output() as (out, err):
|
||||||
root.bfs(sys.stdout.write)
|
bfs(node, sys.stdout.write)
|
||||||
assert_equal(out.getvalue().strip(), '52813')
|
assert_equal(out.getvalue().strip(), '52813')
|
||||||
|
|
||||||
print('Success: test_bfs')
|
print('Success: test_bfs')
|
||||||
|
|||||||
Reference in New Issue
Block a user