interactive-coding-challenges/recursion_dynamic/coinchange_ways/coinchange_ways_solution.ipynb

181 lines
4.2 KiB
Plaintext
Raw Normal View History

2015-07-19 11:21:29 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<small><i>This notebook was prepared by [mrb00l34n](http://github.com/mrb00l34n). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).</i></small>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solution Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: 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",
2015-07-22 23:31:38 +08:00
"* 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",
2015-07-19 11:21:29 +08:00
"* 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",
2015-07-22 23:31:38 +08:00
"* For each coin, and for each index from coin to n, increment arr[i] by arr[i - coin]\n",
2015-07-19 11:21:29 +08:00
"\n",
"How does this work?\n",
2015-07-22 23:31:38 +08:00
"* As we iterate through each coin, we are adding the ways of making arr[i - coin] to arr[i]\n",
2015-07-19 11:21:29 +08:00
"* 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",
2015-07-22 23:31:38 +08:00
"* 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",
2015-07-19 11:21:29 +08:00
"* Space: O(n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2015-07-22 23:31:38 +08:00
"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]"
2015-07-19 11:21:29 +08:00
]
},
{
"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",
2015-07-22 23:31:38 +08:00
" 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",
2015-07-19 11:21:29 +08:00
" 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
}