exercise: partition a list by equivalence

John Machin sjmachin at lexicon.net
Fri Feb 18 19:52:46 EST 2005


John Lenton wrote:
> On Fri, Feb 18, 2005 at 03:21:10PM -0800, John Machin wrote:
> > Not robust in the face of input like:
> > [[1,1]]
> > or
> > [[1,2], [1,2]]
> > or
> > [[1,2], [2,1]]
> > or
> > [[1,2], [2,3], [3,1]]
>
> oops, my bad.
>
> >
> > needs "if first == second: continue" here
> >
> > >             if has_first and has_second:
> >
> > needs "if rev[first] == rev[second]: continue" here
>
> an 'is' is enough, and better.

Good point. You're redeeming yourself :-)

>
> > >                 rev[first].extend(rev[second])
> > >                 rev[second][:] = []
> > >                 rev[second] = rev[first]
> > >             elif has_first:
> > >                 rev[first].append(second)
> > >                 rev[second] = rev[first]
> > >             elif has_second:
> > >                 rev[second].append(first)
> > >                 rev[first] = rev[second]
> > >             else:
> > >                 copy = [first, second]
> > >                 res.append(copy)
> >
> > My reaction to the "magic" by which res grows was "omigod that's
the
> > most horrible thing I've seen for quite a while" but there was
worse to
> > come :-)
>
> what is magic about it? is it really that horrible?

Try explaining to the newbies over on the tutor list how despite "res"
only ever *explicitly* having little bits like [3, 4] appended to it,
it (or more properly the thing to which it refers) is actually
festering and growing and being mutated under the surface until at the
finale it bursts out dripping slime just like the creature from the
black lagoon ...




More information about the Python-list mailing list