PEP 308: Obfuscated Nested Ternaries (INACAIS)

Gerrit Holl gerrit at nl.linux.org
Fri Feb 21 07:31:15 EST 2003


Andrew Bennetts schreef op donderdag 20 februari om 23:49:11 +0000:
> On Thu, Feb 20, 2003 at 03:00:42PM +0100, Gerrit Holl wrote:
> > Hi,
> > 
> > For the Obfuscated Contest: rewrite this peace of code
> > using ternaries. Preferably with a lot of ('s and )'s :)
> > 
> >     if x > 0:
> >         if y > 0:
> >             seq = (tl, br, b, br, r, br)
> >         elif y == 0:
> >             seq = (l, r, t, r, b, r)
> >         else:
> >             seq = (bl, tr, t, tr, r, tr)
> >     elif x == 0:
> >         if y > 0:
> >             seq = (t, b, l, b, r, b)
> >         elif y == 0:
> >             seq = (tl, tr, br, bl, tl)
>                     ^^^^^^^^^^^^^^^^^^^^
>                     This looks buggy to me; why only 5 elements?

I use the code to draw an arrow, but if x == y == 0, there is no
direction to draw an arrow. That's why I'm drawing a rectangle.

But I think I did see a better way to implement it. The full code of
my arrow drawing function is:

def arrow(surf, (x, y)):
    # assumes 50x50
    high, mid, low = 40, 25, 10
    t = (mid, low)
    b = (mid, high)
    l = (low, mid)
    r = (high, mid)
    tl = (low, low)
    tr = (high, low)
    bl = (low, high)
    br = (high, high)

    color = (255, 255, 255)

    d = {(1, 1): (tl, br, b, br, r, br),
         (1, 0): (l, r, t, r, b, r),
         (1, -1): (bl, tr, t, tr, r, tr),
         (0, 1): (t, b, l, b, r, b),
         (0, 0): (tl, tr, br, bl, tl),
         (0, -1): (b, t, l, t, r, t),
         (-1, 1): (tr, bl, b, bl, l, bl),
         (-1, 0): (r, l, t, l, b, l),
         (-1, -1): (br, tl, t, tl, l, tl) }

    pygame.draw.lines(surf, color, False, d[(x, y)], 2)

yours,
Gerrit.

-- 
Asperger Syndroom - een persoonlijke benadering:
	http://people.nl.linux.org/~gerrit/
Het zijn tijden om je zelf met politiek te bemoeien:
	http://www.sp.nl/





More information about the Python-list mailing list