Search This Blog

Sunday, February 2, 2014

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents

#!/usr/bin/python

def find_ways(money,denom):
    next_denom=0
    while denom>0:
        if denom==25:
            next_denom=10
            break
        elif denom==10:
            next_denom=5
            break
        elif denom==5:
            next_denom=1
            break
        elif denom==1:
            return 1       
    ways=0
    i=0
    while i*denom<=money:
        ways +=find_ways(money-i*denom,next_denom)
        i=i+1
    return ways

result=find_ways(100,25)
print result

No comments:

My Profile

My photo
can be reached at 09916017317