[Tutor] programming exercise in Python

Kermit Rose kermit at polaris.net
Wed Aug 9 20:28:26 CEST 2006


 
 
 
From: Alan Gauld 
Date: 08/09/06 13:52:09 
To: Kermit Rose 
Cc: tutor at python.org 
Subject: Re: [Tutor] programming exercise in Python 
 
>> # testsum = 0 
>> # if k > 0: 
>> # mult[k-1] = 0 
>> # for j in range(k,lenmult): 
>> # testsum = testsum + mpylist[j][1] * mult[j] 
> 
> My brain is bending with this bit! I'm not sure if its right or 
> not... 
> 
> for k = 0, checks to see if it may add 1 to mult[0] 
> for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to 
> mult[1] 
> for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to 
> mult[2], 
 
So you work along mult setting each member to zero after incrementing 
it? 
 
 
******
 
Yes.  After each member has gone through all allowed incrementations, I
reset it to zero.
 
Remember that mult is returned as soon as any member is incremented.
 
 
>>>>>>>>>
 
> The incr routine generates a vector of exponents to apply to the 
> list 
> 
> mpylist [n] [0] 
> 
> m = product of mpylist [k] [0] ** mult[k] 
> 
 
But haven't you set all of mult[k] to zero, using the example 
algorithm above? 
 
*******
 
Ah, now I understandd how I misled you.
 
> for k = 0, checks to see if it may add 1 to mult[0] 
 
if it can add 1 to mult[0], do so, and return mult.  otherwise continue
 
> for k = 1, sets mult[0] = 0, and checks to see if it may add 1 to 
> mult[1] 
 
if it can add 1 to mult[1], do so, and return mult.  otherwise continue
 
> for k = 2, sets mult[1] = 0, and checks to see if it may add 1 to 
> mult[2], 
 
if it can add 1 to mult[2], do so, and return mult.  otherwise continue
etc
 
 
 
Kermit   <  kermit at polaris.net  >
 
 



More information about the Tutor mailing list