Blog Archive

Friday, August 22, 2014

Find all combinations of coins when given some dollar value

"""
http://elementsofprogramminginterviews.com/pdf/epi-light-1.4.8.pdf
17.1 Count the number of score combinations
In an American football game, a play can lead to 2 points (safety), 3 points (field
goal), or 7 points (touchdown). Given the final score of a game, we want to compute
how many di erent combinations of 2, 3, and 7 point plays could make up this score.
For example, if W = f2; 3; 7g, four combinations of plays yield a score of 12:
6 safeties (2 6 = 12),
3 safeties and 2 field goals (2 3 + 3 2 = 12),
1 safety, 1 field goal and 1 touchdown (2 1 + 3 1 + 7 1 = 12), and
4 field goals (3 4 = 12).
Problem 17.1 : You have an aggregate score s and W which specifies the points that
can be scored in an individual play. Howwould you find the number of combinations
of plays that result in an aggregate score of s? How would you compute the number
of distinct sequences of individual plays that result in a score of s?

"""

def count_combinations(k, score_ways):
combinations = [0] * (k+1)
combinations[0] = 1
for score in score_ways:
print "score=", score
for j in range(score, k+1):
combinations[j] += combinations[j - score]

return combinations[k]

"""
hint:
https://andrew.neitsch.ca/publications/m496pres1.nb.pdf
http://stackoverflow.com/questions/1106929/find-all-combinations-of-coins-when-given-some-dollar-value

"""


No comments:

Post a Comment