Re: [perl-python] exercise: partition a list by equivalence

2005-02-24 Thread Paul McGuire
A slightly better version, only walks the set of cumulative list of sets once per pairing. -- Paul .import sets . .input = [[1, 2], [3, 4], [2, 3], [4, 5]] .input = [[1, 2], [3, 4], [4, 5]] .input = [[1, 2],[2,1], [3, 4], [4, 5],[2,2],[2,3],[6,6]] . .def merge(pairings): .ret

Re: [perl-python] exercise: partition a list by equivalence

2005-02-23 Thread Paul McGuire
Please consider my submission also (Python 2.3-compatible). -- Paul McGuire .import sets . .input = [[1, 2], [3, 4], [2, 3], [4, 5]] .input = [[1, 2], [3, 4], [4, 5]] .input = [[1, 2],[2,1], [3, 4], [4, 5],[2,2],[2,3],[6,6]] . .def merge(pairings): .ret = [] .f

Re: [perl-python] exercise: partition a list by equivalence

2005-02-20 Thread Reinhold Birkenfeld
Reinhold Birkenfeld wrote: > My solution (which may not be the fastest or most effective, but till > now is the shortest and it works): > > def merge(pairings): > sets = {} > for x1, x2 in pairings: > newset = (sets.get(x1, frozenset([x1])) > | sets.get(x2, froz

[perl-python] exercise: partition a list by equivalence

2005-02-20 Thread Xah Lee
when i try to run the following program, Python complains about some global name frozenset is not defined. Is set some new facility in Python 2.4? ©# from Reinhold Birkenfeld ©def merge(pairings): ©sets = {} ©for x1, x2 in pairings: ©newset = (sets.get(x1, frozenset([x1])) ©

Re: [perl-python] exercise: partition a list by equivalence

2005-02-19 Thread Brian Beck
Reinhold Birkenfeld wrote: def merge(pairings): sets = {} for x1, x2 in pairings: newset = (sets.get(x1, frozenset([x1])) | sets.get(x2, frozenset([x2]))) for i in newset: sets[i] = newset return [list(aset) for aset in set(sets.itervalues()

Re: [perl-python] exercise: partition a list by equivalence

2005-02-19 Thread Reinhold Birkenfeld
Xah Lee wrote: > here's the answer to the partition by equivalence exercise. Your Python solution is, as expected, wrong (try with [[10, 8], [7, 3], [1, 7], [5, 4], [2, 2], [3, 8], [7, 10], [2, 3], [6, 10], [3, 2]] for example). The solution by John Lenton is wrong, too. The solution by Brian Be

[perl-python] exercise: partition a list by equivalence

2005-02-19 Thread Xah Lee
here's the answer to the partition by equivalence exercise. --- # Perl code sub merge($) { my @pairings = @{$_[0]}; my @interm; # array of hashs # chop the first value of @pairings into @interm $interm[0]={$pairings[0][0]=>'x'}; ${interm[0]}{$pairings[0]

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread Brian Beck
Well, it looks like David posted a good solution, but I haven't tested it (I'm assuming his works fine). I fixed mine up anyway... it actually works now. If you're not using 2.4 you'll have to import sets.Set as set. def merge(pairList): pairList = set(tuple(sorted(p)) for p in pairList)

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread Brian Beck
Brian Beck wrote: Brian Beck wrote: > [code] Ah heck, nevermind... it worked for my small test cases but the algorithm is just wrong. -- Brian Beck Adventurer of the First Order -- http://mail.python.org/mailman/listinfo/python-list

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread Brian Beck
Brian Beck wrote: > [code] Whoops, that should say: def merge(pairs): pairs = set(tuple(sorted(p)) for p in pairs) merged = [] # Each loop will result in a new, complete sublist in the result. while pairs: p = set(pairs.pop()) remove = set([]) for pair in pai

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread Brian Beck
Xah Lee wrote: merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes. For example, if the input is merge( [ [1,2], [2,4], [5,6] ] ); that means 1 and 2 are the same. 2 and 4 are the same. Therefore 1==2==4. The result

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread Cyril BAZIN
Hello John, Try your python code on this example: merge([[1,2], [3,4], [1,2], [5,3]]) The result given by your function is: [[3, 4, 5]] Sorry... To Xah: next time you propose an exercise, write some UNIT TESTS!!! Then people will be able to test if there answers are correct or not. Cyril

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread John Lenton
On Thu, Feb 17, 2005 at 03:46:20PM -0800, Xah Lee wrote: > here's another interesting algorithmic exercise, again from part of a > larger program in the previous series. > > Here's the original Perl documentation: > > =pod > > merge($pairings) takes a list of pairs, each pair indicates the > sam

Re: [perl-python] exercise: partition a list by equivalence

2005-02-18 Thread anton muhin
Xah Lee wrote: here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same in

Re: [perl-python] exercise: partition a list by equivalence

2005-02-17 Thread alex23
>For those of you unfamiliar with math lingoes, partition a list means > to create sublists, based on some criteria. Typical moronic mathematicians with their exclusionary lingoes...why can't they just say "create sublists based on some criteria" like normal people? - alex23 -- http://mail.pyth

[perl-python] exercise: partition a list by equivalence

2005-02-17 Thread Xah Lee
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes. For