diff options
| author | E. Choroba <choroba@matfyz.cz> | 2021-02-19 22:29:06 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2021-02-19 22:38:00 +0100 |
| commit | ae0e5e83519db37baed52311035a4a006a46fc75 (patch) | |
| tree | 687760e3b79160043c4c8468d402b8dc184c9891 | |
| parent | 1694a5dfa19928589dfa70c3dfbb9126854ada8d (diff) | |
| download | perlweeklychallenge-club-ae0e5e83519db37baed52311035a4a006a46fc75.tar.gz perlweeklychallenge-club-ae0e5e83519db37baed52311035a4a006a46fc75.tar.bz2 perlweeklychallenge-club-ae0e5e83519db37baed52311035a4a006a46fc75.zip | |
Add solutions to 100: Fun Time & Triangle Sum by E. Choroba
| -rwxr-xr-x | challenge-100/e-choroba/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-100/e-choroba/perl/ch-2.pl | 54 |
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-100/e-choroba/perl/ch-1.pl b/challenge-100/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..cd5701d3d8 --- /dev/null +++ b/challenge-100/e-choroba/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use warnings; +use strict; + +# Following The American Heritage Dictionary of the English Language +# and United States Government Printing Office: midnight is 12:00 am. + +sub fun_time { + my ($h, $m, $p) = $_[0] =~ /([0-9]+):([0-9]+)\s*([ap]m)?/i; + if ($p) { + return sprintf '%02d:%02d', + $h % 12 + 12 * ($p eq 'pm'), + $m + } else { + return sprintf '%02d:%02d %sm', + $h % 12 || 12, + $m, + $h >= 12 ? 'p' : 'a' + } +} + +# As a oneliner (does it make any sense?): +# perl -e '($h,$m,$p)=shift=~/(\d+):(\d+)\s*([ap]m)?/i;printf"%02d:%02d"." %sm"x!$p.$/,$p?($h%12+12*(pm eq$p)):($h%12||12),$m,$h>=12?p:a' '12:01 am' + +use Test::More tests => 15; + +is fun_time('11:10'), '11:10 am', 'plain morning'; +is fun_time('06:10'), '06:10 am', 'morning with a leading zero'; +is fun_time('20:44'), '08:44 pm', 'afternoon'; +is fun_time('00:00'), '12:00 am', 'midnight'; +is fun_time('12:00'), '12:00 pm', 'noon'; +is fun_time('00:01'), '12:01 am', 'early morning'; +is fun_time('12:01'), '12:01 pm', 'early afternoon'; + +is fun_time('11:10 am'), '11:10', 'plain am'; +is fun_time('06:10 am'), '06:10', 'leading zero am'; +is fun_time('08:44 pm'), '20:44', 'pm'; +is fun_time('12:00 am'), '00:00', 'am midnight'; +is fun_time('12:00 pm'), '12:00', 'pm noon'; +is fun_time('12:01 am'), '00:01', 'am early morning'; +is fun_time('12:01 pm'), '12:01', 'pm early afternoon'; +is fun_time('10:59am'), '10:59', 'no space'; diff --git a/challenge-100/e-choroba/perl/ch-2.pl b/challenge-100/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..f7bc1d22d4 --- /dev/null +++ b/challenge-100/e-choroba/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +sub triangle_sum { + my ($triangle) = @_; + my @sums = @{ $triangle->[-1] }; + @sums = map { + $sums[$sums[ $_ - 1 ] < $sums[$_] ? $_ - 1 : $_] + + $triangle->[@sums - 2][ $_ - 1 ] + } 1 .. $#sums + while @sums > 1; + return $sums[0] +} + +sub triangle_sum_show { + my ($triangle) = @_; + my @sums = @{ $triangle->[-1] }; + my @way; + while (@sums > 1) { + unshift @way, []; + @sums = map { + my $i = $sums[ $_ - 1 ] < $sums[$_] ? $_ - 1 : $_; + push @{ $way[0] }, $i; + $sums[$i] + $triangle->[@sums - 2][ $_ - 1 ]; + } 1 .. $#sums; + } + unshift @way, []; + my $previous; + for my $row (@$triangle) { + my @selected = @{ $way[$#$row] }; + $previous = @selected ? $selected[$previous] : 0; + print ' ' x (@$triangle - @$row); + for my $i (0 .. $#$row) { + print ' ', $i == $previous ? "[$row->[$i]]" : $row->[$i]; + } + print "\n"; + } +} + +use Test::More; + +is triangle_sum([[1], [2, 4], [6, 4, 9], [5, 1, 7, 2]]), 8, 'Example 1'; +is triangle_sum([[3], [3, 1], [5, 2, 3], [4, 3, 1, 3]]), 7, 'Example 2'; + +is triangle_sum([[2], [1, 5], [9, 10, 1]]), 8, 'Tricky'; + +done_testing(); + +my $triangle = [ map [map int rand 10, 1 .. $_], 1 .. 20 ]; +triangle_sum_show($triangle); +say triangle_sum($triangle); + |
