diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:14:21 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-04-26 09:14:21 +0100 |
| commit | 46b8aecc9397c6211a1e97a7f0638833726294a2 (patch) | |
| tree | 9c6f842c6780796607f4a39491906124fe5aac7b | |
| parent | 0691e05a6750e20c750d46bb4488a574bb147d8a (diff) | |
| download | perlweeklychallenge-club-46b8aecc9397c6211a1e97a7f0638833726294a2.tar.gz perlweeklychallenge-club-46b8aecc9397c6211a1e97a7f0638833726294a2.tar.bz2 perlweeklychallenge-club-46b8aecc9397c6211a1e97a7f0638833726294a2.zip | |
tidying up code
| -rw-r--r-- | challenge-109/james-smith/perl/ch-1.pl | 14 | ||||
| -rw-r--r-- | challenge-109/james-smith/perl/ch-2.pl | 5 |
2 files changed, 8 insertions, 11 deletions
diff --git a/challenge-109/james-smith/perl/ch-1.pl b/challenge-109/james-smith/perl/ch-1.pl index c7820fb3d2..6fccc59edd 100644 --- a/challenge-109/james-smith/perl/ch-1.pl +++ b/challenge-109/james-smith/perl/ch-1.pl @@ -34,15 +34,13 @@ cmpthese(1_000_000, { ## For 79936/s 34% -- ## -sub chowla_map { - my ($t,$n) = (0,@_); ## First attempt - the one-liner is to write this as a map, ## we add $t at the end which is the value returned +sub chowla_map { + my ($t,$n) = (0,@_); ( map { (($n%$_) || ($t+=$_)) && () } 2..$n>>1 ), $t; } -sub chowla_for { - my($t,$n)=(0,@_); ## This time we won't write this as a nasty map/reduce solution... ## @@ -55,12 +53,12 @@ sub chowla_for { ## can be rewritten as: ## ($condition)||($fun()) ## * in perl `foreach` and `for` are synonymous - so we can shorten - - ($n%$_)||($t+=$_) for 2..$n>>1; - - ## Now a quick "shortening" - if there is no specific return + ## Finally a quick "shortening" - if there is no specific return ## statement - we can just omit the return in the last statement... +sub chowla_for { + my($t,$n)=(0,@_); + ($n%$_)||($t+=$_) for 2..$n>>1; $t; } diff --git a/challenge-109/james-smith/perl/ch-2.pl b/challenge-109/james-smith/perl/ch-2.pl index 436e0fca19..bafd89bff8 100644 --- a/challenge-109/james-smith/perl/ch-2.pl +++ b/challenge-109/james-smith/perl/ch-2.pl @@ -67,8 +67,6 @@ say ''; sub sep { say '------------------------------------------------------------------------'; } sub show { say "@{$_}" foreach @{$_[0]}; } -sub four_square { - ## For a start we make the observation that ## ## $a + 2$b + $c + 2$d + $e + 2$f + $g = $n * 4 where $n is the total of a square @@ -94,6 +92,7 @@ sub four_square { ## ## We push any valid results to the array +sub four_square { my ($t,@n1,@res) = (0,@_); $t+=$_ foreach @n1; foreach my $b ( @n1 ) { @@ -110,7 +109,6 @@ sub four_square { return \@res; } -sub four_square_non_unique { ## Now let us make no assumption about the numbers... ## We choose 3 from the list... ## We then compute n (and check for no remainder) @@ -123,6 +121,7 @@ sub four_square_non_unique { ## will end up with 2 entries in the list ## where you swap the equivalent values... +sub four_square_non_unique { my ($t,$check,@n1,%res) = (0,"@{[sort @_]}",@_); $t+=$_ foreach @n1; foreach my $i ( 0..@n1-1 ) { |
