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()