diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-16 17:35:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-16 17:35:18 +0000 |
| commit | c02c0c436ebaf42c058d2080d118eb144880733e (patch) | |
| tree | f6836cc9c4c496bc5cfab66afd8984ee64bac66a /challenge-100 | |
| parent | 4a7af6dedae7637e6663137ca3ce26e565b6b838 (diff) | |
| parent | 651f7e3f4edebae34347dad3de22a888a119b87f (diff) | |
| download | perlweeklychallenge-club-c02c0c436ebaf42c058d2080d118eb144880733e.tar.gz perlweeklychallenge-club-c02c0c436ebaf42c058d2080d118eb144880733e.tar.bz2 perlweeklychallenge-club-c02c0c436ebaf42c058d2080d118eb144880733e.zip | |
Merge pull request #3542 from drbaggy/master
Save a few bytes by trimming the subroutine name, and using pop rather than shift! and removing extraneous ;
Diffstat (limited to 'challenge-100')
| -rw-r--r-- | challenge-100/james-smith/perl/ch-1.pl | 76 |
1 files changed, 46 insertions, 30 deletions
diff --git a/challenge-100/james-smith/perl/ch-1.pl b/challenge-100/james-smith/perl/ch-1.pl index e241822661..57352d1f30 100644 --- a/challenge-100/james-smith/perl/ch-1.pl +++ b/challenge-100/james-smith/perl/ch-1.pl @@ -6,6 +6,18 @@ use warnings; use feature qw(say); use Test::More; +is( ft( '05:15pm' ), '17:15' ); +is( ft( '05:15 pm' ), '17:15' ); +is( ft( '17:15' ), '05:15pm' ); +is( ft( '05:15am' ), '05:15' ); +is( ft( '05:15 am' ), '05:15' ); +is( ft( '05:15' ), '05:15am' ); +is( ft( '00:00' ), '12:00am' ); +is( ft( '12:00' ), '12:00pm' ); +is( ft( '24:00' ), '12:00pm' ); +is( ft( '12:00 am' ), '00:00' ); +is( ft( '12:00 pm' ), '12:00' ); + is( fun_time( '05:15pm' ), '17:15' ); is( fun_time( '05:15 pm' ), '17:15' ); is( fun_time( '17:15' ), '05:15pm' ); @@ -18,52 +30,56 @@ is( fun_time( '24:00' ), '12:00pm' ); is( fun_time( '12:00 am' ), '00:00' ); is( fun_time( '12:00 pm' ), '12:00' ); -is( fun_time_readable( '05:15pm' ), '17:15' ); -is( fun_time_readable( '05:15 pm' ), '17:15' ); -is( fun_time_readable( '17:15' ), '05:15pm' ); -is( fun_time_readable( '05:15am' ), '05:15' ); -is( fun_time_readable( '05:15 am' ), '05:15' ); -is( fun_time_readable( '05:15' ), '05:15am' ); -is( fun_time_readable( '00:00' ), '12:00am' ); -is( fun_time_readable( '12:00' ), '12:00pm' ); -is( fun_time_readable( '24:00' ), '12:00pm' ); -is( fun_time_readable( '12:00 am' ), '00:00' ); -is( fun_time_readable( '12:00 pm' ), '12:00' ); - done_testing(); ## 72 chars ############################################################ -## Just split so slightly more readable and fits on 72 char lines -sub fun_time { $_[0]=~s{(\d+):(\d+)\s*(\wm|)}{sprintf'%02d:%02d%s', - ($1%12||(12*!$3))+12*('pm'eq$3),$2,$3?'':$1>11?'pm':'am'}re; } +## Just split so slightly more readable and fits on 60 char lines +## Not nasty hack - we pop rather than shift as it saves 2 bytes! +## We assume that the minute portion will be two digits after the +## ":" then we don't need to check it.... + +## 110 bytes total - 102 inside the curly braces.. + +sub ft{pop=~s/(.+)(:..)\s*(.m|)/sprintf'%02d%s%s', +($1%12||(12*!$3))+12*('pm'eq$3),$2,$3?'':$1<12?'am':'pm'/re} ## This is more readable version with notes... -sub fun_time_readable { - return $_[0] =~ - s{ - ## Split into 3 parts, $1 - hours, $2 - minutes & $3 - am/pm - (\d+) : (\d+) \s* ( \wm | ) +sub fun_time { + return pop =~ + ## Note the nasty hack we pop rather than shift - that saves 2 bytes + ## in our golfdom.... + s/ + ## Split into 3 parts, $1 - hours, $2 - minutes & $3 - am-pm + (.+) (:..) \s* ( .m | ) ## We assume all strings are valid - so we don't have to anchor ## at both ends or worry what the 12hr clock sufficies are - ## am/pm & \wm is shorter than [ap]m + ## am-pm and .m is shorter than [ap]m + ## + ## We assume that the time will always have a : followed + ## 2 digits... ## - ## Note if we right (\wm)? the 3rd capture variable $3 is - ## sometimes undefined - better is to use ([ap]m|) which + ## Note if we right (.m)? the 3rd capture variable $3 is + ## sometimes undefined - better is to use (.m|) which ## matches the same way but $3 is now an empty string not ## undefined when we have a 24 hour clock time - } - { - sprintf '%02d:%02d%s', - ( $1%12 || (12*!$3) ) + 12*('pm'eq$3), + / + sprintf '%02d%s%s', + ( $1 % 12 || ( 12 * ! $3 ) ) + 12 * ( 'pm' eq $3 ), ## Get hour modulo 12.. ## From 24hr to 12hr clock we need to convert 00:?? to 12:?? ## From 12hr to 24hr clock it is pm we then need to add 12... + ## Note we use the "yoda condition" for the equals + ## 'pm'eq$3 + ## as this is a byte shorter than the more usual way of + ## writing inequalitys + ## $3 eq'pm' + ## as we don't need a space between the $3 & the eq... $2, ## The minutes is the easy bit just copy.. - $3 ? '' : $1>11 ? 'pm' : 'am' + $3 ? '' : $1 < 12 ? 'am' : 'pm' ## If we are converting from 12hr clock the third string is - ## empty - if not and the time is <12 we return am o/w pm - }rex; + ## empty - if not and the time is <12 we return am otherwise pm + /rex; } |
