Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: results from pairs combination

by Animator (Hermit)
on Oct 01, 2008 at 18:00 UTC ( [id://714857]=note: print w/replies, xml ) Need Help??


in reply to results from pairs combination

Given that it seemed like an intresting problem I opted for re-inventing the wheel and not using Graph. See the readmore for the code.

Do note that the best solution is to use Graph since it went trough more testing and might be optimized. The code in the readmore is only provided as a learning-exercice on how to do it yourself.

#!/usr/bin/perl -l use strict; use warnings; no warnings qw/uninitialized/; for ( [ "1,7", "2,6", "2,7", "5,4", "6,7" ], [ "1,7", "2,6", "2,7", "3,4", "6,7" ], [ "1,7", "2,6", "2,7", "3,4", "9,8" ], [ "a,b", "c,b", "d,f", "e,b", "f,g" ], ) { my @pairs = @$_; my $count = 0; my %sets; my %sets2; for my $p (@pairs) { my ($l, $r) = split /\s*,\s*/, $p; # print "P = $p, L = $l, R = $r, \$sets{\$l} = $sets{$l}, \$sets{\$ +r} = $sets{$r}"; if ($sets{$l} and $sets{$r} and $sets{$l} != $sets{$r}) { # $sets{$l} and $sets{$r} can now be linked my $old_set = $sets{$r}; for my $s (keys %sets) { if ($sets{$s} == $old_set) { if (exists $sets2{ $sets{$s} }) { push @{ $sets2{ $sets{$l} } }, @{ $sets2{ $sets{$s} } }; delete $sets2{ $old_set }; } $sets{$s} = $sets{$l}; } } } elsif ($sets{$l}) { $sets{$r} = $sets{$l}; } elsif ($sets{$r}) { $sets{$l} = $sets{$r}; } else { $count++; $sets{$l} = $sets{$r} = $count; } push @{ $sets2{ $sets{$l} } }, $p; } print "SETS FOR: " . join(", ", @pairs); my $i = 0; for my $key (keys %sets2) { print "\tSET $i: " . join(", ", @{ $sets2{ $key} }); $i++; } print ""; } __END__ Output: SETS FOR: 1,7, 2,6, 2,7, 5,4, 6,7 SET 0: 5,4 SET 1: 2,6, 1,7, 2,7, 6,7 SETS FOR: 1,7, 2,6, 2,7, 3,4, 6,7 SET 0: 3,4 SET 1: 2,6, 1,7, 2,7, 6,7 SETS FOR: 1,7, 2,6, 2,7, 3,4, 9,8 SET 0: 9,8 SET 1: 3,4 SET 2: 2,6, 1,7, 2,7 SETS FOR: a,b, c,b, d,f, e,b, f,g SET 0: a,b, c,b, e,b SET 1: d,f, f,g

Replies are listed 'Best First'.
Re^2: results from pairs combination
by lipong (Initiate) on Oct 02, 2008 at 12:45 UTC
    Hi Thank you very much for the solution. I have try it and it works very well could I just ask you if could please comment the code a bit more so that I can understand each step. Thank you very much. Luis

      I suggest you try documenting it yourself.

      Some advice on how/where to start:

      • Come up with an alogirthm to be used: take a piece of paper and try to solve the problem yourself. Go over each pair and make a note on how you decide in which group to put that pair;
      • Post your algorithm here (in English);
      • Try to turn your algorithm into code;
      • Test the code;
      • Post the code;
      • Look back at the code I posted and see if you can understand what I wrote and why I wrote it. If question ask.;

      I suggest this for the following reasons:

      • It should be more rewarding;
      • You will learn more;
      • You will remember what you learned a lot longer;

      The obvious downside is that it will take your more time to figure it out. If I document it/explain it then you know how to solve this particular problem but you might not be able to solve another, similar, problem since you might not see the alogirthm and/or forgot on how this problem was solved.

        I suggest this for the following reasons:
        Don't forget :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://714857]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found