It appears that I have confused a few people here. What I need "minimized" is the aggregate height per column as summed from @height data.
Given the heights of qw/ 10 10 15 20 10 10 /, I could conceivably construct a table with no column having greater than 20 items in it:
10 15 20 10
10 10
The following solution, would fail because one of the columns has 25 items:
10 10 20 10
15 10
Part of the problem, I suspect, is that I had a bug in my original code because I accidentally posted the wrong version. The @distribution array needs to be reset every iteration. The following is correct:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my @height = qw/ 10 15 25 30 10 13 /;
my @columns = ($height[0],0,0,$height[-1]);
my $curr_height = 0;
# set this unreasonably high to ensure that subsequent
# heights will be lower
$curr_height += $_ foreach @height;
my @distribution;
for my $a (0..1) {
$columns[$a] += $height[1];
for my $b (0..2) {
$columns[$b] += $height[2];
for my $c (1..3) {
$columns[$c] += $height[3];
for my $d (2..3) {
$columns[$d] += $height[4];
my $this_height = ( sort @columns )[-1];
my $valid_dist;
foreach ( @columns ) {
$valid_dist = $_;
last if ! $valid_dist;
}
if ( $valid_dist and $this_height < $curr_height ) {
$curr_height = $this_height;
@distribution = ([1],[],[],[6]);
push @{$distribution[$a]}, 2;
push @{$distribution[$b]}, 3;
push @{$distribution[$c]}, 4;
push @{$distribution[$d]}, 5;
}
$columns[$d] -= $height[4];
}
$columns[$c] -= $height[3];
}
$columns[$b] -= $height[2];
}
$columns[$a] -= $height[1];
}
print Dumper \@distribution;
I hope that's clear, now :)
I also have to add that there's a heck of a lot more discussion on this than I thought there would be.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.