in reply to Best Pairs
I think thatuse warnings; use strict; my %combinations; #hash which holds combination pairs print "Preprocessing data...\n"; while (<>) { chomp; my @line=split; for my $number (@line) { for (@line) { if ($number != $_) { #Disallow combos like (1,1) $combinations{$number}{$_}++; #I saw combo ($number, $_) } } } } for my $number (keys %combinations) { $combinations{$number}=[sort {$combinations{$number}{$b} <=> $combin +ations{$number}{$a}} (keys %{$combinations{$number}})]; #replace our + anonymous hash with an anonymous array containing the combo pairs in + a greatest-to-least order. } print "Hash Initialized. Enter: <number> <quantity> (q to quit)\n"; while (<STDIN>) { chomp; last if /q/; my ($number, $quantity) = split; print "\n"; print "($number, $_)\n" for (@{$combinations{$number}}[0..$quantity-1] +); }
could be speeded up. I leave that as an exercise for the reader.... :)$combinations{$number}=[sort {$combinations{$number}{$b} <=> $combinat +ions{$number}{$a}} (keys %{$combinations{$number}})];
|
|---|