{ "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", "
\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",
    "
\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 }