diff options
| author | Alexander Pankoff <ccntrq@screenri.de> | 2021-02-15 14:05:10 +0100 |
|---|---|---|
| committer | Alexander Pankoff <ccntrq@screenri.de> | 2021-02-21 18:19:25 +0100 |
| commit | ae7b9896c7a0068a7c6dabade73b5407274a7feb (patch) | |
| tree | 534283e589269527dd1710e4da81c634b357a6eb /challenge-100 | |
| parent | 5af038b7346d69072a47529c1330768146e2c15e (diff) | |
| download | perlweeklychallenge-club-ae7b9896c7a0068a7c6dabade73b5407274a7feb.tar.gz perlweeklychallenge-club-ae7b9896c7a0068a7c6dabade73b5407274a7feb.tar.bz2 perlweeklychallenge-club-ae7b9896c7a0068a7c6dabade73b5407274a7feb.zip | |
add perl solution for wk-100 ch-1
Diffstat (limited to 'challenge-100')
| -rwxr-xr-x | challenge-100/alexander-pankoff/perl/ch-1.pl | 95 |
1 files changed, 95 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' ) + ); + } +} |
