aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-21 17:29:49 +0000
committerGitHub <noreply@github.com>2021-02-21 17:29:49 +0000
commit9fa7615048cc3b932baa724a26e5a6d8f51cdc8f (patch)
tree6accad5f96c6f8f3414e448b1156d9acca2e48a3
parentb5fdf5f1bc289f18bbd305f20b11ff27308c2a97 (diff)
parent48ccb1259b08423067789483ab4a7be1c851b3f4 (diff)
downloadperlweeklychallenge-club-9fa7615048cc3b932baa724a26e5a6d8f51cdc8f.tar.gz
perlweeklychallenge-club-9fa7615048cc3b932baa724a26e5a6d8f51cdc8f.tar.bz2
perlweeklychallenge-club-9fa7615048cc3b932baa724a26e5a6d8f51cdc8f.zip
Merge pull request #3590 from ccntrq/challenge-100
Challenge 100
-rwxr-xr-xchallenge-100/alexander-pankoff/perl/ch-1.pl95
-rwxr-xr-xchallenge-100/alexander-pankoff/perl/ch-2.pl40
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-100/alexander-pankoff/perl/ch-1.pl b/challenge-100/alexander-pankoff/perl/ch-1.pl
new file mode 100755
index 0000000000..a47274f2d6
--- /dev/null
+++ b/challenge-100/alexander-pankoff/perl/ch-1.pl
@@ -0,0 +1,95 @@
+#!/usr/bin/env perl
+use v5.20;
+use utf8;
+use strict;
+use warnings;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+
+## As a oneliner:
+## perl -e 'join(" ",@ARGV)=~m/^\s*(\d\d?)\:(\d\d?)\s*([ap]m)?\s*$/&&printf("%02d\:%02d%s\n",$3?($3eq"pm"?$1==12?12:$1+12:$1==12?0:$1,$2,""):$1>=12?($1==12?12:$1-12,$2,"pm"):($1==0?12:$1,$2,"am"))' TIME
+{
+ if ( $ENV{TESTING} ) {
+ test_convert_time();
+ }
+ else {
+ my ($T) = join( ' ', @ARGV );
+ die "usage: $0 TIMESTRING\n" unless length $T;
+
+ say convert_time($T);
+ }
+}
+
+sub test_convert_time() {
+
+ my @test_cases = (
+ [ "05:15 pm", "17:15" ],
+ [ "05:15pm", "17:15" ],
+ [ "19:15", "07:15pm" ],
+ [ "19:15", "07:15pm" ],
+ [ "9:15", "09:15am" ],
+ [ "9:15am", "09:15" ],
+ [ "9:1am", "09:01" ],
+ [ "12:00am", "00:00" ],
+ [ "00:00", "12:00am" ],
+ [ "12:00", "12:00pm" ],
+ [ "00:30", "12:30am" ],
+ [ "12:00am", "00:00" ],
+ [ "12:30am", "00:30" ],
+ );
+
+ require Test::More;
+ Test::More->import( tests => 2 * scalar @test_cases );
+
+ for my $test (@test_cases) {
+ my ( $time, $converted ) = @{$test};
+ ok( convert_time($time) eq $converted,
+ "$time converted to $converted" );
+ ok(
+ convert_time_oneliner($time) eq $converted,
+ "$time converted to $converted with oneliner"
+ );
+ }
+
+}
+
+sub convert_time_oneliner($time) {
+ my $oneliner =
+'join(" ",@ARGV)=~m/^\s*(\d\d?)\:(\d\d?)\s*([ap]m)?\s*$/&&printf("%02d\:%02d%s\n",$3?($3eq"pm"?$1==12?12:$1+12:$1==12?0:$1,$2,""):$1>=12?($1==12?12:$1-12,$2,"pm"):($1==0?12:$1,$2,"am"))';
+ my $out = `perl -e '$oneliner' $time`;
+ chomp($out);
+ return $out;
+}
+
+sub convert_time($time) {
+ if (
+ $time =~ m/
+ ^ # anchor to the beginning of the string
+ \s* # allow leading whitespace
+ (\d\d?) # the hour part
+ \: # match the colon
+ (\d\d?) # the minute part
+ \s* # optional whitespace
+ (am|pm)? # optional am or pm
+ \s* # allow trailing ws
+ $ # anchor to the end of the string
+ /x
+ )
+ {
+ sprintf(
+ "%02d\:%02d%s",
+ $3 ? (
+ $3 eq 'pm' #
+ ? $1 == 12
+ ? 12
+ : $1 + 12
+ : $1 == 12 ? 0
+ : $1,
+ $2, ''
+ )
+ : $1 >= 12 #
+ ? ( $1 == 12 ? 12 : $1 - 12, $2, 'pm' )
+ : ( $1 == 0 ? 12 : $1, $2, 'am' )
+ );
+ }
+}
diff --git a/challenge-100/alexander-pankoff/perl/ch-2.pl b/challenge-100/alexander-pankoff/perl/ch-2.pl
new file mode 100755
index 0000000000..4027ecf9a8
--- /dev/null
+++ b/challenge-100/alexander-pankoff/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+use v5.20;
+use utf8;
+use strict;
+use warnings;
+use feature qw(say signatures);
+no warnings 'experimental::signatures';
+
+use List::Util qw(min);
+
+{
+ test_triangle_sum();
+}
+
+sub test_triangle_sum() {
+
+ my @test_cases = (
+ [ [ [1], [ 2, 4 ], [ 6, 4, 9 ], [ 5, 1, 7, 2 ] ], 8 ],
+ [ [ [3], [ 3, 1 ], [ 5, 2, 3 ], [ 4, 3, 1, 3 ] ], 7 ]
+ );
+
+ require Test::More;
+ Test::More->import( tests => scalar @test_cases );
+
+ for my $test (@test_cases) {
+ my ( $triangle, $expected_sum ) = @{$test};
+ ok(
+ triangle_sum($triangle) == $expected_sum,
+ "The minimum path is $expected_sum"
+ );
+ }
+}
+
+sub triangle_sum ( $triangle, $pos = 0 ) {
+ my ( $cur, @rest ) = @$triangle;
+ return 0 if !$cur;
+
+ return $cur->[$pos] +
+ min( triangle_sum( \@rest, $pos ), triangle_sum( \@rest, $pos + 1 ), );
+}