Discussion:
solving a small programm
broebel
2004-01-10 19:26:52 UTC
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)

"djw" <donald.welch.nospam at hp.com> schreef in bericht
As this is obviously a class assignment of some sorts, I don't know how
much
help you are going to get (or should get) in directly writing the code.
But, what I will say is that you should be able to look at the way the
code
works now, how it keeps track of how many bills are needed for each
denomination (200, 100, etc), and apply the same idea to the total number
of bills required using another variable that is initialized before the
loop begins.
-d
hey,
for the real programmers amongst you, this may be really annoying but
I've
been learning the language for only two days.
this is my problem,
in this programm,which already works I now have to make a total count of
how many coins are used.
this program gives the total per coin. It should be a small peace of
code
(as explained in a tutorial i read, without the answer.)that counts the
total of all the coins.
I ve been searching for two days and for this kind of programm it really
seems kind of long.
# Een bedrag gepast betalen met zo min mogelijk euromunten
bedrag = input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )
aantal = 0
aantal = aantal + 1
bedrag = bedrag - munt
print aantal, 'x', munt
for example
the programm gives
68= 50*1
10*1
5*1
2*1
1*1
I need an additional code to get the "5" total of all the coins
together.
djw
2004-01-10 19:28:09 UTC
Post by broebel
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)
I think you missed my point (and Paul Rubin's). If this is indeed homework
or a class assignment, you should not be asking people on this newsgroup to
do the work for you. We are happy to give some small hints or some guidance
as a teacher or instructor would.

-d
unknown
2004-01-10 18:39:08 UTC
That sounds like a homework problem, so rather than write the code for
you, here's a hint -- use another variable, and update it every time
you decide how many coins of any specific size you'll use.
Anton Vredegoor
2004-01-10 22:33:34 UTC
Post by broebel
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()
broebel
2004-01-10 19:26:26 UTC
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)

"Paul Rubin" <http://phr.cx at NOSPAM.invalid> schreef in bericht
Post by unknown
That sounds like a homework problem, so rather than write the code for
you, here's a hint -- use another variable, and update it every time
you decide how many coins of any specific size you'll use.
broebel
2004-01-10 18:27:35 UTC
hey,

for the real programmers amongst you, this may be really annoying but I've
been learning the language for only two days.

this is my problem,
in this programm,which already works I now have to make a total count of how
many coins are used.
this program gives the total per coin. It should be a small peace of code
(as explained in a tutorial i read, without the answer.)that counts the
total of all the coins.
I ve been searching for two days and for this kind of programm it really
seems kind of long.

# Een bedrag gepast betalen met zo min mogelijk euromunten

bedrag = input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )

for munt in 200, 100, 50, 20, 10, 5, 2, 1 :
aantal = 0

while bedrag >= munt :
aantal = aantal + 1
bedrag = bedrag - munt

if aantal > 0 :
print aantal, 'x', munt

what i need is:
for example
the programm gives
68= 50*1
10*1
5*1
2*1
1*1
I need an additional code to get the "5" total of all the coins together.
djw
2004-01-10 18:40:30 UTC
As this is obviously a class assignment of some sorts, I don't know how much
help you are going to get (or should get) in directly writing the code.
But, what I will say is that you should be able to look at the way the code
works now, how it keeps track of how many bills are needed for each
denomination (200, 100, etc), and apply the same idea to the total number
of bills required using another variable that is initialized before the
loop begins.

-d
Post by broebel
hey,
for the real programmers amongst you, this may be really annoying but I've
been learning the language for only two days.
this is my problem,
in this programm,which already works I now have to make a total count of
how many coins are used.
this program gives the total per coin. It should be a small peace of code
(as explained in a tutorial i read, without the answer.)that counts the
total of all the coins.
I ve been searching for two days and for this kind of programm it really
seems kind of long.
# Een bedrag gepast betalen met zo min mogelijk euromunten
bedrag = input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )
aantal = 0
aantal = aantal + 1
bedrag = bedrag - munt
print aantal, 'x', munt
for example
the programm gives
68= 50*1
10*1
5*1
2*1
1*1
I need an additional code to get the "5" total of all the coins together.
H.V
2004-01-11 16:12:47 UTC
Post by broebel
hey,
for the real programmers amongst you, this may be really annoying but I've
been learning the language for only two days.
this is my problem,
in this programm,which already works I now have to make a total count of how
many coins are used.
this program gives the total per coin. It should be a small peace of code
(as explained in a tutorial i read, without the answer.)that counts the
total of all the coins.
I ve been searching for two days and for this kind of programm it really
seems kind of long.
# Een bedrag gepast betalen met zo min mogelijk euromunten
bedrag = input ( 'Geef bedrag tussen 0 en 500 eurocent: ' )
aantal = 0
aantal = aantal + 1
bedrag = bedrag - munt
print aantal, 'x', munt
for example
the programm gives
68= 50*1
10*1
5*1
2*1
1*1
I need an additional code to get the "5" total of all the coins together.
--
An alternative to keeping track of coins as you go would be to store
your results in some kind of data structure : use a dict whose keys
are the coin type and whose values ate the number of each type of coin
needed. Then you can just add all the "values" . I like the data
centric approach . Your code doesn't seem to exploit any of the
convenient data structures at all .

sulf