mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-03 16:08:02 +00:00
167 lines
4.0 KiB
Python
167 lines
4.0 KiB
Python
{
|
|
"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); 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)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Code"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"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]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Unit Test\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Overwriting test_coin_change_ways.py\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%writefile test_coin_change_ways.py\n",
|
|
"import unittest\n",
|
|
"\n",
|
|
"\n",
|
|
"class Challenge(unittest.TestCase):\n",
|
|
"\n",
|
|
" def test_coin_change_ways(self,solution):\n",
|
|
" self.assertEqual(solution(0, [1, 2]), 0)\n",
|
|
" self.assertEqual(solution(100, [1, 2, 3]), 884)\n",
|
|
" self.assertEqual(solution(1000, range(1, 101)), \n",
|
|
" 15658181104580771094597751280645)\n",
|
|
" print('Success: test_coin_change_ways')\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" test = Challenge()\n",
|
|
" test.test_coin_change_ways(change_ways)\n",
|
|
"\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Success: test_coin_change_ways\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%run -i test_coin_change_ways.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.7.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|