[Tutor] Exercise in writing a python function.
alan.gauld at freenet.co.uk
Wed Aug 9 09:30:03 CEST 2006
> The current specifications for the function are:
> def incr(mult,z,zlim,mpylist):
> # mult is a vector of exponents for the multipliers in mpylist.
> # z is a positive odd integer.
> # zlim is the upper bound critical value for the sum of (
> mpylist[k] *
> mpylist[k] )
Just to clarify what this means.
you have mpylist = [(2,3),(3,5),(5,7)]
So the products list is: [6,15,35]
So zlim in this case should be greater than the
sum of products: 6+15+35 = 56
Have I got that right?
> # where kth multiplier is mpylist [k]
> # and kth multiplier index is mpylist [k]
Not sure what you mean by the multiplier index?
The sum above shows mpylist[k] being used in the multiplication,
not as an index?
> # function incr returns the next value of vector mult.
I'm still not clear what mult does, your example above doesn't
refer to mult anywhere?
> # mult may be thought as a number written in a variable base.
> # mult is the least significant and matches multiplier
> # adding one to mult would mean adding 1 to mult
> # unless doing so would make sum of multiplier * index exceed zlim.
> # in that case mult is set to zero, and 1 is added to mult
> # unless doing so would make sum of multilier * index exceed zlim
> # in that case, mult and mult is set to zero,
> # and 1 is added to mult
> # unless . . .
Sorry, you lost me there, can you provide a concrete example?
Or maybe some of our resident math experts recognise what
you are up to and can explain? Brian? Danny?
> # mult is set to -1 to indicate that the largest possible value
> of mult
> has been exceeded.
> # mpylist = 2 and all other multipliers in mpylist are odd.
> # mult, the index on multiplier 2, must not equal 1. It may
> equal zero,
> or any integer > 1,
> # provided the zlim constraint is met.
The only guidance I'd give in this kind of situation is that I'd adopt
functional programming approaches and try to copy the structure
of the spec in my code. This will be a bitch to test/debug unless
you have a wealth of manually validated examples to use as test
Author of the Learn to Program web site
More information about the Tutor