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
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
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
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]))
©
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()
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
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]
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)
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
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
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
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
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
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
>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
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
16 matches
Mail list logo