solving a small programm

Anton Vredegoor anton at vredegoor.doge.nl
Sat Jan 10 17:33:34 EST 2004


"broebel" <computer.problemen at skynet.be> wrote:

>If you look at this as a homework or not is of no interest to me but I like
>the way you explained which way to go to find the resolution.
>thanks (i solved it in a jiffy now)

Now that it is solved it's fair game for anyone :-) I have no
information whether this is homework for you or not, and posting in a
public newsgroup defeats cheating.

Anton

def split(units,coins):
    for c in coins :
        n, units = divmod(units, c)
        if n: yield c, n
        if not units: raise StopIteration
    raise ValueError, 'no change!'

def test():
    coins = [200,100,50,20,10,5,2,1]
    for i in range(501):
        D = dict(split(i,coins))
        nc = sum(D.values())
        check = sum([c*n for c,n in D.items()])
        print "%3i: %i coins:"  %(i,nc) , D
        assert check == i

if __name__=='__main__':
    test()



More information about the Python-list mailing list