>>> dp_count([1, 2, 3], 4) 4 >>> dp_count([1, 2, 3], 7) 8 >>> dp_count([2, 5, 3, 6], 10) 5 >>> dp_count([10], 99) 0 >>> dp_count([4, 5, 6], 0) 1 >>> dp_count([1, 2, 3], -5) 0
(s, n)
| 8 | |
| 9 | |
| 10 | def dp_count(s, n): |
| 11 | """ |
| 12 | >>> dp_count([1, 2, 3], 4) |
| 13 | 4 |
| 14 | >>> dp_count([1, 2, 3], 7) |
| 15 | 8 |
| 16 | >>> dp_count([2, 5, 3, 6], 10) |
| 17 | 5 |
| 18 | >>> dp_count([10], 99) |
| 19 | 0 |
| 20 | >>> dp_count([4, 5, 6], 0) |
| 21 | 1 |
| 22 | >>> dp_count([1, 2, 3], -5) |
| 23 | 0 |
| 24 | """ |
| 25 | if n < 0: |
| 26 | return 0 |
| 27 | # table[i] represents the number of ways to get to amount i |
| 28 | table = [0] * (n + 1) |
| 29 | |
| 30 | # There is exactly 1 way to get to zero(You pick no coins). |
| 31 | table[0] = 1 |
| 32 | |
| 33 | # Pick all coins one by one and update table[] values |
| 34 | # after the index greater than or equal to the value of the |
| 35 | # picked coin |
| 36 | for coin_val in s: |
| 37 | for j in range(coin_val, n + 1): |
| 38 | table[j] += table[j - coin_val] |
| 39 | |
| 40 | return table[n] |
| 41 | |
| 42 | |
| 43 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected