$10K coding deathmatch

Jacob Fugal lukfugl at gmail.com
Fri Nov 3 17:25:40 MST 2006


On 11/3/06, Levi Pearson <levi at cold.org> wrote:
> On Nov 3, 2006, at 4:30 PM, Andrew McNabb wrote:
> > I'm a big fan of Python, but this example made me want to cry.
> >
> > Grounds for first bullet:
> >>        value = reduce(lambda x,y: 0 < abs(x-y) <= len(nums) and y or
> >>        None, [int(item) for item in nums])
> >
> > Grounds for second bullet:
> >>        print '%smatch' % (not value and 'Not a ' or '')
> >
>
> Are you objecting to the use of logical operators as conditionals?
> The list comprehension?  The anonymous function?  The functional
> style in general?

I think the first bullet has to do not with the structure of the
statement, just the syntax. It does appear rather hard to follow to
me, and python is a language that prides itself (and I think rightly
so, in most cases) in readability. Compare the same approach -- even
if not equivalent in code -- in Ruby:

  begin
    range = (1...nums.length)
    nums = nums.map{ |item| Integer(item) }
    nums.inject{ |x,y| (range === (x - y).abs) ? y : raise)
    print "match"
  rescue
    print "not a match"
  end

I much prefer the use of the ternary operator (in limited cases like
this) over an "x and y or z" approach. I also prefer the explicit
exception, like you.

On the second bullet, why not something like:

  print(value and 'match' or 'not a match')

if you like the "x and y or z" approach"? It's much more succinct. I
don't think you really need to factor out the word "match". And for
me, preferring a ternary (does python have a ternary operator?):

  print value ? 'match' : 'not a match'

Jacob Fugal



More information about the PLUG mailing list