From 9dc41162567a73f206af9339c78cbd0d771015d1 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 4 Aug 2015 08:09:16 -0400 Subject: [PATCH] Revised constraints and algorithm. Added comment denoting key/value for each dictionary in the code section. --- graphs_trees/graph/graph_challenge.ipynb | 10 +- graphs_trees/graph/graph_solution.ipynb | 34 +++-- graphs_trees/templates/__init__.py | 0 graphs_trees/templates/foo_challenge.ipynb | 135 +++++++++++++++++ graphs_trees/templates/foo_solution.ipynb | 167 +++++++++++++++++++++ graphs_trees/templates/test_foo.py | 19 +++ 6 files changed, 349 insertions(+), 16 deletions(-) create mode 100644 graphs_trees/templates/__init__.py create mode 100644 graphs_trees/templates/foo_challenge.ipynb create mode 100644 graphs_trees/templates/foo_solution.ipynb create mode 100644 graphs_trees/templates/test_foo.py diff --git a/graphs_trees/graph/graph_challenge.ipynb b/graphs_trees/graph/graph_challenge.ipynb index dd08de2..b02578c 100644 --- a/graphs_trees/graph/graph_challenge.ipynb +++ b/graphs_trees/graph/graph_challenge.ipynb @@ -34,12 +34,10 @@ "source": [ "## Constraints\n", "\n", - "* I plan to use a Node and Graph class. \n", - " * Node will keep track of its id, visit state, and connections. \n", - " * Connections will be a dictionary with the key = node id and value being the edge weight\n", - " * Graph will hold all of the nodes and have functions to add nodes and edges. \n", - " * Is this in line with what you have in mind?\n", - " * Yes" + "* Is the graph directed?\n", + " * Yes\n", + "* Do the edges have weights?\n", + " * Yes" ] }, { diff --git a/graphs_trees/graph/graph_solution.ipynb b/graphs_trees/graph/graph_solution.ipynb index acb6055..d9cb43c 100644 --- a/graphs_trees/graph/graph_solution.ipynb +++ b/graphs_trees/graph/graph_solution.ipynb @@ -33,12 +33,10 @@ "source": [ "## Constraints\n", "\n", - "* I plan to use a Node and Graph class. \n", - " * Node will keep track of its id, visit state, and connections. \n", - " * Connections will be a dictionary with the key = node id and value being the edge weight\n", - " * Graph will hold all of the nodes and have functions to add nodes and edges. \n", - " * Is this in line with what you have in mind?\n", - " * Yes" + "* Is the graph directed?\n", + " * Yes\n", + "* Do the edges have weights?\n", + " * Yes" ] }, { @@ -72,7 +70,23 @@ "source": [ "## Algorithm\n", "\n", - "### add_node\n", + "### Node\n", + "\n", + "Node will keep track of its:\n", + "* id\n", + "* visit state\n", + "* connections\n", + " * key: node\n", + " * value: weight\n", + "\n", + "### Graph\n", + "\n", + "Graph will keep track of its:\n", + "* Nodes\n", + " * key: node id\n", + " * value: node\n", + "\n", + "#### add_node\n", "\n", "* Create a node with the input id\n", "* Add the newly created node to the list of nodes\n", @@ -81,7 +95,7 @@ "* Time: O(1)\n", "* Space: O(1)\n", "\n", - "### add_edge\n", + "#### add_edge\n", "\n", "* If the source node is not in the list of nodes, add it\n", "* If the dest node is not in the list of nodes, add it\n", @@ -122,13 +136,13 @@ " def __init__(self, id):\n", " self.id = id\n", " self.state = State.unvisited\n", - " self.connections = {}\n", + " self.connections = {} # key = node, val = weight\n", "\n", "\n", "class Graph:\n", "\n", " def __init__(self):\n", - " self.nodes = {}\n", + " self.nodes = {} # key = node id, val = node\n", "\n", " def add_node(self, id):\n", " node = Node(id)\n", diff --git a/graphs_trees/templates/__init__.py b/graphs_trees/templates/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/graphs_trees/templates/foo_challenge.ipynb b/graphs_trees/templates/foo_challenge.ipynb new file mode 100644 index 0000000..6321eb1 --- /dev/null +++ b/graphs_trees/templates/foo_challenge.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Author](https://github.com/). 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: Implement foo(val), which returns val\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", + "* Does foo do anything else?\n", + " * No" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* foo(val) -> val" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](). 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": [ + "def foo(val):\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": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%load test_foo.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook]() 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 +} diff --git a/graphs_trees/templates/foo_solution.ipynb b/graphs_trees/templates/foo_solution.ipynb new file mode 100644 index 0000000..d4f9eb5 --- /dev/null +++ b/graphs_trees/templates/foo_solution.ipynb @@ -0,0 +1,167 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Implement foo(val), which returns val\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Does foo do anything else?\n", + " * No" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* foo(val) -> val" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Return the input, val\n", + " \n", + "Complexity:\n", + "* Time: O(1)\n", + "* Space: O(1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def foo(val):\n", + " return val" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_foo.py\n" + ] + } + ], + "source": [ + "%%writefile test_foo.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestFoo(object):\n", + "\n", + " def test_foo(self):\n", + " assert_equal(foo(None), None)\n", + " assert_equal(foo(0), 0)\n", + " assert_equal(foo('bar'), 'bar')\n", + " print('Success: test_foo')\n", + "\n", + " \n", + "def main():\n", + " test = TestFoo()\n", + " test.test_foo()\n", + "\n", + " \n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_foo\n" + ] + } + ], + "source": [ + "%run -i test_foo.py" + ] + } + ], + "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 +} diff --git a/graphs_trees/templates/test_foo.py b/graphs_trees/templates/test_foo.py new file mode 100644 index 0000000..8380e56 --- /dev/null +++ b/graphs_trees/templates/test_foo.py @@ -0,0 +1,19 @@ +from nose.tools import assert_equal + + +class TestFoo(object): + + def test_foo(self): + assert_equal(foo(None), None) + assert_equal(foo(0), 0) + assert_equal(foo('bar'), 'bar') + print('Success: test_foo') + + +def main(): + test = TestFoo() + test.test_foo() + + +if __name__ == '__main__': + main() \ No newline at end of file