[Tutor] programming exercise in Python

Alan Gauld alan.gauld at freenet.co.uk
Wed Aug 9 18:53:48 CEST 2006


> #     lenmult = len(mult)

This is pretty redundant, it only saves two characters typing,
you might as well do

for k in range(len(mult)):

> #     for k in range(lenmult):
> #         mult[k] = mult[k] + 1


> #         if k == 0:
> #             if mult[k] == 1:
> #                mult[k] = 2

The whole of this bit only happens on the first time through
the outer loop. The effect of it is to set mult[0] to 2 if its initial
value was 0, so why not move it outside the loop and do:

if mult[0] == 0: mult[0] = 1

Then the  increment of the whole list will uplift it to 2 along
with all the other increments.

> #         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...
I'm still not sure i understand what its trying to do...


> #         if testsum < zlim:
> #             return mult
> #     mult[0] = -1
> #     return mult

But I think this can be better expressed as:

              if testsum >= zlim:
                   mult[0] = -1   # failure condiotion
         return mult


As a matter of interest what does this bizarre algorithm
accomplish in a practical sense? What is its purpose?
I am truly intrigued...


HTH,

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



More information about the Tutor mailing list