Add coin change challenge

This commit is contained in:
Donne Martin 2017-03-28 05:10:10 -04:00
parent 88d860076b
commit a97d35f86a
4 changed files with 414 additions and 0 deletions

View File

@ -0,0 +1,164 @@
{
"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: Determine the total number of unique ways to make n cents, given coins of denominations less than n cents.\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",
"* Do the coins have to reach exactly n cents?\n",
" * Yes\n",
"* Can we assume we have an infinite number of coins to make n cents?\n",
" * Yes\n",
"* Do we need to report the combination(s) of coins that represent the minimum?\n",
" * No\n",
"* Can we assume the coin denominations are given in sorted order?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* coins: None or n: None -> Exception\n",
"* coins: [] or n: 0 -> 0\n",
"* coins: [1, 2, 3], n: 5 -> 5"
]
},
{
"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/coin_change/coin_change_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": [
"class CoinChanger(object):\n",
"\n",
" def make_change(self, coins, total):\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": [],
"source": [
"# %load test_coin_change.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class Challenge(object):\n",
"\n",
" def test_coin_change(self):\n",
" coin_changer = CoinChanger()\n",
" assert_equal(coin_changer.make_change([1, 2], 0), 0)\n",
" assert_equal(coin_changer.make_change([1, 2, 3], 5), 5)\n",
" assert_equal(coin_changer.make_change([1, 5, 25, 50], 10), 3)\n",
" print('Success: test_coin_change')\n",
"\n",
"\n",
"def main():\n",
" test = Challenge()\n",
" test.test_coin_change()\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/coin_change/coin_change_solution.ipynb) 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
}

View File

@ -0,0 +1,230 @@
{
"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: Determine the total number of unique ways to make n cents, given coins of denominations less than n cents.\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",
"* Do the coins have to reach exactly n cents?\n",
" * Yes\n",
"* Can we assume we have an infinite number of coins to make n cents?\n",
" * Yes\n",
"* Do we need to report the combination(s) of coins that represent the minimum?\n",
" * No\n",
"* Can we assume the coin denominations are given in sorted order?\n",
" * No\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"* coins: None or n: None -> Exception\n",
"* coins: [] or n: 0 -> 0\n",
"* coins: [1, 2, 3], n: 5 -> 5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"We'll use a bottom-up dynamic programming approach.\n",
"\n",
"<pre>\n",
"The rows (i) represent the coin values.\n",
"The columns (j) represent the totals.\n",
"\n",
" -------------------------\n",
" | 0 | 1 | 2 | 3 | 4 | 5 |\n",
" -------------------------\n",
"0 | 1 | 0 | 0 | 0 | 0 | 0 |\n",
"1 | 1 | 1 | 1 | 1 | 1 | 1 |\n",
"2 | 1 | 1 | 2 | 2 | 3 | 3 |\n",
"3 | 1 | 1 | 2 | 3 | 4 | 5 |\n",
" -------------------------\n",
"\n",
"Number of ways to get total n with coin[n] equals:\n",
"* Number of ways to get total n with coin[n - 1] plus\n",
"* Number of ways to get total n - coin[n]\n",
"\n",
"if j == 0:\n",
" T[i][j] = 1\n",
"if row == 0:\n",
" T[i][j] = 0\n",
"if coins[i] >= j\n",
" T[i][j] = T[i - 1][j] + T[i][j - coins[i]]\n",
"else:\n",
" T[i][j] = T[i - 1][j]\n",
"\n",
"The answer will be in the bottom right corner of the matrix.\n",
"</pre>\n",
"\n",
"Complexity:\n",
"* Time: O(i * j)\n",
"* Space: O(i * j)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class CoinChanger(object):\n",
"\n",
" def make_change(self, coins, total):\n",
" if coins is None or total is None:\n",
" return None\n",
" if not coins or total == 0:\n",
" return 0\n",
" coins = [0] + coins\n",
" num_rows = len(coins)\n",
" num_cols = total + 1\n",
" T = [[None] * num_cols for _ in range(num_rows)]\n",
" for i in range(num_rows):\n",
" for j in range(num_cols):\n",
" if i == 0:\n",
" T[i][j] = 0\n",
" continue\n",
" if j == 0:\n",
" T[i][j] = 1\n",
" continue\n",
" if coins[i] <= j:\n",
" T[i][j] = T[i - 1][j] + T[i][j - coins[i]]\n",
" else:\n",
" T[i][j] = T[i - 1][j]\n",
" return T[num_rows - 1][num_cols - 1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_coin_change.py\n"
]
}
],
"source": [
"%%writefile test_coin_change.py\n",
"from nose.tools import assert_equal\n",
"\n",
"\n",
"class Challenge(object):\n",
"\n",
" def test_coin_change(self):\n",
" coin_changer = CoinChanger()\n",
" assert_equal(coin_changer.make_change([1, 2], 0), 0)\n",
" assert_equal(coin_changer.make_change([1, 2, 3], 5), 5)\n",
" assert_equal(coin_changer.make_change([1, 5, 25, 50], 10), 3)\n",
" print('Success: test_coin_change')\n",
"\n",
"\n",
"def main():\n",
" test = Challenge()\n",
" test.test_coin_change()\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_coin_change\n"
]
}
],
"source": [
"%run -i test_coin_change.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
}

View File

@ -0,0 +1,20 @@
from nose.tools import assert_equal
class Challenge(object):
def test_coin_change(self):
coin_changer = CoinChanger()
assert_equal(coin_changer.make_change([1, 2], 0), 0)
assert_equal(coin_changer.make_change([1, 2, 3], 5), 5)
assert_equal(coin_changer.make_change([1, 5, 25, 50], 10), 3)
print('Success: test_coin_change')
def main():
test = Challenge()
test.test_coin_change()
if __name__ == '__main__':
main()