mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Revised constraints and algorithm. Added comment denoting key/value for each dictionary in the code section.
This commit is contained in:
parent
91dc014573
commit
9dc4116256
|
@ -34,12 +34,10 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* I plan to use a Node and Graph class. \n",
|
"* Is the graph directed?\n",
|
||||||
" * Node will keep track of its id, visit state, and connections. \n",
|
" * Yes\n",
|
||||||
" * Connections will be a dictionary with the key = node id and value being the edge weight\n",
|
"* Do the edges have weights?\n",
|
||||||
" * Graph will hold all of the nodes and have functions to add nodes and edges. \n",
|
" * Yes"
|
||||||
" * Is this in line with what you have in mind?\n",
|
|
||||||
" * Yes"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,12 +33,10 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* I plan to use a Node and Graph class. \n",
|
"* Is the graph directed?\n",
|
||||||
" * Node will keep track of its id, visit state, and connections. \n",
|
" * Yes\n",
|
||||||
" * Connections will be a dictionary with the key = node id and value being the edge weight\n",
|
"* Do the edges have weights?\n",
|
||||||
" * Graph will hold all of the nodes and have functions to add nodes and edges. \n",
|
" * Yes"
|
||||||
" * Is this in line with what you have in mind?\n",
|
|
||||||
" * Yes"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -72,7 +70,23 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\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",
|
"\n",
|
||||||
"* Create a node with the input id\n",
|
"* Create a node with the input id\n",
|
||||||
"* Add the newly created node to the list of nodes\n",
|
"* Add the newly created node to the list of nodes\n",
|
||||||
|
@ -81,7 +95,7 @@
|
||||||
"* Time: O(1)\n",
|
"* Time: O(1)\n",
|
||||||
"* Space: O(1)\n",
|
"* Space: O(1)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### add_edge\n",
|
"#### add_edge\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* If the source node is not in the list of nodes, add it\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",
|
"* If the dest node is not in the list of nodes, add it\n",
|
||||||
|
@ -122,13 +136,13 @@
|
||||||
" def __init__(self, id):\n",
|
" def __init__(self, id):\n",
|
||||||
" self.id = id\n",
|
" self.id = id\n",
|
||||||
" self.state = State.unvisited\n",
|
" self.state = State.unvisited\n",
|
||||||
" self.connections = {}\n",
|
" self.connections = {} # key = node, val = weight\n",
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
"class Graph:\n",
|
"class Graph:\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def __init__(self):\n",
|
" def __init__(self):\n",
|
||||||
" self.nodes = {}\n",
|
" self.nodes = {} # key = node id, val = node\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def add_node(self, id):\n",
|
" def add_node(self, id):\n",
|
||||||
" node = Node(id)\n",
|
" node = Node(id)\n",
|
||||||
|
|
0
graphs_trees/templates/__init__.py
Normal file
0
graphs_trees/templates/__init__.py
Normal file
135
graphs_trees/templates/foo_challenge.ipynb
Normal file
135
graphs_trees/templates/foo_challenge.ipynb
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small><i>This notebook was prepared by [Author](https://github.com/). 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 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
|
||||||
|
}
|
167
graphs_trees/templates/foo_solution.ipynb
Normal file
167
graphs_trees/templates/foo_solution.ipynb
Normal file
|
@ -0,0 +1,167 @@
|
||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"<small><i>This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
19
graphs_trees/templates/test_foo.py
Normal file
19
graphs_trees/templates/test_foo.py
Normal file
|
@ -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()
|
Loading…
Reference in New Issue
Block a user