From 8c23acd55ac31b6cc531121edbe6f0828f10dd9c Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Tue, 7 Jul 2015 17:11:33 -0400 Subject: [PATCH 01/15] edited challenge notebook and added tests --- arrays_strings/group_items/__init__.py | 0 .../group_items/group_items_challenge.ipynb | 158 +++++++++++++++++ arrays_strings/group_items/group_ordered.py | 26 +++ .../group_items/group_ordered_challenge.ipynb | 140 +++++++++++++++ .../group_items/group_ordered_solution.ipynb | 166 ++++++++++++++++++ .../group_items/test_group_ordered.py | 20 +++ 6 files changed, 510 insertions(+) create mode 100644 arrays_strings/group_items/__init__.py create mode 100644 arrays_strings/group_items/group_items_challenge.ipynb create mode 100644 arrays_strings/group_items/group_ordered.py create mode 100644 arrays_strings/group_items/group_ordered_challenge.ipynb create mode 100644 arrays_strings/group_items/group_ordered_solution.ipynb create mode 100644 arrays_strings/group_items/test_group_ordered.py diff --git a/arrays_strings/group_items/__init__.py b/arrays_strings/group_items/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_strings/group_items/group_items_challenge.ipynb b/arrays_strings/group_items/group_items_challenge.ipynb new file mode 100644 index 0000000..869344b --- /dev/null +++ b/arrays_strings/group_items/group_items_challenge.ipynb @@ -0,0 +1,158 @@ +{ + "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 a function that groups identical items based on their order in the list.\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", + "* Can we use extra data structures?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", + "* group_ordered(['a','b','a') -> ['a','a','b']\n", + "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", + "* group_ordered([1,2,3,4,3,4]) -> [1,2,3,3,4,4]\n", + "* group_ordered([]) -> []\n", + "* group_ordered([1]) -> [1]\n", + "* group_ordered(None) -> None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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": [ + "def group_ordered(list_in):\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_group_ordered.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", + "def main():\n", + " test = TestFoo()\n", + " test.test_foo()\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/templates/foo_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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/group_items/group_ordered.py b/arrays_strings/group_items/group_ordered.py new file mode 100644 index 0000000..3de2483 --- /dev/null +++ b/arrays_strings/group_items/group_ordered.py @@ -0,0 +1,26 @@ +def make_order_list(list_in): + order_list = [] + for item in list_in: + if item not in order_list: + order_list.append(item) + return order_list + + +def group_ordered(list_in): + if list_in is None: + return None + order_list = make_order_list(list_in) + current = 0 + for item in order_list: + search = current + 1 + while True: + try: + if list_in[search] != item: + search += 1 + else: + current += 1 + list_in[current], list_in[search] = list_in[search], list_in[current] + search += 1 + except IndexError: + break + return list_in diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb new file mode 100644 index 0000000..e061433 --- /dev/null +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -0,0 +1,140 @@ +{ + "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 a function that groups identical items based on their order in the list.\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", + "* Can we use extra data structures?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", + "* group_ordered(['a','b','a') -> ['a','a','b']\n", + "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", + "* group_ordered([]) -> []\n", + "* group_ordered([1]) -> [1]\n", + "* group_ordered(None) -> None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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": [ + "def group_ordered(list_in):\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_group_ordered.py" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/group_items/group_ordered_solution.ipynb b/arrays_strings/group_items/group_ordered_solution.ipynb new file mode 100644 index 0000000..b7d8b00 --- /dev/null +++ b/arrays_strings/group_items/group_ordered_solution.ipynb @@ -0,0 +1,166 @@ +{ + "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", + + "* Is it foo or bar?\n", + " * foo" + ] + }, + { + "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", + "def main():\n", + " test = TestFoo()\n", + " test.test_foo()\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/arrays_strings/group_items/test_group_ordered.py b/arrays_strings/group_items/test_group_ordered.py new file mode 100644 index 0000000..943ac67 --- /dev/null +++ b/arrays_strings/group_items/test_group_ordered.py @@ -0,0 +1,20 @@ +from nose.tools import assert_equal + +class TestFoo(object): + + def test_foo(self): + assert_equal(group_ordered(None), None) + assert_equal(group_ordered([]), []) + assert_equal(group_ordered([1]), [1]) + assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3]) + assert_equal(group_ordered(['a','b','a']),['a','a','b']) + assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) + assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4]) + print('Success: test_foo') + +def main(): + test = TestFoo() + test.test_foo() + +if __name__ == '__main__': + main() From 84ca16fbb5ad744db4f3280a43a3a19de5efff0b Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 09:04:37 -0400 Subject: [PATCH 02/15] finshed challenge notebook --- .../group_items/group_ordered_challenge.ipynb | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index e061433..a0a5b06 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -70,15 +70,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ + "def make_order_list(list_in):\n", + " order_list = []\n", + " for item in list_in:\n", + " if item not in order_list:\n", + " order_list.append(item)\n", + " return order_list\n", + "\n", "def group_ordered(list_in):\n", - " # TODO: Implement me\n", - " pass" + " if list_in is None:\n", + " return None\n", + " order_list = make_order_list(list_in)\n", + " current = 0\n", + " for item in order_list:\n", + " search = current + 1\n", + " while True:\n", + " try:\n", + " if list_in[search] != item:\n", + " search += 1\n", + " else:\n", + " current += 1\n", + " list_in[current], list_in[search] = list_in[search], list_in[current]\n", + " search += 1\n", + " except IndexError:\n", + " break\n", + " return list_in" ] }, { @@ -97,13 +119,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_foo\n" + ] + } + ], "source": [ - "%load test_group_ordered.py" + "# %load test_group_ordered.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestFoo(object):\n", + "\n", + " def test_foo(self):\n", + " assert_equal(group_ordered(None), None)\n", + " assert_equal(group_ordered([]), [])\n", + " assert_equal(group_ordered([1]), [1])\n", + " assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", + " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: test_foo')\n", + "\n", + "def main():\n", + " test = TestFoo()\n", + " test.test_foo()\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n" ] }, { From 7ffe77b236de7c6414ecddf4062d3ba57e6965ce Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 09:07:28 -0400 Subject: [PATCH 03/15] remove wrong file --- .../group_items/group_items_challenge.ipynb | 158 ------------------ 1 file changed, 158 deletions(-) delete mode 100644 arrays_strings/group_items/group_items_challenge.ipynb diff --git a/arrays_strings/group_items/group_items_challenge.ipynb b/arrays_strings/group_items/group_items_challenge.ipynb deleted file mode 100644 index 869344b..0000000 --- a/arrays_strings/group_items/group_items_challenge.ipynb +++ /dev/null @@ -1,158 +0,0 @@ -{ - "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 a function that groups identical items based on their order in the list.\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", - "* Can we use extra data structures?\n", - " * Yes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Test Cases\n", - "\n", - "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", - "* group_ordered(['a','b','a') -> ['a','a','b']\n", - "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", - "* group_ordered([1,2,3,4,3,4]) -> [1,2,3,3,4,4]\n", - "* group_ordered([]) -> []\n", - "* group_ordered([1]) -> [1]\n", - "* group_ordered(None) -> None" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Algorithm\n", - "\n", - "Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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": [ - "def group_ordered(list_in):\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_group_ordered.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", - "def main():\n", - " test = TestFoo()\n", - " test.test_foo()\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/templates/foo_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.6" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} From 47750576bae461877c14d4096987ae5b977740eb Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 09:10:48 -0400 Subject: [PATCH 04/15] removed my code from challenge --- .../group_items/group_ordered_challenge.ipynb | 28 ++----------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index a0a5b06..479a452 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -70,37 +70,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def make_order_list(list_in):\n", - " order_list = []\n", - " for item in list_in:\n", - " if item not in order_list:\n", - " order_list.append(item)\n", - " return order_list\n", - "\n", "def group_ordered(list_in):\n", - " if list_in is None:\n", - " return None\n", - " order_list = make_order_list(list_in)\n", - " current = 0\n", - " for item in order_list:\n", - " search = current + 1\n", - " while True:\n", - " try:\n", - " if list_in[search] != item:\n", - " search += 1\n", - " else:\n", - " current += 1\n", - " list_in[current], list_in[search] = list_in[search], list_in[current]\n", - " search += 1\n", - " except IndexError:\n", - " break\n", - " return list_in" + " # TODO: Implement me\n", + " pass" ] }, { From ca02eb7b30e7d2012ba6f8c2453bc44f6f6e8d96 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 09:17:36 -0400 Subject: [PATCH 05/15] change test class and function names from default --- arrays_strings/group_items/group_ordered_challenge.ipynb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index 479a452..a01eee6 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -114,9 +114,9 @@ "# %load test_group_ordered.py\n", "from nose.tools import assert_equal\n", "\n", - "class TestFoo(object):\n", + "class TestGroupOrdered(object):\n", "\n", - " def test_foo(self):\n", + " def test_group_ordered(self):\n", " assert_equal(group_ordered(None), None)\n", " assert_equal(group_ordered([]), [])\n", " assert_equal(group_ordered([1]), [1])\n", @@ -124,11 +124,10 @@ " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", - " print('Success: test_foo')\n", "\n", "def main():\n", - " test = TestFoo()\n", - " test.test_foo()\n", + " test = TestGroupOrdered()\n", + " test.test_group_ordered()\n", "\n", "if __name__ == '__main__':\n", " main()\n" From 8491eb374b9ec4d3080edc5a119e1dbd35c7a6c1 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 11:03:36 -0400 Subject: [PATCH 06/15] added both solutions w/ test --- .../group_items/group_ordered_solution.ipynb | 162 +++++++++++++----- 1 file changed, 115 insertions(+), 47 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_solution.ipynb b/arrays_strings/group_items/group_ordered_solution.ipynb index b7d8b00..9441421 100644 --- a/arrays_strings/group_items/group_ordered_solution.ipynb +++ b/arrays_strings/group_items/group_ordered_solution.ipynb @@ -4,7 +4,7 @@ "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)." + "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { @@ -18,12 +18,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Problem: Implement foo(val), which returns val\n", + "## Problem: Implement a function that groups identical items based on their order in the list.\n", "\n", "* [Constraints](#Constraints)\n", "* [Test Cases](#Test-Cases)\n", - "* [Algorithm](#Algorithm)\n", - "* [Code](#Code)\n", + "* [Algorithm: Modified Selection Sort](#Algorithm: Modified Selection Sort)\n", + "* [Code: Modified Selection Sort](#Code: Modified Selection Sort)\n", + "* [Algorithm: Ordered Dict](#Algorithm: Ordered Dict)\n", + "* [Code: Ordered Dict](#Code:-Ordered-Dict)\n", "* [Unit Test](#Unit-Test)" ] }, @@ -33,9 +35,8 @@ "source": [ "## Constraints\n", "\n", - - "* Is it foo or bar?\n", - " * foo" + "* Can we use extra data structures?\n", + " * Yes" ] }, { @@ -44,82 +45,118 @@ "source": [ "## Test Cases\n", "\n", - "* foo(val) -> val" + "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", + "* group_ordered(['a','b','a') -> ['a','a','b']\n", + "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", + "* group_ordered([]) -> []\n", + "* group_ordered([1]) -> [1]\n", + "* group_ordered(None) -> None\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Algorithm\n", + "## Algorithm: Modified Selection Sort\n", "\n", - "Return the input, val\n", + "* Save the relative position of the first-occurence of each item in a list.\n", + "* Iterate through list of unique items.\n", + " * Keep an outer index; scan rest of list, swapping matching items with outer index and incrementing outer index each time. \n", " \n", "Complexity:\n", - "* Time: O(1)\n", - "* Space: O(1)" + "* Time: O(n^2)\n", + "* Space: O(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Code" + "# Code: Modified Selection Sort" ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def foo(val):\n", - " return val" + "def make_order_list(list_in):\n", + " order_list = []\n", + " for item in list_in:\n", + " if item not in order_list:\n", + " order_list.append(item)\n", + " return order_list\n", + "\n", + "\n", + "def group_ordered(list_in):\n", + " if list_in is None:\n", + " return None\n", + " order_list = make_order_list(list_in)\n", + " current = 0\n", + " for item in order_list:\n", + " search = current + 1\n", + " while True:\n", + " try:\n", + " if list_in[search] != item:\n", + " search += 1\n", + " else:\n", + " current += 1\n", + " list_in[current], list_in[search] = list_in[search], list_in[current]\n", + " search += 1\n", + " except IndexError:\n", + " break\n", + " return list_in" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Unit Test" + "## Algorithm: Ordered Dict.\n", + "\n", + "* Use an ordered dict to track insertion order of each key\n", + "* Flatten list of values.\n", + "\n", + "Complexity:\n", + "\n", + "* Time: O(n)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code: Ordered Dict" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { - "collapsed": false + "collapsed": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting test_foo.py\n" - ] - } - ], + "outputs": [], "source": [ - "%%writefile test_foo.py\n", - "from nose.tools import assert_equal\n", + "from collections import OrderedDict\n", "\n", + "def group_ordered(list_in):\n", + " result = OrderedDict()\n", + " for value in list_in:\n", + " result.setdefault(value, []).append(value)\n", + " return [v for group in result.values() for v in group]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\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", - "def main():\n", - " test = TestFoo()\n", - " test.test_foo()\n", - "\n", - "if __name__ == '__main__':\n", - " main()" + "#### The following unit test is expected to fail until you solve the challenge." ] }, { @@ -133,16 +170,47 @@ "name": "stdout", "output_type": "stream", "text": [ - "Success: test_foo\n" + "Overwriting test_group_ordered.py\n" ] } ], "source": [ - "%run -i test_foo.py" + "%%writefile test_group_ordered.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestGroupOrdered(object):\n", + "\n", + " def test_group_ordered(self):\n", + " assert_equal(group_ordered(None), None)\n", + " assert_equal(group_ordered([]), [])\n", + " assert_equal(group_ordered([1]), [1])\n", + " assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", + " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + "\n", + "def main():\n", + " test = TestGroupOrdered()\n", + " test.test_group_ordered()\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "%run -i test_group_ordered.py" ] } ], "metadata": { + "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", @@ -158,7 +226,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, From c2ec6fe616f5571a4af25e75ebd92e10a8565e9e Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 11:29:28 -0400 Subject: [PATCH 07/15] added alternate solution w/ test --- .../group_items/group_ordered_challenge.ipynb | 2 +- .../group_items/group_ordered_solution.ipynb | 31 +++++++++++------- .../group_items/test_group_ordered.py | 32 +++++++++++-------- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb index a01eee6..6bc87a6 100644 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ b/arrays_strings/group_items/group_ordered_challenge.ipynb @@ -4,7 +4,7 @@ "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)." + "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." ] }, { diff --git a/arrays_strings/group_items/group_ordered_solution.ipynb b/arrays_strings/group_items/group_ordered_solution.ipynb index 9441421..e312b8c 100644 --- a/arrays_strings/group_items/group_ordered_solution.ipynb +++ b/arrays_strings/group_items/group_ordered_solution.ipynb @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": true }, @@ -143,7 +143,7 @@ "source": [ "from collections import OrderedDict\n", "\n", - "def group_ordered(list_in):\n", + "def group_ordered_alt(list_in):\n", " result = OrderedDict()\n", " for value in list_in:\n", " result.setdefault(value, []).append(value)\n", @@ -161,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -180,18 +180,25 @@ "\n", "class TestGroupOrdered(object):\n", "\n", - " def test_group_ordered(self):\n", - " assert_equal(group_ordered(None), None)\n", - " assert_equal(group_ordered([]), [])\n", - " assert_equal(group_ordered([1]), [1])\n", - " assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", - " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", - " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", - " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " def test_group_ordered(self,func):\n", + "\n", + " assert_equal(func(None), None)\n", + " assert_equal(func([]), [])\n", + " assert_equal(func([1]), [1])\n", + " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(func(['a','b','a']),['a','a','b'])\n", + " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", "\n", "def main():\n", " test = TestGroupOrdered()\n", - " test.test_group_ordered()\n", + " test.test_group_ordered(group_ordered)\n", + " try:\n", + " test.test_group_ordered(group_ordered_alt)\n", + " except NameError:\n", + " # Alternate solutions are only defined\n", + " # in the solutions file\n", + " pass\n", "\n", "if __name__ == '__main__':\n", " main()\n" diff --git a/arrays_strings/group_items/test_group_ordered.py b/arrays_strings/group_items/test_group_ordered.py index 943ac67..a1176ff 100644 --- a/arrays_strings/group_items/test_group_ordered.py +++ b/arrays_strings/group_items/test_group_ordered.py @@ -1,20 +1,26 @@ from nose.tools import assert_equal -class TestFoo(object): +class TestGroupOrdered(object): - def test_foo(self): - assert_equal(group_ordered(None), None) - assert_equal(group_ordered([]), []) - assert_equal(group_ordered([1]), [1]) - assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3]) - assert_equal(group_ordered(['a','b','a']),['a','a','b']) - assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) - assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4]) - print('Success: test_foo') + def test_group_ordered(self,func): + + assert_equal(func(None), None) + assert_equal(func([]), []) + assert_equal(func([1]), [1]) + assert_equal(func([1,2,1,3,2]),[1,1,2,2,3]) + assert_equal(func(['a','b','a']),['a','a','b']) + assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) + assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4]) def main(): - test = TestFoo() - test.test_foo() + test = TestGroupOrdered() + test.test_group_ordered(group_ordered) + try: + test.test_group_ordered(group_ordered_alt) + except NameError: + # Alternate solutions are only defined + # in the solutions file + pass if __name__ == '__main__': - main() + main() \ No newline at end of file From 8734f38408697746f60bbcc254f42730345d6f20 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 11:37:39 -0400 Subject: [PATCH 08/15] removed my code --- arrays_strings/group_items/group_ordered.py | 26 --------------------- 1 file changed, 26 deletions(-) delete mode 100644 arrays_strings/group_items/group_ordered.py diff --git a/arrays_strings/group_items/group_ordered.py b/arrays_strings/group_items/group_ordered.py deleted file mode 100644 index 3de2483..0000000 --- a/arrays_strings/group_items/group_ordered.py +++ /dev/null @@ -1,26 +0,0 @@ -def make_order_list(list_in): - order_list = [] - for item in list_in: - if item not in order_list: - order_list.append(item) - return order_list - - -def group_ordered(list_in): - if list_in is None: - return None - order_list = make_order_list(list_in) - current = 0 - for item in order_list: - search = current + 1 - while True: - try: - if list_in[search] != item: - search += 1 - else: - current += 1 - list_in[current], list_in[search] = list_in[search], list_in[current] - search += 1 - except IndexError: - break - return list_in From 40733239fc0c7e72adafb8c92e4eae1ba1b87040 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 12:28:02 -0400 Subject: [PATCH 09/15] fixed execution counts --- .../group_ordered_challenge.ipynb | 168 ++++++++++++ .../group_ordered_solution.ipynb | 250 ++++++++++++++++++ .../group_ordered/test_group_ordered.py | 27 ++ 3 files changed, 445 insertions(+) create mode 100644 arrays_strings/group_ordered/group_ordered_challenge.ipynb create mode 100644 arrays_strings/group_ordered/group_ordered_solution.ipynb create mode 100644 arrays_strings/group_ordered/test_group_ordered.py diff --git a/arrays_strings/group_ordered/group_ordered_challenge.ipynb b/arrays_strings/group_ordered/group_ordered_challenge.ipynb new file mode 100644 index 0000000..488ffad --- /dev/null +++ b/arrays_strings/group_ordered/group_ordered_challenge.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). 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 a function that groups identical items based on their order in the list.\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", + "* Can we use extra data structures?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", + "* group_ordered(['a','b','a') -> ['a','a','b']\n", + "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", + "* group_ordered([]) -> []\n", + "* group_ordered([1]) -> [1]\n", + "* group_ordered(None) -> None" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def group_ordered(list_in):\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": 2, + "metadata": { + "collapsed": false + }, + "outputs": [ + ], + "source": [ + "# %load test_group_ordered.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestGroupOrdered(object):\n", + "\n", + " def test_group_ordered(self,func):\n", + "\n", + " assert_equal(func(None), None)\n", + " assert_equal(func([]), [])\n", + " assert_equal(func([1]), [1])\n", + " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(func(['a','b','a']),['a','a','b'])\n", + " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: ' + func.__name__)\n", + "\n", + "def main():\n", + " test = TestGroupOrdered()\n", + " test.test_group_ordered(group_ordered)\n", + " try:\n", + " test.test_group_ordered(group_ordered_alt)\n", + " except NameError:\n", + " # Alternate solutions are only defined\n", + " # in the solutions file\n", + " pass\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/group_ordered/group_ordered_solution.ipynb b/arrays_strings/group_ordered/group_ordered_solution.ipynb new file mode 100644 index 0000000..4c9bc60 --- /dev/null +++ b/arrays_strings/group_ordered/group_ordered_solution.ipynb @@ -0,0 +1,250 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). 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 a function that groups identical items based on their order in the list.\n", + "\n", + "* [Constraints](#Constraints)\n", + "* [Test Cases](#Test-Cases)\n", + "* [Algorithm: Modified Selection Sort](#Algorithm: Modified Selection Sort)\n", + "* [Code: Modified Selection Sort](#Code: Modified Selection Sort)\n", + "* [Algorithm: Ordered Dict](#Algorithm: Ordered Dict)\n", + "* [Code: Ordered Dict](#Code:-Ordered-Dict)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Constraints\n", + "\n", + "* Can we use extra data structures?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", + "* group_ordered(['a','b','a') -> ['a','a','b']\n", + "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", + "* group_ordered([]) -> []\n", + "* group_ordered([1]) -> [1]\n", + "* group_ordered(None) -> None\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm: Modified Selection Sort\n", + "\n", + "* Save the relative position of the first-occurence of each item in a list.\n", + "* Iterate through list of unique items.\n", + " * Keep an outer index; scan rest of list, swapping matching items with outer index and incrementing outer index each time. \n", + " \n", + "Complexity:\n", + "* Time: O(n^2)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Code: Modified Selection Sort" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def make_order_list(list_in):\n", + " order_list = []\n", + " for item in list_in:\n", + " if item not in order_list:\n", + " order_list.append(item)\n", + " return order_list\n", + "\n", + "\n", + "def group_ordered(list_in):\n", + " if list_in is None:\n", + " return None\n", + " order_list = make_order_list(list_in)\n", + " current = 0\n", + " for item in order_list:\n", + " search = current + 1\n", + " while True:\n", + " try:\n", + " if list_in[search] != item:\n", + " search += 1\n", + " else:\n", + " current += 1\n", + " list_in[current], list_in[search] = list_in[search], list_in[current]\n", + " search += 1\n", + " except IndexError:\n", + " break\n", + " return list_in" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm: Ordered Dict.\n", + "\n", + "* Use an ordered dict to track insertion order of each key\n", + "* Flatten list of values.\n", + "\n", + "Complexity:\n", + "\n", + "* Time: O(n)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code: Ordered Dict" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from collections import OrderedDict\n", + "\n", + "def group_ordered_alt(list_in):\n", + " result = OrderedDict()\n", + " for value in list_in:\n", + " result.setdefault(value, []).append(value)\n", + " return [v for group in result.values() for v in group]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + "\n", + "#### The following unit test is expected to fail until you solve the challenge." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_group_ordered.py\n" + ] + } + ], + "source": [ + "%%writefile test_group_ordered.py\n", + "from nose.tools import assert_equal\n", + "\n", + "class TestGroupOrdered(object):\n", + "\n", + " def test_group_ordered(self,func):\n", + "\n", + " assert_equal(func(None), None)\n", + " assert_equal(func([]), [])\n", + " assert_equal(func([1]), [1])\n", + " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", + " assert_equal(func(['a','b','a']),['a','a','b'])\n", + " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", + " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: ' + func.__name__)\n", + "\n", + "def main():\n", + " test = TestGroupOrdered()\n", + " test.test_group_ordered(group_ordered)\n", + " try:\n", + " test.test_group_ordered(group_ordered_alt)\n", + " except NameError:\n", + " # Alternate solutions are only defined\n", + " # in the solutions file\n", + " pass\n", + "\n", + "if __name__ == '__main__':\n", + " main()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: group_ordered\n" + ] + } + ], + "source": [ + "%run -i test_group_ordered.py" + ] + } + ], + "metadata": { + "celltoolbar": "Edit 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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/arrays_strings/group_ordered/test_group_ordered.py b/arrays_strings/group_ordered/test_group_ordered.py new file mode 100644 index 0000000..653c3fd --- /dev/null +++ b/arrays_strings/group_ordered/test_group_ordered.py @@ -0,0 +1,27 @@ +from nose.tools import assert_equal + +class TestGroupOrdered(object): + + def test_group_ordered(self,func): + + assert_equal(func(None), None) + assert_equal(func([]), []) + assert_equal(func([1]), [1]) + assert_equal(func([1,2,1,3,2]),[1,1,2,2,3]) + assert_equal(func(['a','b','a']),['a','a','b']) + assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) + assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4]) + print('Success: ' + func.__name__) + +def main(): + test = TestGroupOrdered() + test.test_group_ordered(group_ordered) + try: + test.test_group_ordered(group_ordered_alt) + except NameError: + # Alternate solutions are only defined + # in the solutions file + pass + +if __name__ == '__main__': + main() \ No newline at end of file From 0282c85bd0ba4011053288178c37be6b27ed5b70 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 12:28:28 -0400 Subject: [PATCH 10/15] fixed execution counts --- arrays_strings/group_ordered/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 arrays_strings/group_ordered/__init__.py diff --git a/arrays_strings/group_ordered/__init__.py b/arrays_strings/group_ordered/__init__.py new file mode 100644 index 0000000..e69de29 From 8ab6a2334a1bba0e76f5c31a6cdc170f2df61660 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 12:30:18 -0400 Subject: [PATCH 11/15] removed duplicate repository --- arrays_strings/group_items/__init__.py | 0 .../group_items/group_ordered_challenge.ipynb | 167 ------------ .../group_items/group_ordered_solution.ipynb | 241 ------------------ .../group_items/test_group_ordered.py | 26 -- 4 files changed, 434 deletions(-) delete mode 100644 arrays_strings/group_items/__init__.py delete mode 100644 arrays_strings/group_items/group_ordered_challenge.ipynb delete mode 100644 arrays_strings/group_items/group_ordered_solution.ipynb delete mode 100644 arrays_strings/group_items/test_group_ordered.py diff --git a/arrays_strings/group_items/__init__.py b/arrays_strings/group_items/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/arrays_strings/group_items/group_ordered_challenge.ipynb b/arrays_strings/group_items/group_ordered_challenge.ipynb deleted file mode 100644 index 6bc87a6..0000000 --- a/arrays_strings/group_items/group_ordered_challenge.ipynb +++ /dev/null @@ -1,167 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). 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 a function that groups identical items based on their order in the list.\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", - "* Can we use extra data structures?\n", - " * Yes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Test Cases\n", - "\n", - "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", - "* group_ordered(['a','b','a') -> ['a','a','b']\n", - "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", - "* group_ordered([]) -> []\n", - "* group_ordered([1]) -> [1]\n", - "* group_ordered(None) -> None" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Algorithm\n", - "\n", - "Refer to the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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": 1, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def group_ordered(list_in):\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": 6, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Success: test_foo\n" - ] - } - ], - "source": [ - "# %load test_group_ordered.py\n", - "from nose.tools import assert_equal\n", - "\n", - "class TestGroupOrdered(object):\n", - "\n", - " def test_group_ordered(self):\n", - " assert_equal(group_ordered(None), None)\n", - " assert_equal(group_ordered([]), [])\n", - " assert_equal(group_ordered([1]), [1])\n", - " assert_equal(group_ordered([1,2,1,3,2]),[1,1,2,2,3])\n", - " assert_equal(group_ordered(['a','b','a']),['a','a','b'])\n", - " assert_equal(group_ordered([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", - " assert_equal(group_ordered([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", - "\n", - "def main():\n", - " test = TestGroupOrdered()\n", - " test.test_group_ordered()\n", - "\n", - "if __name__ == '__main__':\n", - " main()\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Solution Notebook\n", - "\n", - "Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_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.6" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/arrays_strings/group_items/group_ordered_solution.ipynb b/arrays_strings/group_items/group_ordered_solution.ipynb deleted file mode 100644 index e312b8c..0000000 --- a/arrays_strings/group_items/group_ordered_solution.ipynb +++ /dev/null @@ -1,241 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). 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 a function that groups identical items based on their order in the list.\n", - "\n", - "* [Constraints](#Constraints)\n", - "* [Test Cases](#Test-Cases)\n", - "* [Algorithm: Modified Selection Sort](#Algorithm: Modified Selection Sort)\n", - "* [Code: Modified Selection Sort](#Code: Modified Selection Sort)\n", - "* [Algorithm: Ordered Dict](#Algorithm: Ordered Dict)\n", - "* [Code: Ordered Dict](#Code:-Ordered-Dict)\n", - "* [Unit Test](#Unit-Test)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Constraints\n", - "\n", - "* Can we use extra data structures?\n", - " * Yes" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Test Cases\n", - "\n", - "* group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]\n", - "* group_ordered(['a','b','a') -> ['a','a','b']\n", - "* group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]\n", - "* group_ordered([]) -> []\n", - "* group_ordered([1]) -> [1]\n", - "* group_ordered(None) -> None\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Algorithm: Modified Selection Sort\n", - "\n", - "* Save the relative position of the first-occurence of each item in a list.\n", - "* Iterate through list of unique items.\n", - " * Keep an outer index; scan rest of list, swapping matching items with outer index and incrementing outer index each time. \n", - " \n", - "Complexity:\n", - "* Time: O(n^2)\n", - "* Space: O(n)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Code: Modified Selection Sort" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def make_order_list(list_in):\n", - " order_list = []\n", - " for item in list_in:\n", - " if item not in order_list:\n", - " order_list.append(item)\n", - " return order_list\n", - "\n", - "\n", - "def group_ordered(list_in):\n", - " if list_in is None:\n", - " return None\n", - " order_list = make_order_list(list_in)\n", - " current = 0\n", - " for item in order_list:\n", - " search = current + 1\n", - " while True:\n", - " try:\n", - " if list_in[search] != item:\n", - " search += 1\n", - " else:\n", - " current += 1\n", - " list_in[current], list_in[search] = list_in[search], list_in[current]\n", - " search += 1\n", - " except IndexError:\n", - " break\n", - " return list_in" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Algorithm: Ordered Dict.\n", - "\n", - "* Use an ordered dict to track insertion order of each key\n", - "* Flatten list of values.\n", - "\n", - "Complexity:\n", - "\n", - "* Time: O(n)\n", - "* Space: O(n)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Code: Ordered Dict" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "from collections import OrderedDict\n", - "\n", - "def group_ordered_alt(list_in):\n", - " result = OrderedDict()\n", - " for value in list_in:\n", - " result.setdefault(value, []).append(value)\n", - " return [v for group in result.values() for v in group]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Unit Test\n", - "\n", - "#### The following unit test is expected to fail until you solve the challenge." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Overwriting test_group_ordered.py\n" - ] - } - ], - "source": [ - "%%writefile test_group_ordered.py\n", - "from nose.tools import assert_equal\n", - "\n", - "class TestGroupOrdered(object):\n", - "\n", - " def test_group_ordered(self,func):\n", - "\n", - " assert_equal(func(None), None)\n", - " assert_equal(func([]), [])\n", - " assert_equal(func([1]), [1])\n", - " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", - " assert_equal(func(['a','b','a']),['a','a','b'])\n", - " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", - " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", - "\n", - "def main():\n", - " test = TestGroupOrdered()\n", - " test.test_group_ordered(group_ordered)\n", - " try:\n", - " test.test_group_ordered(group_ordered_alt)\n", - " except NameError:\n", - " # Alternate solutions are only defined\n", - " # in the solutions file\n", - " pass\n", - "\n", - "if __name__ == '__main__':\n", - " main()\n" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "%run -i test_group_ordered.py" - ] - } - ], - "metadata": { - "celltoolbar": "Edit 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.6" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/arrays_strings/group_items/test_group_ordered.py b/arrays_strings/group_items/test_group_ordered.py deleted file mode 100644 index a1176ff..0000000 --- a/arrays_strings/group_items/test_group_ordered.py +++ /dev/null @@ -1,26 +0,0 @@ -from nose.tools import assert_equal - -class TestGroupOrdered(object): - - def test_group_ordered(self,func): - - assert_equal(func(None), None) - assert_equal(func([]), []) - assert_equal(func([1]), [1]) - assert_equal(func([1,2,1,3,2]),[1,1,2,2,3]) - assert_equal(func(['a','b','a']),['a','a','b']) - assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) - assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4]) - -def main(): - test = TestGroupOrdered() - test.test_group_ordered(group_ordered) - try: - test.test_group_ordered(group_ordered_alt) - except NameError: - # Alternate solutions are only defined - # in the solutions file - pass - -if __name__ == '__main__': - main() \ No newline at end of file From f703014decd822b9899367a8dd0dcdc5318fd4e9 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Thu, 9 Jul 2015 12:36:32 -0400 Subject: [PATCH 12/15] added group_ordered to list --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6c3725b..edd6648 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,7 @@ Review the [Contributing Guidelines](https://github.com/donnemartin/interactive- | Compress a string | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/compress/compress_solution.ipynb) | | Reverse characters in a string | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_solution.ipynb) | | Implement a hash table | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/hash_map/hash_map_solution.ipynb) | +| Group list items | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/group_ordered/group_ordered_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/group_ordered/group_ordered_solution.ipynb) | | Find the first non-repeated character in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Remove specified characters in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Reverse words in a string | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | From b4476d3f7742b1aa28e14a6ca969aab898e336d5 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Sat, 11 Jul 2015 10:43:28 -0400 Subject: [PATCH 13/15] fixed problems, new pull --- .../permutation/permutation_solution.ipynb | 5 +- .../rotation/rotation_challenge.ipynb | 47 ++++++++++++++----- .../unique_chars/unique_chars_challenge.ipynb | 28 ++++++++--- .../group_ordered/__init__.py | 0 .../group_ordered/group_ordered.py | 34 ++++++++++++++ .../group_ordered_challenge.ipynb | 25 +++++----- .../group_ordered_solution.ipynb | 38 ++++++++++----- .../group_ordered/test_group_ordered.py | 16 ++++--- .../insertion_sort_solution.ipynb | 31 +++++++++--- .../insertion_sort_challenge.ipynb | 34 +++++++++++--- 10 files changed, 193 insertions(+), 65 deletions(-) rename {arrays_strings => sorting_searching}/group_ordered/__init__.py (100%) create mode 100644 sorting_searching/group_ordered/group_ordered.py rename {arrays_strings => sorting_searching}/group_ordered/group_ordered_challenge.ipynb (82%) rename {arrays_strings => sorting_searching}/group_ordered/group_ordered_solution.ipynb (84%) rename {arrays_strings => sorting_searching}/group_ordered/test_group_ordered.py (51%) rename sorting_searching/{insertion_sort => selection_sort}/insertion_sort_challenge.ipynb (83%) diff --git a/arrays_strings/permutation/permutation_solution.ipynb b/arrays_strings/permutation/permutation_solution.ipynb index 6eb4e58..8f4589c 100644 --- a/arrays_strings/permutation/permutation_solution.ipynb +++ b/arrays_strings/permutation/permutation_solution.ipynb @@ -35,7 +35,6 @@ "source": [ "## Constraints\n", "\n", - "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -164,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -242,7 +241,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/arrays_strings/rotation/rotation_challenge.ipynb b/arrays_strings/rotation/rotation_challenge.ipynb index e66ebe3..c892cb2 100644 --- a/arrays_strings/rotation/rotation_challenge.ipynb +++ b/arrays_strings/rotation/rotation_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Can you assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -75,20 +74,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def is_substring(s1, s2):\n", - " # TODO: Implement me\n", - " pass\n", + "def is_substring(substr, s):\n", + " if len(s) < len(substr):\n", + " return False\n", + " elif len(substr) == len(s) and len(s) == 0:\n", + " return True\n", + " pos = 0\n", + " while pos < len(s):\n", + " so_far = 0\n", + " while so_far < len(substr):\n", + " if substr[so_far] != s[pos]:\n", + " pos = pos + 1\n", + " break\n", + " else:\n", + " pos = pos + 1\n", + " so_far = so_far + 1\n", + " if so_far == len(substr):\n", + " return True\n", + " return False\n", "\n", "def is_rotation(s1, s2):\n", - " # TODO: Implement me\n", - " # Call is_substring only once\n", - " pass" + " if s1 is None or s2 is None:\n", + " return False\n", + " elif len(s1) != len(s2):\n", + " return False\n", + " else:\n", + " return is_substring(s2,s1+s1)" ] }, { @@ -109,11 +126,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_rotation\n" + ] + } + ], "source": [ "# %load test_rotation.py\n", "from nose.tools import assert_equal\n", @@ -163,7 +188,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/arrays_strings/unique_chars/unique_chars_challenge.ipynb b/arrays_strings/unique_chars/unique_chars_challenge.ipynb index 0e85a04..c9ffbff 100644 --- a/arrays_strings/unique_chars/unique_chars_challenge.ipynb +++ b/arrays_strings/unique_chars/unique_chars_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Can you assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -73,15 +72,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def unique_chars(string):\n", - " # TODO: Implement me\n", - " pass" + " from collections import defaultdict\n", + " \n", + " char_table = defaultdict(int)\n", + " for c in string:\n", + " if char_table[c] > 0:\n", + " return False\n", + " else:\n", + " char_table[c] = 1\n", + " return True" ] }, { @@ -100,11 +106,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_unique_chars\n" + ] + } + ], "source": [ "# %load test_unique_chars.py\n", "from nose.tools import assert_equal\n", @@ -159,7 +173,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/arrays_strings/group_ordered/__init__.py b/sorting_searching/group_ordered/__init__.py similarity index 100% rename from arrays_strings/group_ordered/__init__.py rename to sorting_searching/group_ordered/__init__.py diff --git a/sorting_searching/group_ordered/group_ordered.py b/sorting_searching/group_ordered/group_ordered.py new file mode 100644 index 0000000..7bb37fc --- /dev/null +++ b/sorting_searching/group_ordered/group_ordered.py @@ -0,0 +1,34 @@ +def make_order_list(list_in): + order_list = [] + for item in list_in: + if item not in order_list: + order_list.append(item) + return order_list + + +def group_ordered(list_in): + if list_in is None: + return None + order_list = make_order_list(list_in) + current = 0 + for item in order_list: + search = current + 1 + while True: + try: + if list_in[search] != item: + search += 1 + else: + current += 1 + list_in[current], list_in[search] = list_in[search], list_in[current] + search += 1 + except IndexError: + break + return list_in + + +def group_ordered2(list_in): + from collections import OrderedDict + result = OrderedDict() + for value in list_in: + result.setdefault(value, []).append(value) + return result diff --git a/arrays_strings/group_ordered/group_ordered_challenge.ipynb b/sorting_searching/group_ordered/group_ordered_challenge.ipynb similarity index 82% rename from arrays_strings/group_ordered/group_ordered_challenge.ipynb rename to sorting_searching/group_ordered/group_ordered_challenge.ipynb index 488ffad..96d8238 100644 --- a/arrays_strings/group_ordered/group_ordered_challenge.ipynb +++ b/sorting_searching/group_ordered/group_ordered_challenge.ipynb @@ -70,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "collapsed": false }, @@ -97,29 +97,32 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - ], + "outputs": [], "source": [ "# %load test_group_ordered.py\n", "from nose.tools import assert_equal\n", "\n", - "class TestGroupOrdered(object):\n", "\n", - " def test_group_ordered(self,func):\n", + "class TestGroupOrdered(object):\n", + " def test_group_ordered(self, func):\n", "\n", " assert_equal(func(None), None)\n", + " print('Success: ' + func.__name__ + \" None case.\")\n", " assert_equal(func([]), [])\n", + " print('Success: ' + func.__name__ + \" Empty case.\")\n", " assert_equal(func([1]), [1])\n", - " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", - " assert_equal(func(['a','b','a']),['a','a','b'])\n", - " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", - " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: ' + func.__name__ + \" Single element case.\")\n", + " assert_equal(func([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])\n", + " assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b'])\n", + " assert_equal(func([1, 1, 2, 3, 4, 5, 2, 1]), [1, 1, 1, 2, 2, 3, 4, 5])\n", + " assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])\n", " print('Success: ' + func.__name__)\n", "\n", + "\n", "def main():\n", " test = TestGroupOrdered()\n", " test.test_group_ordered(group_ordered)\n", @@ -140,7 +143,7 @@ "source": [ "## Solution Notebook\n", "\n", - "Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/templates/foo_solution.ipynb) for a discussion on algorithms and code solutions." + "Review the [solution notebook](https://github.com/donnemartin/interactive-coding-challenges/sorting_searching/group_ordered/group_ordered_solution.ipynb) for a discussion on algorithms and code solutions." ] } ], diff --git a/arrays_strings/group_ordered/group_ordered_solution.ipynb b/sorting_searching/group_ordered/group_ordered_solution.ipynb similarity index 84% rename from arrays_strings/group_ordered/group_ordered_solution.ipynb rename to sorting_searching/group_ordered/group_ordered_solution.ipynb index 4c9bc60..2dd1a1b 100644 --- a/arrays_strings/group_ordered/group_ordered_solution.ipynb +++ b/sorting_searching/group_ordered/group_ordered_solution.ipynb @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": { "collapsed": true }, @@ -144,6 +144,8 @@ "from collections import OrderedDict\n", "\n", "def group_ordered_alt(list_in):\n", + " if list_in is None:\n", + " return None\n", " result = OrderedDict()\n", " for value in list_in:\n", " result.setdefault(value, []).append(value)\n", @@ -161,7 +163,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -178,19 +180,23 @@ "%%writefile test_group_ordered.py\n", "from nose.tools import assert_equal\n", "\n", - "class TestGroupOrdered(object):\n", "\n", - " def test_group_ordered(self,func):\n", + "class TestGroupOrdered(object):\n", + " def test_group_ordered(self, func):\n", "\n", " assert_equal(func(None), None)\n", + " print('Success: ' + func.__name__ + \" None case.\")\n", " assert_equal(func([]), [])\n", + " print('Success: ' + func.__name__ + \" Empty case.\")\n", " assert_equal(func([1]), [1])\n", - " assert_equal(func([1,2,1,3,2]),[1,1,2,2,3])\n", - " assert_equal(func(['a','b','a']),['a','a','b'])\n", - " assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5])\n", - " assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4])\n", + " print('Success: ' + func.__name__ + \" Single element case.\")\n", + " assert_equal(func([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])\n", + " assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b'])\n", + " assert_equal(func([1, 1, 2, 3, 4, 5, 2, 1]), [1, 1, 1, 2, 2, 3, 4, 5])\n", + " assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])\n", " print('Success: ' + func.__name__)\n", "\n", + "\n", "def main():\n", " test = TestGroupOrdered()\n", " test.test_group_ordered(group_ordered)\n", @@ -202,12 +208,12 @@ " pass\n", "\n", "if __name__ == '__main__':\n", - " main()\n" + " main()" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -216,7 +222,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "Success: group_ordered\n" + "Success: group_ordered None case.\n", + "Success: group_ordered Empty case.\n", + "Success: group_ordered Single element case.\n", + "Success: group_ordered\n", + "Success: group_ordered_alt None case.\n", + "Success: group_ordered_alt Empty case.\n", + "Success: group_ordered_alt Single element case.\n", + "Success: group_ordered_alt\n" ] } ], @@ -226,7 +239,6 @@ } ], "metadata": { - "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 2", "language": "python", diff --git a/arrays_strings/group_ordered/test_group_ordered.py b/sorting_searching/group_ordered/test_group_ordered.py similarity index 51% rename from arrays_strings/group_ordered/test_group_ordered.py rename to sorting_searching/group_ordered/test_group_ordered.py index 653c3fd..8f499b5 100644 --- a/arrays_strings/group_ordered/test_group_ordered.py +++ b/sorting_searching/group_ordered/test_group_ordered.py @@ -1,18 +1,22 @@ from nose.tools import assert_equal -class TestGroupOrdered(object): - def test_group_ordered(self,func): +class TestGroupOrdered(object): + def test_group_ordered(self, func): assert_equal(func(None), None) + print('Success: ' + func.__name__ + " None case.") assert_equal(func([]), []) + print('Success: ' + func.__name__ + " Empty case.") assert_equal(func([1]), [1]) - assert_equal(func([1,2,1,3,2]),[1,1,2,2,3]) - assert_equal(func(['a','b','a']),['a','a','b']) - assert_equal(func([1,1,2,3,4,5,2,1]),[1,1,1,2,2,3,4,5]) - assert_equal(func([1,2,3,4,3,4]),[1,2,3,3,4,4]) + print('Success: ' + func.__name__ + " Single element case.") + assert_equal(func([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3]) + assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b']) + assert_equal(func([1, 1, 2, 3, 4, 5, 2, 1]), [1, 1, 1, 2, 2, 3, 4, 5]) + assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4]) print('Success: ' + func.__name__) + def main(): test = TestGroupOrdered() test.test_group_ordered(group_ordered) diff --git a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb index 83314b7..3d865e9 100644 --- a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb @@ -33,7 +33,6 @@ "source": [ "## Constraints\n", "\n", - "* Is a naiive solution sufficient?\n", " * Yes" ] @@ -152,7 +151,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -161,16 +160,34 @@ "name": "stdout", "output_type": "stream", "text": [ - "Empty input\n", - "One element\n", - "Two or more elements\n", - "Success: test_insertion_sort\n" + "Empty input\n" + ] + }, + { + "ename": "NameError", + "evalue": "global name 'insertion_sort' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 24\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mtest\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTestInsertionSort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 26\u001b[1;33m \u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest_insertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36mtest_insertion_sort\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Empty input'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0minsertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mNameError\u001b[0m: global name 'insertion_sort' is not defined" ] } ], "source": [ "%run -i test_insertion_sort.py" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] } ], "metadata": { @@ -189,7 +206,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb b/sorting_searching/selection_sort/insertion_sort_challenge.ipynb similarity index 83% rename from sorting_searching/insertion_sort/insertion_sort_challenge.ipynb rename to sorting_searching/selection_sort/insertion_sort_challenge.ipynb index c8a7f69..b35d575 100644 --- a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb +++ b/sorting_searching/selection_sort/insertion_sort_challenge.ipynb @@ -34,7 +34,6 @@ "source": [ "## Constraints\n", "\n", - "* Is a naiive solution sufficient?\n", " * Yes" ] @@ -68,15 +67,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ + "def insert(l,item_pos):\n", + " item = l[item_pos]\n", + " while item_pos > 0:\n", + " if l[item_pos - 1] > item:\n", + " l[item_pos - 1], l[item_pos] = l[item_pos], l[item_pos - 1]\n", + " item_pos -= 1\n", + " else:\n", + " break\n", + "\n", "def insertion_sort(data):\n", - " # TODO: Implement me\n", - " pass" + " for i,item in enumerate(data):\n", + " insert(data,i)\n", + " return data" ] }, { @@ -92,11 +101,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_insertion_sort\n" + ] + } + ], "source": [ "# %load test_insertion_sort.py\n", "from nose.tools import assert_equal\n", @@ -153,7 +173,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.10" + "version": "2.7.6" } }, "nbformat": 4, From f45be890af5fe8b22d21b6f29c4faf9ea44d265a Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Sat, 11 Jul 2015 10:45:08 -0400 Subject: [PATCH 14/15] fixed problems, new pull --- .../group_ordered/group_ordered.py | 34 ------------------- .../insertion_sort_challenge.ipynb | 0 2 files changed, 34 deletions(-) delete mode 100644 sorting_searching/group_ordered/group_ordered.py rename sorting_searching/{selection_sort => insertion_sort}/insertion_sort_challenge.ipynb (100%) diff --git a/sorting_searching/group_ordered/group_ordered.py b/sorting_searching/group_ordered/group_ordered.py deleted file mode 100644 index 7bb37fc..0000000 --- a/sorting_searching/group_ordered/group_ordered.py +++ /dev/null @@ -1,34 +0,0 @@ -def make_order_list(list_in): - order_list = [] - for item in list_in: - if item not in order_list: - order_list.append(item) - return order_list - - -def group_ordered(list_in): - if list_in is None: - return None - order_list = make_order_list(list_in) - current = 0 - for item in order_list: - search = current + 1 - while True: - try: - if list_in[search] != item: - search += 1 - else: - current += 1 - list_in[current], list_in[search] = list_in[search], list_in[current] - search += 1 - except IndexError: - break - return list_in - - -def group_ordered2(list_in): - from collections import OrderedDict - result = OrderedDict() - for value in list_in: - result.setdefault(value, []).append(value) - return result diff --git a/sorting_searching/selection_sort/insertion_sort_challenge.ipynb b/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb similarity index 100% rename from sorting_searching/selection_sort/insertion_sort_challenge.ipynb rename to sorting_searching/insertion_sort/insertion_sort_challenge.ipynb From 91b021f4b54136792c64e665ad771baf9fc7b275 Mon Sep 17 00:00:00 2001 From: wdonahoe Date: Sat, 11 Jul 2015 11:07:15 -0400 Subject: [PATCH 15/15] reverted files --- .../permutation/permutation_solution.ipynb | 5 +- .../rotation/rotation_challenge.ipynb | 47 +++++-------------- .../unique_chars/unique_chars_challenge.ipynb | 28 +++-------- .../insertion_sort_challenge.ipynb | 34 +++----------- .../insertion_sort_solution.ipynb | 31 +++--------- 5 files changed, 35 insertions(+), 110 deletions(-) diff --git a/arrays_strings/permutation/permutation_solution.ipynb b/arrays_strings/permutation/permutation_solution.ipynb index 8f4589c..6eb4e58 100644 --- a/arrays_strings/permutation/permutation_solution.ipynb +++ b/arrays_strings/permutation/permutation_solution.ipynb @@ -35,6 +35,7 @@ "source": [ "## Constraints\n", "\n", + "* Can we assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -163,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -241,7 +242,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.6" + "version": "2.7.10" } }, "nbformat": 4, diff --git a/arrays_strings/rotation/rotation_challenge.ipynb b/arrays_strings/rotation/rotation_challenge.ipynb index c892cb2..e66ebe3 100644 --- a/arrays_strings/rotation/rotation_challenge.ipynb +++ b/arrays_strings/rotation/rotation_challenge.ipynb @@ -34,6 +34,7 @@ "source": [ "## Constraints\n", "\n", + "* Can you assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -74,38 +75,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def is_substring(substr, s):\n", - " if len(s) < len(substr):\n", - " return False\n", - " elif len(substr) == len(s) and len(s) == 0:\n", - " return True\n", - " pos = 0\n", - " while pos < len(s):\n", - " so_far = 0\n", - " while so_far < len(substr):\n", - " if substr[so_far] != s[pos]:\n", - " pos = pos + 1\n", - " break\n", - " else:\n", - " pos = pos + 1\n", - " so_far = so_far + 1\n", - " if so_far == len(substr):\n", - " return True\n", - " return False\n", + "def is_substring(s1, s2):\n", + " # TODO: Implement me\n", + " pass\n", "\n", "def is_rotation(s1, s2):\n", - " if s1 is None or s2 is None:\n", - " return False\n", - " elif len(s1) != len(s2):\n", - " return False\n", - " else:\n", - " return is_substring(s2,s1+s1)" + " # TODO: Implement me\n", + " # Call is_substring only once\n", + " pass" ] }, { @@ -126,19 +109,11 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Success: test_rotation\n" - ] - } - ], + "outputs": [], "source": [ "# %load test_rotation.py\n", "from nose.tools import assert_equal\n", @@ -188,7 +163,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.6" + "version": "2.7.10" } }, "nbformat": 4, diff --git a/arrays_strings/unique_chars/unique_chars_challenge.ipynb b/arrays_strings/unique_chars/unique_chars_challenge.ipynb index c9ffbff..0e85a04 100644 --- a/arrays_strings/unique_chars/unique_chars_challenge.ipynb +++ b/arrays_strings/unique_chars/unique_chars_challenge.ipynb @@ -34,6 +34,7 @@ "source": [ "## Constraints\n", "\n", + "* Can you assume the string is ASCII?\n", " * Yes\n", " * Note: Unicode strings could require special handling depending on your language\n", @@ -72,22 +73,15 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def unique_chars(string):\n", - " from collections import defaultdict\n", - " \n", - " char_table = defaultdict(int)\n", - " for c in string:\n", - " if char_table[c] > 0:\n", - " return False\n", - " else:\n", - " char_table[c] = 1\n", - " return True" + " # TODO: Implement me\n", + " pass" ] }, { @@ -106,19 +100,11 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Success: test_unique_chars\n" - ] - } - ], + "outputs": [], "source": [ "# %load test_unique_chars.py\n", "from nose.tools import assert_equal\n", @@ -173,7 +159,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.6" + "version": "2.7.10" } }, "nbformat": 4, diff --git a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb b/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb index b35d575..c8a7f69 100644 --- a/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_challenge.ipynb @@ -34,6 +34,7 @@ "source": [ "## Constraints\n", "\n", + "* Is a naiive solution sufficient?\n", " * Yes" ] @@ -67,25 +68,15 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ - "def insert(l,item_pos):\n", - " item = l[item_pos]\n", - " while item_pos > 0:\n", - " if l[item_pos - 1] > item:\n", - " l[item_pos - 1], l[item_pos] = l[item_pos], l[item_pos - 1]\n", - " item_pos -= 1\n", - " else:\n", - " break\n", - "\n", "def insertion_sort(data):\n", - " for i,item in enumerate(data):\n", - " insert(data,i)\n", - " return data" + " # TODO: Implement me\n", + " pass" ] }, { @@ -101,22 +92,11 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Empty input\n", - "One element\n", - "Two or more elements\n", - "Success: test_insertion_sort\n" - ] - } - ], + "outputs": [], "source": [ "# %load test_insertion_sort.py\n", "from nose.tools import assert_equal\n", @@ -173,7 +153,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.6" + "version": "2.7.10" } }, "nbformat": 4, diff --git a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb index 3d865e9..83314b7 100644 --- a/sorting_searching/insertion_sort/insertion_sort_solution.ipynb +++ b/sorting_searching/insertion_sort/insertion_sort_solution.ipynb @@ -33,6 +33,7 @@ "source": [ "## Constraints\n", "\n", + "* Is a naiive solution sufficient?\n", " * Yes" ] @@ -151,7 +152,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -160,34 +161,16 @@ "name": "stdout", "output_type": "stream", "text": [ - "Empty input\n" - ] - }, - { - "ename": "NameError", - "evalue": "global name 'insertion_sort' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 24\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 25\u001b[0m \u001b[0mtest\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTestInsertionSort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 26\u001b[1;33m \u001b[0mtest\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtest_insertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[1;32m/home/wdonahoe/Documents/interactive-coding-challenges/sorting_searching/insertion_sort/test_insertion_sort.py\u001b[0m in \u001b[0;36mtest_insertion_sort\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Empty input'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0minsertion_sort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[0massert_equal\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", - "\u001b[1;31mNameError\u001b[0m: global name 'insertion_sort' is not defined" + "Empty input\n", + "One element\n", + "Two or more elements\n", + "Success: test_insertion_sort\n" ] } ], "source": [ "%run -i test_insertion_sort.py" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] } ], "metadata": { @@ -206,7 +189,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.6" + "version": "2.7.10" } }, "nbformat": 4,