mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
216 lines
5.4 KiB
Plaintext
216 lines
5.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<small><i>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).</i></small>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Challenge Notebook"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Problem: Implement a basic graph.\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 the graph directed?\n",
|
|
" * Yes\n",
|
|
"* Do the edges have weights?\n",
|
|
" * Yes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Test Cases\n",
|
|
"\n",
|
|
"Input:\n",
|
|
"* `add_edge(source, destination, weight)`\n",
|
|
"\n",
|
|
"```\n",
|
|
"graph.add_edge(0, 1, 5)\n",
|
|
"graph.add_edge(0, 5, 2)\n",
|
|
"graph.add_edge(1, 2, 3)\n",
|
|
"graph.add_edge(2, 3, 4)\n",
|
|
"graph.add_edge(3, 4, 5)\n",
|
|
"graph.add_edge(3, 5, 6)\n",
|
|
"graph.add_edge(4, 0, 7)\n",
|
|
"graph.add_edge(5, 4, 8)\n",
|
|
"graph.add_edge(5, 2, 9)\n",
|
|
"```\n",
|
|
"\n",
|
|
"Result:\n",
|
|
"* `source` and `destination` nodes within `graph` are connected with specified `weight`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Algorithm\n",
|
|
"\n",
|
|
"Refer to the [Solution Notebook](https://github.com/donnemartin/interactive-coding-challenges/graphs_trees/graphs/graph_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": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Python 2 users: Run pip install enum34\n",
|
|
"from enum import Enum\n",
|
|
"\n",
|
|
"\n",
|
|
"class State(Enum):\n",
|
|
" unvisited = 1\n",
|
|
" visited = 2\n",
|
|
" visiting = 3\n",
|
|
"\n",
|
|
"\n",
|
|
"class Node:\n",
|
|
"\n",
|
|
" def __init__(self, id):\n",
|
|
" # TODO: Implement me\n",
|
|
"\n",
|
|
"\n",
|
|
"class Graph:\n",
|
|
"\n",
|
|
" def __init__(self):\n",
|
|
" # TODO: Implement me\n",
|
|
"\n",
|
|
" def add_node(self, id):\n",
|
|
" # TODO: Implement me\n",
|
|
"\n",
|
|
" def add_edge(self, source, dest, weight=0):\n",
|
|
" # TODO: Implement me"
|
|
]
|
|
},
|
|
{
|
|
"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": {
|
|
"collapsed": false
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"# %load test_graph.py\n",
|
|
"from nose.tools import assert_equal\n",
|
|
"\n",
|
|
"\n",
|
|
"class TestGraph(object):\n",
|
|
"\n",
|
|
" def test_graph(self):\n",
|
|
" graph = Graph()\n",
|
|
" for key in range(0, 6):\n",
|
|
" graph.add_node(key)\n",
|
|
" graph.add_edge(0, 1, 5)\n",
|
|
" graph.add_edge(0, 5, 2)\n",
|
|
" graph.add_edge(1, 2, 3)\n",
|
|
" graph.add_edge(2, 3, 4)\n",
|
|
" graph.add_edge(3, 4, 5)\n",
|
|
" graph.add_edge(3, 5, 6)\n",
|
|
" graph.add_edge(4, 0, 7)\n",
|
|
" graph.add_edge(5, 4, 8)\n",
|
|
" graph.add_edge(5, 2, 9)\n",
|
|
"\n",
|
|
" assert_equal(graph.nodes[0].connections[graph.nodes[1]], 5)\n",
|
|
" assert_equal(graph.nodes[0].connections[graph.nodes[5]], 2)\n",
|
|
" assert_equal(graph.nodes[1].connections[graph.nodes[2]], 3)\n",
|
|
" assert_equal(graph.nodes[2].connections[graph.nodes[3]], 4)\n",
|
|
" assert_equal(graph.nodes[3].connections[graph.nodes[4]], 5)\n",
|
|
" assert_equal(graph.nodes[3].connections[graph.nodes[5]], 6)\n",
|
|
" assert_equal(graph.nodes[4].connections[graph.nodes[0]], 7)\n",
|
|
" assert_equal(graph.nodes[5].connections[graph.nodes[4]], 8)\n",
|
|
" assert_equal(graph.nodes[5].connections[graph.nodes[2]], 9)\n",
|
|
"\n",
|
|
" print('Success: test_graph')\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" test = TestGraph()\n",
|
|
" test.test_graph()\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/graphs/graph_solution.ipynb) for a discussion on algorithms and code solutions."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 2",
|
|
"language": "python",
|
|
"name": "python2"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 2
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython2",
|
|
"version": "2.7.10"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 0
|
|
}
|