in reply to Best Pairs

My take on a solution:

use strict; use warnings; my $anchor = 1; # This is the basic element of the pairs we are lookin +g for my %results; # The final results will be found in this hash foreach (<DATA>) { my %hash; foreach (split) { # Put the array into a hash $hash{$_}=1; } if ($hash{$anchor}) { # Only process the hash if the anchor is pre +sent foreach (keys %hash) { # increment the frequencies in the resu +lts-hash ++$results{$_} unless $_==$anchor; # but omit the anchor o +r the sort will not work! } } } for my $x(sort {return $results{$b} <=> $results{$a}} keys %results) { print "$x = $results{$x}\n"; } __DATA__ 2 4 5 7 8 10 1 2 5 6 7 9 2 6 7 8 9 10 1 3 5 10 1 3 4 5 6 8 9 1 2 4 6 1 2 4 5 7 10 1 3 4 6 7 8 9

This solution only reads in each array once one at a time (low memory use and linear time for processing) and only processes the arrays where the anchor element of the pairs we are looking for is present (should be reasonably fast: on my system it processes 10000 arrays with max. 30 elements each in less than 2 seconds).

It outputs a sorted list of the number of times each element is found together with the array-element in the same array. Getting the top K-elements out of the %results-hash is left as an exercise for the reader.

CountZero

"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Replies are listed 'Best First'.
Re: Re: Best Pairs
by Not_a_Number (Prior) on Nov 09, 2003 at 23:00 UTC

    Here's another version. I was about to post it, but then I saw CountZero's solution above, which uses pretty much the same procedure. However, as mine looks more concise, I thought, why not? FWIW:

    my $elem = 1; my $k = 3; my %out; while ( <DATA> ) { next unless /\b$elem\b/; $out{$_}++ for split; } delete $out{$elem}; print "$_ ($out{$_} times)\n" for (sort { $out{$b} <=> $out{$a} } keys %out)[ 0 .. ( $k - 1 ) ];

    dave

      TIMTOWTDI

      ++ for Not a Number

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law