Dear Monks

Here is my problem,

  1. A List is a list of given numbers.
  2. A Pair is a set of 2 numbers extracted from the List.
  3. A Row is a set of 'P' such pairs such that each number in the row is unique.
  4. P is a fixed number : Let's take it as 2.
  5. Goal: is to maximize the the number of rows.
I am able to extract the pairs and need suggestion in improving that and in reaching the goal.

Here is my code.

use 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
Thanks
artist.

In reply to Pairing the pairs by artist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.