aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-100/wanderdoc/perl/ch-1.pl74
-rw-r--r--challenge-100/wanderdoc/perl/ch-2.pl52
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-100/wanderdoc/perl/ch-1.pl b/challenge-100/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..87c9253caf
--- /dev/null
+++ b/challenge-100/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a time (12 hour / 24 hour). Write a script to convert the given time from 12 hour format to 24 hour format and vice versa. Ideally we expect a one-liner.
+Example 1: Input: 05:15 pm or 05:15pm Output: 17:15
+Example 2: Input: 19:15 Output: 07:15 pm or 07:15pm
+=cut
+
+
+
+
+
+
+
+
+
+use Test::More;
+
+sub fun_time
+{
+ my $time_string = $_[0];
+ my $time_re = qr/([0-9]{2}):([0-9]{2}) ?([a|p]m)?/;
+ my ($hour, $min, $format) = $time_string =~ /$time_re/;
+
+ $format //='';
+
+
+ if ( length($format) ) # 'am/pm'
+ {
+ if ( $format eq 'am' )
+ {
+ return $hour == 12 ? "00:${min}" : "${hour}:${min}";
+ }
+ else # pm
+ {
+ $hour = ($hour < 12) ? $hour += 12 : $hour;
+
+
+ return "${hour}:${min}";
+ }
+ }
+
+ else # ISO
+ {
+ if ( $hour eq '00' ) { return "12:${min} am"; }
+ elsif ( $hour == 12 ) {return "${hour}:${min} pm";}
+
+ elsif ($hour > 12 )
+ {
+ $hour = sprintf("%02d", $hour - 12);
+ return "${hour}:${min} pm";
+ }
+ else { return "${hour}:${min} am"; }
+ }
+}
+
+
+# https://en.wikipedia.org/wiki/12-hour_clock
+is(fun_time(q(12:00 am)), q(00:00), q(Midnight No.1));
+is(fun_time(q(00:00)), q(12:00 am), q(Midnight No.2));
+is(fun_time(q(01:00 am)), q(01:00), q(01:00 No.1));
+is(fun_time(q(01:00)), q(01:00 am), q(01:00 No.2));
+is(fun_time(q(11:59 am)), q(11:59), q(11:59 No.1));
+is(fun_time(q(11:59)), q(11:59 am), q(11:59 No.2));
+
+is(fun_time(q(12:00 pm)), q(12:00), q(Midday 1));
+is(fun_time(q(12:00)), q(12:00 pm), q(Midday 2));
+is(fun_time(q(01:00 pm)), q(13:00), q(13:00 No.1));
+is(fun_time(q(13:00)), q(01:00 pm), q(13:00 No.2));
+is(fun_time(q(11:59 pm)), q(23:59), q(23:59 No.1));
+is(fun_time(q(23:59)), q(11:59 pm), q(23:59 No.2));
+done_testing(); \ No newline at end of file
diff --git a/challenge-100/wanderdoc/perl/ch-2.pl b/challenge-100/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..43c05e5a99
--- /dev/null
+++ b/challenge-100/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,52 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given triangle array. Write a script to find the minimum path sum from top to bottom.
+When you are on index i on the current row then you may move to either index i or index i + 1 on the next row.
+Example 1: Input: Triangle = [ [1], [2,4], [6,4,9], [5,1,7,2] ] Output: 8
+Explanation: The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8.
+Example 2: Input: Triangle = [ [3], [3,1], [5,2,3], [4,3,1,3] ] Output: 7
+Explanation: The minimum path sum from top to bottom: 3 + 1 + 2 + 1 = 7.
+=cut
+
+
+
+
+
+
+use List::Util qw(min);
+use Test::More;
+
+sub triangle_path
+{
+ my $aref = $_[0];
+ my $mtr;
+
+ do { my $i = $_;
+ do { $mtr->[$i][$_] = 0 } for 0 .. $#$aref }
+ for 0 .. $#$aref;
+ $mtr->[$#$aref] = $aref->[$#$aref];
+
+ for my $i ( reverse 0 .. $#$aref - 1 )
+ {
+ my $row = $aref->[$i];
+
+ for my $j (0 .. $#$row)
+ {
+ my $left = $mtr->[$i + 1][$j];
+
+
+ my $right = $mtr->[$i + 1][$j + 1];
+ $mtr->[$i][$j] = $row->[$j] + min($left, $right);
+ }
+
+ }
+ return $mtr->[0][0];
+}
+
+
+is(triangle_path([[1], [2,4], [6,4,9], [5,1,7,2]]), 8, 'Example 1');;
+is(triangle_path([[3], [3,1], [5,2,3], [4,3,1,3]]), 7, 'Example 2');;
+done_testing(); \ No newline at end of file