$10K coding deathmatch

Dan Stovall dbstovall at gmail.com
Fri Nov 3 08:51:41 MST 2006


Here's my python version.  I think that the list comperhension and use
of the reduce function might help with the speed.  I have read that
they are supposed to be a bit faster than a standard for loop in
python.  However, I worry that using reduce to raise an exception may
take away any benefit from an early exit.  I doubt that python's
exception handling is all that optimized for speed.

import sys
for line in sys.stdin:
    nums = line.strip().split()
    try:
        value = reduce(lambda x,y: 0 < abs(x-y) <= len(nums) and y or
None, [int(item) for item in nums])
    except TypeError:
        ## early exit, reduce got an error subtracting None and int
        print 'Not a match'
    else:
        ## print match if the reduce function returns a value
        ## print Not a match if reduce function returns None
        print '%smatch' % (not value and 'Not a ' or '')



More information about the PLUG mailing list