diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-08-29 00:24:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-08-29 00:24:20 +0100 |
| commit | 5cbca7eb8446f1633b9327022d07457f5270c7fd (patch) | |
| tree | 94235e7f7967ba0384097f510ec289bfb9a0a8de | |
| parent | 7e8952678ef63a61aa43c1bf53e20ec61c770a6b (diff) | |
| parent | 6e8b0f83069a80f5cc9f3e36983e139cd125fad2 (diff) | |
| download | perlweeklychallenge-club-5cbca7eb8446f1633b9327022d07457f5270c7fd.tar.gz perlweeklychallenge-club-5cbca7eb8446f1633b9327022d07457f5270c7fd.tar.bz2 perlweeklychallenge-club-5cbca7eb8446f1633b9327022d07457f5270c7fd.zip | |
Merge pull request #2165 from LubosKolouch/master
Fix Perl - printing of histogram if the number is more than 1 digit
| -rw-r--r-- | challenge-075/lubos-kolouch/perl/ch-2.pl | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/challenge-075/lubos-kolouch/perl/ch-2.pl b/challenge-075/lubos-kolouch/perl/ch-2.pl index bbf03742ba..01717a96c6 100644 --- a/challenge-075/lubos-kolouch/perl/ch-2.pl +++ b/challenge-075/lubos-kolouch/perl/ch-2.pl @@ -12,24 +12,28 @@ sub printHistogram { my $hist_max = max(@$histogram); my $out_str; + my $max_len = length($hist_max) + 1; + my $total_len = 0; + for my $i (reverse 1..$hist_max) { - $out_str = $i; + $out_str = sprintf "%${max_len}s", $i; + $total_len += length($i); for my $bar (@$histogram) { - $out_str .= $bar >= $i? '#' : ' ' + $out_str .= $bar >= $i ? sprintf "%${max_len}s", '#' : sprintf "%${max_len}s", ' ' } say $out_str; } - $out_str = '_'; - $out_str .= '_' x scalar @$histogram; + $out_str = '_' x $max_len; + $out_str .= '_' x ($max_len * scalar @$histogram); say $out_str; - $out_str = ' '; - $out_str .= $_ for (@$histogram); + $out_str = ' ' x $max_len; + $out_str .= sprintf "%${max_len}s", $_ for (@$histogram); say $out_str; } |
