gpt4 book ai didi

algorithm - 寻找一种将宽度分配给列的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:02:25 25 4
gpt4 key购买 nike

我想将数据库表打印到 STDOUT。如果表格宽度大于屏幕宽度,我想以相同的百分比剪切每个列(除非表格宽度已达到 min_width),直到表格适合屏幕。我试图用发布的子程序解决这个问题。有人知道解决这个问题的更短更优雅的算法吗?

sub cal_size {
my ( $maxcols, $ref ) = @_;
# $maxcols => screen width
# $ref => ref to an AoA; holds the table
my ( $max ) = cal_tab( $ref );
# $max => ref to an array; holds the length of the longest string of each column
# $tab = 2;
if ( $max and @$max ) {
my $sum = sum( @$max ) + $tab * @$max;
$sum -= $tab;
my @max_tmp = @$max;
my $percent = 0;
while ( $sum > $maxcols ) {
$percent += 0.5;
if ( $percent > 99 ) {
return;
}
my $count = 0;

for my $i ( 0 .. $#max_tmp ) {
# $min_width => columns width should not be less than $min_width if possible
next if $min_width >= $max_tmp[$i];
# no need to cut if the column width id less than min_width
next if $min_width >= minus_x_percent( $max_tmp[$i], $percent );
# don't cut if column width become less than min_width
$max_tmp[$i] = minus_x_percent( $max_tmp[$i], $percent );
$count++;
last if $sum <= $maxcols;
}
$min_width-- if $count == 0 and $min_width > 1;
# if no cuts but $sum > $maxcols reduce $min_width
$sum = sum( @max_tmp ) + $tab * @max_tmp;
$sum -= $tab;
}
my $rest = $maxcols - $sum;
while ( $rest > 0 ) { # distribute the rest
my $count = 0;
for my $i ( 0 .. $#max_tmp ) {
if ( $max_tmp[$i] < $max->[$i] ) {
$max_tmp[$i]++;
$rest--;
$count++;
last if $rest < 1;
}
}
last if $count == 0;
last if $rest < 1;
}
$max = [ @max_tmp ] if @max_tmp;
}
return $max;
}


sub minus_x_percent {
my ( $value, $percent ) = @_;
return int $value - ( $value * 1/100 * $percent );
}

最佳答案

如果不是字段宽度的下限,这个问题会很简单。一旦字段不能再变小,只有较大的字段才有资格进行缩放,因此计算会有所不同,具体取决于是所有字段、没有字段还是部分字段已缩小到其限制。

缩放比例有几个,每个唯一字段宽度一个。由于字段按比例缩小,其中最小的字段将最先达到最小字段大小限制。之后只有大于最小尺寸的列才能进一步缩小,直到第二小的也达到限制。

这一直持续到所有列都达到其最小大小,之后可用空间在列之间平均分配。

该程序实现了该算法的计算,我认为它可以满足您的要求。

请注意,返回的字段宽度是浮点值,您必须根据需要对其进行四舍五入。

use strict;
use warnings;

use List::Util 'max';

my $min_col_width = 10;
my $tab = 2;

my $widths = recalc_widths(80, [ 10, 15, 20, 25, 30 ]);
print join ' ', map sprintf('%.3f', $_), @$widths;
print "\n";

sub recalc_widths {

my ($target, $widths) = @_;
$target -= (@$widths - 1) * $tab;

my @sorted_widths = sort { $a <=> $b } @$widths;

my $num_limited = 0;
my $adjustable_total_width = 0;
$adjustable_total_width += $_ for @sorted_widths;

while (@sorted_widths) {

my $boundary = $sorted_widths[0];

my $scale = ($target - $num_limited * $min_col_width) / $adjustable_total_width;

if ($boundary * $scale >= $min_col_width) {
return [ map max($_ * $scale, $min_col_width), @$widths ];
}

while (@sorted_widths and $sorted_widths[0] == $boundary) {
shift @sorted_widths;
$adjustable_total_width -= $boundary;
$num_limited++;
}
}

return [ ($target / $num_limited) x $num_limited ];
}

输出

10.000  10.333  13.778  17.222  20.667

关于algorithm - 寻找一种将宽度分配给列的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11567388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com