{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Challenge Notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem: Find a build order given a list of projects and dependencies.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", "* [Algorithm](#Algorithm)\n", "* [Code](#Code)\n", "* [Unit Test](#Unit-Test)\n", "* [Solution Notebook](#Solution-Notebook)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constraints\n", "\n", "* Is it possible to have a cyclic graph as the input?\n", " * Yes\n", "* Can we assume we already have Graph and Node classes?\n", " * Yes\n", "* Can we assume this is a connected graph?\n", " * Yes\n", "* Can we assume the inputs are valid?\n", " * Yes\n", "* Can we assume this fits memory?\n", " * Yes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Test Cases\n", "\n", "* projects: a, b, c, d, e, f, g\n", "* dependencies: (d, g), (f, c), (f, b), (f, a), (c, a), (b, a), (a, e), (b, e)\n", "* output: d, f, c, b, g, a, e\n", "\n", "Note: Edge direction is down\n", "
\n",
" f d\n",
" /|\\ |\n",
" c | b g\n",
" \\|/|\n",
" a |\n",
" |/\n",
" e\n",
"\n",
"\n",
"Test a graph with a cycle, output should be None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](http://nbviewer.jupyter.org/github/donnemartin/interactive-coding-challenges/blob/master/graphs_trees/graph_build_order/build_order_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class Dependency(object):\n",
"\n",
" def __init__(self, node_key_before, node_key_after):\n",
" self.node_key_before = node_key_before\n",
" self.node_key_after = node_key_after"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run ../graph/graph.py\n",
"%load ../graph/graph.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class BuildOrder(object):\n",
"\n",
" def __init__(self, dependencies):\n",
" # TODO: Implement me\n",
" pass\n",
"\n",
" def find_build_order(self):\n",
" # TODO: Implement me\n",
" pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load test_build_order.py\n",
"import unittest\n",
"\n",
"\n",
"class TestBuildOrder(unittest.TestCase):\n",
"\n",
" def __init__(self, *args, **kwargs):\n",
" super(TestBuildOrder, self).__init__()\n",
" self.dependencies = [\n",
" Dependency('d', 'g'),\n",
" Dependency('f', 'c'),\n",
" Dependency('f', 'b'),\n",
" Dependency('f', 'a'),\n",
" Dependency('c', 'a'),\n",
" Dependency('b', 'a'),\n",
" Dependency('a', 'e'),\n",
" Dependency('b', 'e'),\n",
" ]\n",
"\n",
" def test_build_order(self):\n",
" build_order = BuildOrder(self.dependencies)\n",
" processed_nodes = build_order.find_build_order()\n",
"\n",
" expected_result0 = ('d', 'f')\n",
" expected_result1 = ('c', 'b', 'g')\n",
" self.assertTrue(processed_nodes[0].key in expected_result0)\n",
" self.assertTrue(processed_nodes[1].key in expected_result0)\n",
" self.assertTrue(processed_nodes[2].key in expected_result1)\n",
" self.assertTrue(processed_nodes[3].key in expected_result1)\n",
" self.assertTrue(processed_nodes[4].key in expected_result1)\n",
" self.assertTrue(processed_nodes[5].key is 'a')\n",
" self.assertTrue(processed_nodes[6].key is 'e')\n",
"\n",
" print('Success: test_build_order')\n",
"\n",
" def test_build_order_circular(self):\n",
" self.dependencies.append(Dependency('e', 'f'))\n",
" build_order = BuildOrder(self.dependencies)\n",
" processed_nodes = build_order.find_build_order()\n",
" self.assertTrue(processed_nodes is None)\n",
"\n",
" print('Success: test_build_order_circular')\n",
"\n",
"\n",
"def main():\n",
" test = TestBuildOrder()\n",
" test.test_build_order()\n",
" test.test_build_order_circular()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/graphs_trees/build_order/build_order_solution.ipynb) for a discussion on algorithms and code solutions."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}