diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-15 19:27:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-15 19:27:49 +0000 |
| commit | 18754ea0fb750141f7612e701276f81d38dc1333 (patch) | |
| tree | be241bba7754224e9db0158712f9395c58a2444e | |
| parent | 5cf4b45a8f643679475e453864b706bc6645f100 (diff) | |
| parent | 10429651368a903a5265cefb7438a1086702eff4 (diff) | |
| download | perlweeklychallenge-club-18754ea0fb750141f7612e701276f81d38dc1333.tar.gz perlweeklychallenge-club-18754ea0fb750141f7612e701276f81d38dc1333.tar.bz2 perlweeklychallenge-club-18754ea0fb750141f7612e701276f81d38dc1333.zip | |
Merge pull request #3532 from akarelas/pr-akarelas-100
tasks 1 and 2
| -rwxr-xr-x | challenge-100/alexander-karelas/perl/ch-1.pl | 51 | ||||
| -rwxr-xr-x | challenge-100/alexander-karelas/perl/ch-2.pl | 30 |
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-100/alexander-karelas/perl/ch-1.pl b/challenge-100/alexander-karelas/perl/ch-1.pl new file mode 100755 index 0000000000..58ea39a157 --- /dev/null +++ b/challenge-100/alexander-karelas/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +use v5.10; +use warnings; + +use Test::More; + +sub xform { + my ($input) = @_; + + $input =~ /^(\d{2})\:(\d{2})\s*(am|pm)?\z/ or die "Usage: $0 05:15 pm or 05:15pm or 17:15"; + + my ($h, $m, $ap) = ($1, $2, $3); + + if (defined $ap) { + if ($h == 12) { + $h = 0 if $ap eq 'am'; + } else { + $h += 12 if $ap eq 'pm'; + } + $ap = ''; + } else { + if ($h == 12) { + $ap = 'pm'; + } elsif ($h > 12) { + $h -= 12; + $ap = 'pm'; + } elsif ($h == 0) { + $h = 12; + $ap = 'am'; + } else { + $ap = 'am'; + } + } + + return sprintf("%02d\:%02d%s", $h, $m, $ap); +} + +is xform('05:15 pm'), '17:15', '05:15 pm'; +is xform('05:15pm'), '17:15', '05:15pm'; +is xform('19:15'), '07:15pm', '19:15'; + +is xform('00:34'), '12:34am', '00:34'; +is xform('12:34am'), '00:34', '12:34am'; +is xform('12:34'), '12:34pm', '12:34'; +is xform('12:34pm'), '12:34', '12:34pm'; + +is xform('05:15am'), '05:15', '05:15am'; +is xform('05:15'), '05:15am', '05:15'; + +done_testing;
\ No newline at end of file diff --git a/challenge-100/alexander-karelas/perl/ch-2.pl b/challenge-100/alexander-karelas/perl/ch-2.pl new file mode 100755 index 0000000000..e2a357064b --- /dev/null +++ b/challenge-100/alexander-karelas/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use v5.10; +use warnings; + +use List::Util 'min'; + +use Test::More; + +sub minimum_path_sum { + my ($tree, $x, $y) = @_; + $x //= 0; + $y //= 0; + + if ($y == $#$tree) { + return $tree->[$y][$x]; + } else { + my $sum1 = minimum_path_sum($tree, $x, $y + 1); + my $sum2 = minimum_path_sum($tree, $x + 1, $y + 1); + return $tree->[$y][$x] + min($sum1, $sum2); + } +} + +my $tree1 = [ [1], [2, 4], [6, 4, 9], [5, 1, 7, 2] ]; +is minimum_path_sum($tree1), 8, 'example 1'; + +my $tree2 = [ [3], [3, 1], [5, 2, 3], [4, 3, 1, 3] ]; +is minimum_path_sum($tree2), 7, 'example 2'; + +done_testing;
\ No newline at end of file |
