artist has asked for the wisdom of the Perl Monks concerning the following question:
Here is my problem,
Here is my code.
Thanksuse strict; use warnings; my $Z; my @array; foreach (1..36){ $Z->{1 + int rand(8)}++; } foreach (sort { $Z->{$a} <=> $Z->{$b}} keys %{$Z}){ my $element = $_; foreach (1..$Z->{$element}){ push @array, $element; } } #my @array = qw(2 8 8 8 1 1 1 1 5 5 5 5 7 7 7 7 4 4 4 4 4 4 3 3 3 3 3 +3 3 6 6 6 6 6 6 6); print join " " => @array,"\n"; sub get_pairs { print "====================\n"; my @array = @_; my $X; my $hash; my $A; my $counter; my @pairs; foreach (@array){ $X->{$_} += 1; } foreach my $x (sort { $a <=> $b } keys %{$X}){ foreach my $y (sort { $a <=> $b } keys %{$X}){ next if $X->{$x} == 0; next if $x eq $y; next if $X->{$y} == 0; next if defined $hash->{$x}{$y}; $counter++; print "$counter = $x $y\n"; push @pairs, [$x,$y]; $hash->{$x}{$y} = $hash->{$y}{$x} = 1; $X->{$x} -= 1; $X->{$y} -= 1; } } print "Counts $counter\n"; } get_pairs(@array); __END__ 1 = 1 2 2 = 1 3 3 = 1 4 4 = 1 5 5 = 3 4 6 = 3 5 7 = 3 6 8 = 3 7 9 = 3 8 10 = 4 5 11 = 4 6 12 = 4 7 13 = 4 8 14 = 5 6 15 = 6 7 16 = 6 8 Counts 16
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Pairing the pairs
by BrowserUk (Patriarch) on Jun 12, 2003 at 22:57 UTC | |
by artist (Parson) on Jun 13, 2003 at 02:36 UTC | |
|
Re: Pairing the pairs
by antirice (Priest) on Jun 12, 2003 at 22:11 UTC | |
by antirice (Priest) on Jun 13, 2003 at 05:00 UTC | |
|
Re: Pairing the pairs
by shemp (Deacon) on Jun 12, 2003 at 21:58 UTC | |
by artist (Parson) on Jun 13, 2003 at 02:48 UTC | |
|
Re: Pairing the pairs
by fglock (Vicar) on Jun 12, 2003 at 22:03 UTC | |
by artist (Parson) on Jun 13, 2003 at 02:49 UTC | |
|
Re: Pairing the pairs
by aquarium (Curate) on Jun 12, 2003 at 22:04 UTC |