From ba8325329aea4a1a29d2a1b4edf6ece13112bceb Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 27 Mar 2017 05:20:27 -0400 Subject: [PATCH] Add power set challenge --- recursion_dynamic/power_set/__init__.py | 0 .../power_set/power_set_challenge.ipynb | 202 +++++++++++++ .../power_set/power_set_solution.ipynb | 270 ++++++++++++++++++ recursion_dynamic/power_set/test_power_set.py | 39 +++ 4 files changed, 511 insertions(+) create mode 100644 recursion_dynamic/power_set/__init__.py create mode 100644 recursion_dynamic/power_set/power_set_challenge.ipynb create mode 100644 recursion_dynamic/power_set/power_set_solution.ipynb create mode 100644 recursion_dynamic/power_set/test_power_set.py diff --git a/recursion_dynamic/power_set/__init__.py b/recursion_dynamic/power_set/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recursion_dynamic/power_set/power_set_challenge.ipynb b/recursion_dynamic/power_set/power_set_challenge.ipynb new file mode 100644 index 0000000..907b56c --- /dev/null +++ b/recursion_dynamic/power_set/power_set_challenge.ipynb @@ -0,0 +1,202 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Challenge Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Return all subsets of a set.\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", + "* Should the resulting subsets be unique?\n", + " * Yes, treat 'ab' and 'bc' as the same\n", + "* Is the empty set included as a subset?\n", + " * Yes\n", + "* Are the inputs unique?\n", + " * No\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "
\n",
+    "* None -> None\n",
+    "* [] -> [[]]\n",
+    "* ['a'] -> [[], \n",
+    "            ['a']]\n",
+    "* ['a', 'b'] -> [[], \n",
+    "                 ['a'], \n",
+    "                 ['b'], \n",
+    "                 ['a', 'b']]\n",
+    "* ['a', 'b', 'c'] -> [[], \n",
+    "                      ['a'], \n",
+    "                      ['b'], \n",
+    "                      ['c'],\n",
+    "                      ['a', 'b'], \n",
+    "                      ['a', 'c'], \n",
+    "                      ['b', 'c'],\n",
+    "                      ['a', 'b', 'c']]\n",
+    "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "class Sets(object):\n", + "\n", + " def find_power_set_recursive(self, input_set):\n", + " # TODO: Implement me\n", + " pass\n", + "\n", + " def find_power_set_iterative(self, input_set):\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_power_set.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestPowerSet(object):\n", + "\n", + " def test_power_set(self):\n", + " input_set = []\n", + " expected = [[]]\n", + " self.run_test(input_set, expected)\n", + " input_set = ['a']\n", + " expected = [['a'], []]\n", + " self.run_test(input_set, expected)\n", + " input_set = ['a', 'b']\n", + " expected = [['a'], ['a', 'b'], ['b'], []]\n", + " self.run_test(input_set, expected)\n", + " input_set = ['a', 'b', 'c']\n", + " expected = [['a'], ['a', 'b'], ['b'], ['a', 'c'], \n", + " ['a', 'b', 'c'], ['b', 'c'], ['c'], []]\n", + " self.run_test(input_set, expected)\n", + " print('Success: test_power_set')\n", + "\n", + " def run_test(self, input_set, expected):\n", + " combinatoric = Combinatoric()\n", + " result = combinatoric.find_power_set_recursive(input_set)\n", + " assert_equal(result, expected)\n", + " result = combinatoric.find_power_set_iterative(input_set)\n", + " assert_equal(result, expected)\n", + "\n", + "\n", + "def main():\n", + " test = TestPowerSet()\n", + " test.test_power_set()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook]() for a discussion on algorithms and code solutions." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.5.0" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/recursion_dynamic/power_set/power_set_solution.ipynb b/recursion_dynamic/power_set/power_set_solution.ipynb new file mode 100644 index 0000000..451ce1b --- /dev/null +++ b/recursion_dynamic/power_set/power_set_solution.ipynb @@ -0,0 +1,270 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Solution Notebook" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Problem: Return all subsets of a set.\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", + "* Should the resulting subsets be unique?\n", + " * Yes, treat 'ab' and 'bc' as the same\n", + "* Is the empty set included as a subset?\n", + " * Yes\n", + "* Are the inputs unique?\n", + " * No\n", + "* Can we assume the inputs are valid?\n", + " * No\n", + "* Can we assume this fits memory?\n", + " * Yes" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "
\n",
+    "* None -> None\n",
+    "* '' -> ['']\n",
+    "* 'a' -> ['a', '']\n",
+    "* 'ab' -> ['a', 'ab', 'b', '']\n",
+    "* 'abc' -> ['a', 'ab', 'abc', 'ac',\n",
+    "            'b', 'bc', 'c', '']\n",
+    "* 'aabc' -> ['a', 'aa', 'aab', 'aabc', \n",
+    "             'aac', 'ab', 'abc', 'ac', \n",
+    "             'b', 'bc', 'c', '']\n",
+    "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "* Build a dictionary of {chars: counts} where counts is the number of times each char is found in the input\n", + "* Loop through each item in the dictionary\n", + " * Keep track of the current index (first item will have current index 0)\n", + " * If the char's count is 0, continue\n", + " * Decrement the current char's count in the dictionary\n", + " * Add the current char to the current results\n", + " * Add the current result to the results\n", + " * Recurse, passing in the current index as the new starting point index\n", + " * When we recurse, we'll check if current index < starting point index, and if so, continue\n", + " * This avoids duplicate results such as 'ab' and 'bc'\n", + " * Backtrack by:\n", + " * Removing the just added current char from the current results\n", + " * Incrementing the current char's acount in the dictionary\n", + "\n", + "Complexity:\n", + "* Time: O(2^n)\n", + "* Space: O(2^n) if we are saving each result, or O(n) if we are just printing each result\n", + "\n", + "We are doubling the number of operations every time we add an element to the results: O(2^n).\n", + "\n", + "Note, you could also use the following method to solve this problem:\n", + "\n", + "
\n",
+    "number binary  subset\n",
+    "0      000      {}\n",
+    "1      001      {c}\n",
+    "2      010      {b}\n",
+    "3      011      {b,c}\n",
+    "4      100      {a}\n",
+    "5      101      {a,c}\n",
+    "6      110      {a,b}\n",
+    "7      111      {a,b,c}\n",
+    "
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from collections import OrderedDict\n", + "\n", + "\n", + "class Combinatoric(object):\n", + "\n", + " def _build_counts_map(self, string):\n", + " counts_map = OrderedDict()\n", + " for char in string:\n", + " if char in counts_map:\n", + " counts_map[char] += 1\n", + " else:\n", + " counts_map[char] = 1\n", + " return counts_map\n", + "\n", + " def find_power_set(self, string):\n", + " if string is None:\n", + " return string\n", + " if string == '':\n", + " return ['']\n", + " counts_map = self._build_counts_map(string)\n", + " curr_results = []\n", + " results = []\n", + " self._find_power_set(counts_map, curr_results,\n", + " results, index=0)\n", + " results.append('')\n", + " return results\n", + "\n", + " def _find_power_set(self, counts_map, curr_result,\n", + " results, index):\n", + " for curr_index, char in enumerate(counts_map):\n", + " if curr_index < index or counts_map[char] == 0:\n", + " continue\n", + " curr_result.append(char)\n", + " counts_map[char] -= 1\n", + " results.append(''.join(curr_result))\n", + " self._find_power_set(counts_map, curr_result,\n", + " results, curr_index)\n", + " counts_map[char] += 1\n", + " curr_result.pop()" + ] + }, + { + "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_power_set.py\n" + ] + } + ], + "source": [ + "%%writefile test_power_set.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class TestPowerSet(object):\n", + "\n", + " def test_power_set(self):\n", + " input_set = ''\n", + " expected = ['']\n", + " self.run_test(input_set, expected)\n", + " input_set = 'a'\n", + " expected = ['a', '']\n", + " self.run_test(input_set, expected)\n", + " input_set = 'ab'\n", + " expected = ['a', 'ab', 'b', '']\n", + " self.run_test(input_set, expected)\n", + " input_set = 'abc'\n", + " expected = ['a', 'ab', 'abc', 'ac',\n", + " 'b', 'bc', 'c', '']\n", + " self.run_test(input_set, expected)\n", + " input_set = 'aabc'\n", + " expected = ['a', 'aa', 'aab', 'aabc', \n", + " 'aac', 'ab', 'abc', 'ac', \n", + " 'b', 'bc', 'c', '']\n", + " self.run_test(input_set, expected)\n", + " print('Success: test_power_set')\n", + "\n", + " def run_test(self, input_set, expected):\n", + " combinatoric = Combinatoric()\n", + " result = combinatoric.find_power_set(input_set)\n", + " assert_equal(result, expected)\n", + "\n", + "\n", + "def main():\n", + " test = TestPowerSet()\n", + " test.test_power_set()\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_power_set\n" + ] + } + ], + "source": [ + "%run -i test_power_set.py" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.4.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/recursion_dynamic/power_set/test_power_set.py b/recursion_dynamic/power_set/test_power_set.py new file mode 100644 index 0000000..e64b59e --- /dev/null +++ b/recursion_dynamic/power_set/test_power_set.py @@ -0,0 +1,39 @@ +from nose.tools import assert_equal + + +class TestPowerSet(object): + + def test_power_set(self): + input_set = '' + expected = [''] + self.run_test(input_set, expected) + input_set = 'a' + expected = ['a', ''] + self.run_test(input_set, expected) + input_set = 'ab' + expected = ['a', 'ab', 'b', ''] + self.run_test(input_set, expected) + input_set = 'abc' + expected = ['a', 'ab', 'abc', 'ac', + 'b', 'bc', 'c', ''] + self.run_test(input_set, expected) + input_set = 'aabc' + expected = ['a', 'aa', 'aab', 'aabc', + 'aac', 'ab', 'abc', 'ac', + 'b', 'bc', 'c', ''] + self.run_test(input_set, expected) + print('Success: test_power_set') + + def run_test(self, input_set, expected): + combinatoric = Combinatoric() + result = combinatoric.find_power_set(input_set) + assert_equal(result, expected) + + +def main(): + test = TestPowerSet() + test.test_power_set() + + +if __name__ == '__main__': + main() \ No newline at end of file