Improving style (and speed)?

Alex Martelli alex at magenta.com
Thu Aug 3 18:16:44 EDT 2000


"ingo" <ingoogni at home.nl> wrote in message
news:8F85D66E7seed7 at 212.120.66.218...
    [snip]
> I did it. But I can't find a better, more elegant, way to do it. Lack of
> experience. So I'm looking for some guidance.
    [snip]
> if MinIndex==0:
>     Posy=Posy-1
>     Posz=Posz-1
> elif MinIndex==1:
>     Posy=Posy
>     Posz=Posz-1
> elif MinIndex==2:
>     Posy=Posy+1
>     Posz=Posz-1
> elif MinIndex==3:
>     Posy=Posy-1
>     Posz=Posz
> elif MinIndex==4:
>     Posy=Posy
>     Posz=Posz
> elif MinIndex==5:
>     Posy=Posy+1
>     Posz=Posz
> elif MinIndex==6:
>     Posy=Posy+1
>     Posz=Posz-1
> elif MinIndex==7:
>     Posy=Posy+1
>     Posz=Posz
> elif MinIndex==8:
>     Posy=Posy+1
>     Posz=Posz+1

Let's see the deltas on Posy/Posz depending on MinIndex...:

MinIndex    0    1    2    3    4    5    6    7    8
delta y     -1   0    1    -1   0    1    1    1    1
delta z     -1   -1   -1   0    0    0    -1   0    1

Now this is peculiar.  Are the deltas truly meant to be
identical and indistinguishable for minindex of 5 and 7,
for example?  Or aren't actually y and z confused in the
cases 6 7 8 so that it should actually be...:

MinIndex    0    1    2    3    4    5    6    7    8
delta y     -1   0    1    -1   0    1    -1   0    1
delta z     -1   -1   -1   0    0    0    1    1    1

this seems so much more natural and regular...!  Isn't
this the right table?


If it is, then delta y is (minindex modulo 3) minus 1,
and delta z is (minindex integer-divided 3) minus 1.


So, in Python, the above set of lines could be changed
into:

Posy=Posy+(MinIndex%3)-1
Posz=Posz+(MinIndex/3)-1

or equivalent expressions -- if we had elementwise
arithmetic, as per the recent proposal, for example:

Posz,Posy=(Posz,Posy)~+divmod(MinIndex,3)~-(1,1)

[but we don't, so don't try this:-)].


But that's dependent on there being a bug in your
code as presented.  Is there...?


Alex






More information about the Python-list mailing list