fork download
  1. # your code goes here
  2. def count(S, m, n):
  3.  
  4. # table[i] will be storing the number of solutions for
  5. # value i. We need n+1 rows as the table is constructed
  6. # in bottom up manner using the base case (n = 0)
  7. # Initialize all table values as 0
  8. table = [0 for k in range(n+1)]
  9.  
  10. # Base case (If given value is 0)
  11. table[0] = 1
  12.  
  13. # Pick all coins one by one and update the table[] values
  14. # after the index greater than or equal to the value of the
  15. # picked coin
  16. for i in range(0,m):
  17. for j in range(S[i],n+1):
  18. table[j] += table[j-S[i]]
  19.  
  20. return table[n]
  21.  
  22. # Driver program to test above function
  23. arr = [1, 2, 3]
  24. m = len(arr)
  25. n = 4
  26. x = count(arr, m, n)
  27. print (x)
  28.  
  29.  
Success #stdin #stdout 0.02s 9116KB
stdin
Standard input is empty
stdout
4