[Tutor] programming exercise in Python
Kermit Rose
kermit at polaris.net
Wed Aug 9 19:29:49 CEST 2006
From: Alan Gauld
Date: 08/09/06 12:53:59
To: Kermit Rose; tutor at python.org
Subject: Re: [Tutor] programming exercise in Python
> # lenmult = len(mult)
This is pretty redundant, it only saves two characters typing,
you might as well do
******
hmm....
I had gotten in the habit of replacing potential multiple function calls
and array references with variable name references
acting from the hypothesis
that variable name references take much less time than array referrences or
function calls.
I agree that in this case I did not need to do so.
>>>>>>>
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.
******
Yes. I see that now.
I did see that I asked the computer to do extra work by
having it do work conditional of the initial value of k, but did not
immediately see
how to move that work outside the loop.
>>>>>>>>
> # 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...
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],
etc
>>>>>>>>>>>>
> # 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
*****
The indentation did not survive repeated postings.
.. if testsum < zlim:
.. return mult
.. mult[0] = -1
.. return mult
if testsum < zlim I return mult
if testsum >= zlim, I advance the k loop
failure condition is that it get all the way through the k loop and not be
able to increment mult.
>>>>>>>>>
As a matter of interest what does this bizarre algorithm
accomplish in a practical sense? What is its purpose?
I am truly intrigued...
*******
:)
The incr routine generates a vector of exponents to apply to the list
mpylist [n] [0]
m = product of mpylist [k] [0] ** mult[k]
Another subroutine uses the number m
in an algorithm I've devised,
to find a divisor of the number z,
from which I had calculated the critical value of zlim,
and the mpylist vector.
Experimentation will tell if this factor routine is as good as I would like
it to be.
Kermit < kermit at polaris.net >
More information about the Tutor
mailing list