From 3370106d36f9cb1f03591a155b52c4880e4eb23e Mon Sep 17 00:00:00 2001 From: mrb00l34n Date: Sat, 18 Jul 2015 23:21:29 -0400 Subject: [PATCH 1/2] Coin Change Ways Problem/Solution --- README.md | 1 + .../coinchange_ways_challenge.ipynb | 164 ++++++++++++++++ .../coinchange_ways_solution.ipynb | 180 ++++++++++++++++++ .../coinchange_ways/test_coinchange_ways.py | 19 ++ 4 files changed, 364 insertions(+) create mode 100644 recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb create mode 100644 recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb create mode 100644 recursion_dynamic/coinchange_ways/test_coinchange_ways.py diff --git a/README.md b/README.md index 2908dbb..7fbd04a 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,7 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup | Print all valid combinations of n-pairs of parentheses | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Implement a paint fill function | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Find all permutations to represent n cents, given 1, 5, 10, 25 cent coins | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | +| Find the number of ways to represent n cents given k arbitrary cent coins | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_solution.ipynb) | | Add a challenge | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) |
diff --git a/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb b/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb new file mode 100644 index 0000000..ecabaa8 --- /dev/null +++ b/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb @@ -0,0 +1,164 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [mrb00l34n](http://github.com/mrb00l34n). 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: Counting Ways of Making Change\n", + "\n", + "* [Explanation](#Explanation)\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": [ + "## Explanation\n", + "\n", + "How many ways are there of making change for n, given k distinct coins? For example:\n", + "\n", + "Input: n = 4, k = 2, coins = [1,2]\n", + "\n", + "Output: 3. 1+1+1+1, 1+2+1, 2+2, would be the ways of making change.\n", + "\n", + "Note that a coin can be used any number of times, and we are counting unique combinations." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Test Cases\n", + "\n", + "* Input: n = 0, k = 2, coins = [1,2] -> Output: 0\n", + "* Input: n = 100, k = 3, coins = [1,2,3] -> Output: 884\n", + "* Input: n = 1000, k = 100, coins = [1,2,3...99,100] -> Output: 15658181104580771094597751280645\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coinchange_ways/coinchange_ways_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 change_ways(n,k,coins):\n", + " # TODO: Implement me\n", + " return n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + "\n", + "\n", + "\n", + "**The following unit test is expected to fail until you solve the challenge.**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name":"stdout", + "text":[], + "output_type": "stream" + } + ], + "source": [ + "# %load test_coinchange_ways.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class Challenge(object):\n", + "\n", + " def test_coinchange_ways(self,solution):\n", + " assert_equal(solution(0,2,[1,2]), 0)\n", + " assert_equal(solution(100,3,[1,2,3]), 884)\n", + " assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645)\n", + " print('Success: test_coinchange_ways')\n", + "\n", + "\n", + "def main():\n", + " test = Challenge()\n", + " test.test_coinchange_ways(change_ways)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Solution Notebook\n", + "\n", + "Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coinchange_ways/coinchange_ways_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/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb b/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb new file mode 100644 index 0000000..424159c --- /dev/null +++ b/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb @@ -0,0 +1,180 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This notebook was prepared by [mrb00l34n](http://github.com/mrb00l34n). 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: Counting Ways of Making Change\n", + "\n", + "* [Hints](#Hints)\n", + "* [Algorithm](#Algorithm)\n", + "* [Code](#Code)\n", + "* [Unit Test](#Unit-Test)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Hints\n", + "\n", + "* Can you think of a way to build up to a solution?\n", + "* If there are 2 ways of making 3, and you are now given a coin of value v, how many ways can you make 3+v?\n", + "* Can you think of a way to divide the problem into smaller subproblems?" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Algorithm\n", + "\n", + "One possible solution using dynamic programming:\n", + "* Create an array, s.t arr[i] = # of ways to make change for i\n", + "* Initialize arr[0] = 1, arr[i>0] = 0\n", + "* For each coin, and for each index from coin to n, increment arr[i] by arr[i-coin]\n", + "\n", + "How does this work?\n", + "* As we iterate through each coin, we are adding the ways of making arr[i-coin] to arr[i]\n", + "* If we have 2 ways of making 4, and are now iterating on a coin of value 3, there should be 2 ways of making 7.\n", + "* We are essentially adding the coin we are iterating on to the # of ways of making arr[i].\n", + "\n", + "Complexity:\n", + "* Time: O(mn)\n", + "* Space: O(n)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Code" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def change_ways(n,k,coins):\n", + " arr = [1] + [0] * n\n", + " for coin in coins:\n", + " for i in xrange(coin,n + 1):\n", + " arr[i] += arr[i - coin]\n", + " return 0 if n == 0 else arr[n]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Test\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting test_coinchange_ways.py\n" + ] + } + ], + "source": [ + "%%writefile test_coinchange_ways.py\n", + "from nose.tools import assert_equal\n", + "\n", + "\n", + "class Challenge(object):\n", + "\n", + " def test_coinchange_ways(self,solution):\n", + " assert_equal(solution(0,2,[1,2]), 0)\n", + " assert_equal(solution(100,3,[1,2,3]), 884)\n", + " assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645)\n", + " print('Success: test_coinchange_ways')\n", + "\n", + "\n", + "def main():\n", + " test = Challenge()\n", + " test.test_coinchange_ways(change_ways)\n", + "\n", + "\n", + "if __name__ == '__main__':\n", + " main()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Success: test_coinchange_ways\n" + ] + } + ], + "source": [ + "%run -i test_coinchange_ways.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "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/recursion_dynamic/coinchange_ways/test_coinchange_ways.py b/recursion_dynamic/coinchange_ways/test_coinchange_ways.py new file mode 100644 index 0000000..0d8aef0 --- /dev/null +++ b/recursion_dynamic/coinchange_ways/test_coinchange_ways.py @@ -0,0 +1,19 @@ +from nose.tools import assert_equal + + +class Challenge(object): + + def test_coinchange_ways(self,solution): + assert_equal(solution(0,2,[1,2]), 0) + assert_equal(solution(100,3,[1,2,3]), 884) + assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645) + print('Success: test_coinchange_ways') + + +def main(): + test = Challenge() + test.test_coinchange_ways(change_ways) + + +if __name__ == '__main__': + main() \ No newline at end of file From e8de164d9dd5009de05472ec861661e4db4f96ea Mon Sep 17 00:00:00 2001 From: mrb00l34n Date: Wed, 22 Jul 2015 11:31:38 -0400 Subject: [PATCH 2/2] Fixed pull request changes --- README.md | 2 +- .../coinchange_ways_challenge.ipynb | 18 ++++++------- .../coinchange_ways_solution.ipynb | 26 +++++++++---------- .../coinchange_ways/test_coinchange_ways.py | 8 +++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 7fbd04a..db5f37f 100644 --- a/README.md +++ b/README.md @@ -216,7 +216,7 @@ Challenges, solutions, and unit tests are presented in the form of **IPython/Jup | Print all valid combinations of n-pairs of parentheses | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Implement a paint fill function | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | | Find all permutations to represent n cents, given 1, 5, 10, 25 cent coins | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) | -| Find the number of ways to represent n cents given k arbitrary cent coins | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_solution.ipynb) | +| Find the number of ways to represent n cents given an array of coins | [Challenge](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_challenge.ipynb)│[Solution](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/recursion_dynamic/coin_change_ways/coinchange_ways_solution.ipynb) | | Add a challenge | [Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md)│[Contribute](https://github.com/donnemartin/interactive-coding-challenges/blob/master/CONTRIBUTING.md) |
diff --git a/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb b/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb index ecabaa8..214bd8f 100644 --- a/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb +++ b/recursion_dynamic/coinchange_ways/coinchange_ways_challenge.ipynb @@ -34,9 +34,9 @@ "source": [ "## Explanation\n", "\n", - "How many ways are there of making change for n, given k distinct coins? For example:\n", + "How many ways are there of making change for n, given an array of distinct coins? For example:\n", "\n", - "Input: n = 4, k = 2, coins = [1,2]\n", + "Input: n = 4, coins = [1, 2]\n", "\n", "Output: 3. 1+1+1+1, 1+2+1, 2+2, would be the ways of making change.\n", "\n", @@ -49,9 +49,9 @@ "source": [ "## Test Cases\n", "\n", - "* Input: n = 0, k = 2, coins = [1,2] -> Output: 0\n", - "* Input: n = 100, k = 3, coins = [1,2,3] -> Output: 884\n", - "* Input: n = 1000, k = 100, coins = [1,2,3...99,100] -> Output: 15658181104580771094597751280645\n" + "* Input: n = 0, coins = [1, 2] -> Output: 0\n", + "* Input: n = 100, coins = [1, 2, 3] -> Output: 884\n", + "* Input: n = 1000, coins = [1, 2, 3...99, 100] -> Output: 15658181104580771094597751280645\n" ] }, { @@ -78,7 +78,7 @@ }, "outputs": [], "source": [ - "def change_ways(n,k,coins):\n", + "def change_ways(n, coins):\n", " # TODO: Implement me\n", " return n" ] @@ -115,9 +115,9 @@ "class Challenge(object):\n", "\n", " def test_coinchange_ways(self,solution):\n", - " assert_equal(solution(0,2,[1,2]), 0)\n", - " assert_equal(solution(100,3,[1,2,3]), 884)\n", - " assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645)\n", + " assert_equal(solution(0, [1, 2]), 0)\n", + " assert_equal(solution(100, [1, 2, 3]), 884)\n", + " assert_equal(solution(1000, range(1, 101)), 15658181104580771094597751280645)\n", " print('Success: test_coinchange_ways')\n", "\n", "\n", diff --git a/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb b/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb index 424159c..c8706fd 100644 --- a/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb +++ b/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb @@ -33,7 +33,7 @@ "## Hints\n", "\n", "* Can you think of a way to build up to a solution?\n", - "* If there are 2 ways of making 3, and you are now given a coin of value v, how many ways can you make 3+v?\n", + "* If there are 2 ways of making 3, and you are now given a coin of value v, how many ways can you make 3 + v?\n", "* Can you think of a way to divide the problem into smaller subproblems?" ] }, @@ -46,15 +46,15 @@ "One possible solution using dynamic programming:\n", "* Create an array, s.t arr[i] = # of ways to make change for i\n", "* Initialize arr[0] = 1, arr[i>0] = 0\n", - "* For each coin, and for each index from coin to n, increment arr[i] by arr[i-coin]\n", + "* For each coin, and for each index from coin to n, increment arr[i] by arr[i - coin]\n", "\n", "How does this work?\n", - "* As we iterate through each coin, we are adding the ways of making arr[i-coin] to arr[i]\n", + "* As we iterate through each coin, we are adding the ways of making arr[i - coin] to arr[i]\n", "* If we have 2 ways of making 4, and are now iterating on a coin of value 3, there should be 2 ways of making 7.\n", "* We are essentially adding the coin we are iterating on to the # of ways of making arr[i].\n", "\n", "Complexity:\n", - "* Time: O(mn)\n", + "* Time: O(mn); let the number of coins be m. We iterate from arr[coin] -> arr[n], or ~ n operations on each coin, hence n*m. \n", "* Space: O(n)" ] }, @@ -73,12 +73,12 @@ }, "outputs": [], "source": [ - "def change_ways(n,k,coins):\n", - " arr = [1] + [0] * n\n", - " for coin in coins:\n", - " for i in xrange(coin,n + 1):\n", - " arr[i] += arr[i - coin]\n", - " return 0 if n == 0 else arr[n]" + "def change_ways(n, coins):\n", + " arr = [1] + [0] * n\n", + " for coin in coins:\n", + " for i in range(coin, n + 1):\n", + " arr[i] += arr[i - coin]\n", + " return 0 if n == 0 else arr[n]" ] }, { @@ -112,9 +112,9 @@ "class Challenge(object):\n", "\n", " def test_coinchange_ways(self,solution):\n", - " assert_equal(solution(0,2,[1,2]), 0)\n", - " assert_equal(solution(100,3,[1,2,3]), 884)\n", - " assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645)\n", + " assert_equal(solution(0, [1, 2]), 0)\n", + " assert_equal(solution(100, [1, 2, 3]), 884)\n", + " assert_equal(solution(1000, range(1, 101)), 15658181104580771094597751280645)\n", " print('Success: test_coinchange_ways')\n", "\n", "\n", diff --git a/recursion_dynamic/coinchange_ways/test_coinchange_ways.py b/recursion_dynamic/coinchange_ways/test_coinchange_ways.py index 0d8aef0..2e9d0a2 100644 --- a/recursion_dynamic/coinchange_ways/test_coinchange_ways.py +++ b/recursion_dynamic/coinchange_ways/test_coinchange_ways.py @@ -4,9 +4,9 @@ from nose.tools import assert_equal class Challenge(object): def test_coinchange_ways(self,solution): - assert_equal(solution(0,2,[1,2]), 0) - assert_equal(solution(100,3,[1,2,3]), 884) - assert_equal(solution(1000,100,range(1,101)), 15658181104580771094597751280645) + assert_equal(solution(0, [1, 2]), 0) + assert_equal(solution(100, [1, 2, 3]), 884) + assert_equal(solution(1000, range(1, 101)), 15658181104580771094597751280645) print('Success: test_coinchange_ways') @@ -16,4 +16,4 @@ def main(): if __name__ == '__main__': - main() \ No newline at end of file + main()