aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-10 03:00:18 +0000
committerGitHub <noreply@github.com>2021-11-10 03:00:18 +0000
commitf12b838e791bbf7fb90d962063a3c93ea0a15056 (patch)
tree4adf310a3fce5838d2b33c16bab0ba0197b558b2
parentb2a6f8fc7d361c8fe0745b8002ac361b751dce8f (diff)
parent8701a49d8f4ffc667bc6b7f273e35caf3e25cf96 (diff)
downloadperlweeklychallenge-club-f12b838e791bbf7fb90d962063a3c93ea0a15056.tar.gz
perlweeklychallenge-club-f12b838e791bbf7fb90d962063a3c93ea0a15056.tar.bz2
perlweeklychallenge-club-f12b838e791bbf7fb90d962063a3c93ea0a15056.zip
Merge pull request #5193 from drbaggy/master
Added some docs and prettified code
-rw-r--r--challenge-138/james-smith/README.md35
-rw-r--r--challenge-138/james-smith/perl/ch-1.pl17
-rw-r--r--challenge-138/james-smith/perl/ch-2.pl10
3 files changed, 32 insertions, 30 deletions
diff --git a/challenge-138/james-smith/README.md b/challenge-138/james-smith/README.md
index 33c1c426f1..fa191ce639 100644
--- a/challenge-138/james-smith/README.md
+++ b/challenge-138/james-smith/README.md
@@ -40,26 +40,25 @@ We can then use a look up table which stores the number of working days (over 26
We break this calculation into 3 functions:
- * `workdays` - this does the look up
- * `ly` - tests for leap year
- * `zf` - uses Zeller's formulae to work out first day of the year {works for the Gregorian calendar}
+ * `work_days` - this does the look up
+ * `leap_year` - tests for leap year
+ * `zellers_congruence_jan_1` - uses Zeller's congruence to work out first day of the year {works for the Gregorian calendar}
All of which
```perl
-my @EXTRA = ( [0,1,1,1,1,1,0], [1,2,2,2,2,1,0] );
+my @EXTRA_WORKDAYS = ( [0,1,1,1,1,1,0], [1,2,2,2,2,1,0] );
-sub workdays {
- 260 + $EXTRA[ ly($_[0]) ][ zf($_[0]) ];
+sub leap_year {
+ $_[0]&3 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
}
-
-sub ly {
- $_[0]%4 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
+sub zellers_congruence_jan_1 {
+ ( 1 + $_[0]%100 + ($_[0]%100>>2) - ($_[0]/100<<1) + ($_[0]/400>>0) ) % 7;
}
-sub zf {
- my $y = $_[0]-1;
- ( 1 + $y%100 + ($y%100>>2) + ($y/400<<0) - ($y/100<<1) )%7;
+sub work_days {
+ 260 + $EXTRA_WORKDAYS[ leap_year $_[0] ][ zellers_congruence_jan_1 $_[0] - 1 ];
}
+
```
# Task 2 - Split Number
@@ -87,15 +86,17 @@ It takes 2 parameters - the sum required add a string of digits. We call the fun
```perl
sub check_square {
- return 0 if $_[0]<=1;
- return split_no( sqrt($_[0]), $_[0] );
+ return $_[0] <= 1 ? 0 : split_no( sqrt($_[0]), $_[0] );
}
sub split_no {
my( $sum, $str ) = @_;
- return $sum ? 0 : 1 if $str eq '';
- return 0 if $sum < 0;
- return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) } 0 .. -1 + length $str;
+ return 0 if $sum < 0;
+ return 0 if $str eq '' && $sum;
+ return 1 if $str eq '';
+ return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) }
+ 0 .. -1 + length $str;
return 0;
}
+
```
diff --git a/challenge-138/james-smith/perl/ch-1.pl b/challenge-138/james-smith/perl/ch-1.pl
index 6f6fdd0979..82d1ad9fe8 100644
--- a/challenge-138/james-smith/perl/ch-1.pl
+++ b/challenge-138/james-smith/perl/ch-1.pl
@@ -8,27 +8,26 @@ use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);
-my @EXTRA = ( [qw(0 1 1 1 1 1 0)], [qw(1 2 2 2 2 1 0)] );
+my @EXTRA_WORKDAYS = ( [qw(0 1 1 1 1 1 0)], [qw(1 2 2 2 2 1 0)] );
my @TESTS = (
[ 2021, 261 ],
[ 2020, 262 ],
);
-is( workdays($_->[0]), $_->[1] ) foreach @TESTS;
+is( work_days($_->[0]), $_->[1] ) foreach @TESTS;
done_testing();
#say $_, ' ', ly($_), ' ', zf($_), ' ', workdays($_) foreach 1900..2100;
-sub workdays {
- 260 + $EXTRA[ ly($_[0]) ][ zf($_[0]) ];
+sub leap_year {
+ $_[0]&3 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
}
-
-sub ly {
- $_[0]%4 || (!($_[0]%100) && $_[0]%400) ? 0 : 1;
+sub zellers_congruence_jan_1 {
+ ( 1 + $_[0]%100 + ($_[0]%100>>2) - ($_[0]/100<<1) + ($_[0]/400>>0) ) % 7;
}
-sub zf {
- ( 1 + (my$y=$_[0]-1)%100 + ($y%100>>2) + ($y/400<<0) - ($y/100<<1) )%7;
+sub work_days {
+ 260 + $EXTRA_WORKDAYS[ leap_year $_[0] ][ zellers_congruence_jan_1 $_[0]-1 ];
}
diff --git a/challenge-138/james-smith/perl/ch-2.pl b/challenge-138/james-smith/perl/ch-2.pl
index e1b04c63e7..44d447b321 100644
--- a/challenge-138/james-smith/perl/ch-2.pl
+++ b/challenge-138/james-smith/perl/ch-2.pl
@@ -19,14 +19,16 @@ is( check_square($_->[0]), $_->[1] ) foreach @TESTS;
done_testing();
sub check_square {
- return split_no( sqrt($_[0]), $_[0], 0 );
+ $_[0] <= 1 ? 0 : split_no( sqrt($_[0]), $_[0], 0 );
}
sub split_no {
my( $sum, $str ) = @_;
- return $sum?0:1 if $str eq '';
- return 0 if $sum<0;
- return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) } 0 .. -1 + length $str;
+ return 0 if $sum < 0;
+ return 0 if $str eq '' && $sum;
+ return 1 if $str eq '';
+ return 1 if grep { split_no( ($sum - substr $str,$_) , substr $str, 0, $_ ) }
+ 0 .. -1 + length $str;
return 0;
}