aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-100/alexander-karelas/perl/ch-1.pl51
-rwxr-xr-xchallenge-100/alexander-karelas/perl/ch-2.pl30
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