aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-07-30 01:25:34 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-07-30 01:25:34 +0100
commit066ad6ac3eea1f0d02b5098ca921addade4b784f (patch)
tree52409a6b8b488034c1ff42dcfdabd122ced19116
parent40550e1e6ea41dabe7a6a72e17f5f56de85fabcd (diff)
downloadperlweeklychallenge-club-066ad6ac3eea1f0d02b5098ca921addade4b784f.tar.gz
perlweeklychallenge-club-066ad6ac3eea1f0d02b5098ca921addade4b784f.tar.bz2
perlweeklychallenge-club-066ad6ac3eea1f0d02b5098ca921addade4b784f.zip
tweaks and shortenings
-rw-r--r--challenge-175/james-smith/README.md6
-rw-r--r--challenge-175/james-smith/perl/ch-1.pl8
2 files changed, 9 insertions, 5 deletions
diff --git a/challenge-175/james-smith/README.md b/challenge-175/james-smith/README.md
index 89f94bc409..56bf461373 100644
--- a/challenge-175/james-smith/README.md
+++ b/challenge-175/james-smith/README.md
@@ -31,7 +31,7 @@ sub last_day_of_months {
## Compute the last Sunday in december of the previous year
my $last = 31 - ( int($yr%100/4) - $ly + 6 - 2*$yr/100%4 + $yr%100 ) % 7;
## Finally work out the last days of the following 2 months.
- map { ( ($last += 35 - $L[$_-1]) > $L[$_] ) && ($last-=7);
+ map { $last-=7 if $L[$_] < ($last += 35 - $L[$_-1]);
sprintf '%04d-%02d-%02d', $yr, $_, $last
} 1..12
}
@@ -95,7 +95,7 @@ We can rewrite this as a 1-liner using `List::Util`s product function.
Now there is a second nasty trick here - which if you are used to javascript *self-executing closure*. The trick allows us here to
use the result of the totient calculation and use it to calculate the `t(n) + S( t(n) )` calculation without a *temporary* variable.
-The code reduces to, watch out for the two difference `$_[]` and the `$_->[]`...:
+The code reduces to this, watch out for the two difference `$_[]` and the `$_->[]`...:
```perl
sub st {
@@ -107,3 +107,5 @@ sub st {
);
}
```
+
+The longer form is about 10-15% faster - due to the map and use of external method product...
diff --git a/challenge-175/james-smith/perl/ch-1.pl b/challenge-175/james-smith/perl/ch-1.pl
index 3355c93461..b4ec450c98 100644
--- a/challenge-175/james-smith/perl/ch-1.pl
+++ b/challenge-175/james-smith/perl/ch-1.pl
@@ -30,16 +30,18 @@ sub last_day_of_months {
## Compute the last Sunday in december of the previous year
my $last = 31 - ( int($yr%100/4) - $ly + 6 - 2*$yr/100%4 + $yr%100 ) % 7;
## Finally work out the last days of the following 2 months.
- map { ( ($last += 35 - $L[$_-1]) > $L[$_] ) && ($last-=7);
+ map { $last-=7 if $L[$_] < ($last += 35 - $L[$_-1]);
sprintf '%04d-%02d-%02d', $yr, $_, $last
} 1..12
}
-#123456789#123456789#123456789#123456789#123456789#123456789#123456789#123456789
+# 1 2 3 4 5
+#23456789#123456789#123456789#123456789#123456789#1
sub l {
my$y=pop;
$L[2]=(my$f=!($y%400)^!($y%100)^!($y%4))?29:28;
my$l=31-(int($y%100/4)-$f+6-2*$y/100%4+$y%100)%7;
- map{(($l+=35-$L[$_-1])>$L[$_])&&($l-=7);sprintf'%04d-%02d-%02d',$y,$_,$l}1..12
+ map{$l-=7if$L[$_]<($l+=35-$L[$_-1]);
+ sprintf'%04d-%02d-%02d',$y,$_,$l}1..12
}