This notebook was prepared by [mrb00l34n](http://github.com/mrb00l34n). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).
One possible solution using dynamic programming:
How does this work?
Complexity:
In [1]:
def change_ways(n, coins):
arr = [1] + [0] * n
for coin in coins:
for i in range(coin, n + 1):
arr[i] += arr[i - coin]
return 0 if n == 0 else arr[n]
In [2]:
%%writefile test_coin_change_ways.py
from nose.tools import assert_equal
class Challenge(object):
def test_coin_change_ways(self,solution):
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_coin_change_ways')
def main():
test = Challenge()
test.test_coin_change_ways(change_ways)
if __name__ == '__main__':
main()
In [3]:
%run -i test_coin_change_ways.py