diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-03-07 06:20:06 +0000 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-03-07 06:20:06 +0000 |
| commit | 6345ed685fc976500248cac5a0485f4c94d15e2b (patch) | |
| tree | e9cfd3c060475da9d5281f4725ec8f94956289ff /challenge-154 | |
| parent | c6a4ac485deb19dc74928fb571321af11bfa8f63 (diff) | |
| parent | aaa4bc2c92501f932cd31cfc2c7e986874682ae5 (diff) | |
| download | perlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.tar.gz perlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.tar.bz2 perlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-154')
73 files changed, 2549 insertions, 63 deletions
diff --git a/challenge-154/abigail/perl/ch-2.pl b/challenge-154/abigail/perl/ch-2.pl index 4a691a2da6..784cef4ee2 100644 --- a/challenge-154/abigail/perl/ch-2.pl +++ b/challenge-154/abigail/perl/ch-2.pl @@ -1,13 +1,5 @@ #!/opt/perl/bin/perl -use 5.032; - -no strict; -no warnings; - -use experimental 'signatures'; -use experimental 'lexical_subs'; - # # See https://theweeklychallenge.org/blog/perl-weekly-challenge-154 # @@ -37,12 +29,10 @@ use experimental 'lexical_subs'; # # -# To find out whether a number is prime, we could use -# -# Math::Util::Prime, but we have seen this a gazillion times recently -# in the Colins weekly review. We could use trial division, but we -# have seen that one a gazillion times in the same review as well. -# Same with using a sieve. +# To find out whether a number is prime, we could use Math::Util::Prime, +# but we have seen this a gazillion times recently in Colins weekly review. +# We could use trial division, but we have seen that one a gazillion times +# in the same review as well. Same with using a sieve. # # It'll be fucking boring to see the same copy and pasted code over # and over again. @@ -61,10 +51,10 @@ use experimental 'lexical_subs'; # # You might get a cup of tea when running this program. Better, get two cups. # -# Oh, and we cram the entire program in just three lines. Just to make the -# challenge a bit interesting. +# Oh, and we cram the entire program in just a little over two lines. +# Just to make the challenge a bit interesting. # -sub _($n){%;=();while(%;<$n-2){$n%($;=2+int(rand($n-2)))||return;$;{$;}=1}1} -($-,$:,@:)=(10,(1)x3,2); -++$:&&($:==$:[-2]+$:[-3])&&(push@:,$:)&&_($:)&&say($:)&&$---while$- +sub _{($=,%;)=@_;while(%;<$=-2){$=%($;=2+int rand($=-2))||return;$;{$;}=1}1}($\ +,$-,$:,@:)=($/,10,(1)x3,2);++$:&&($:==$:[-2]+$:[-3])&&(push@:,$:)&&_($:)&&print +($:)&&$---while$- diff --git a/challenge-154/adam-russell/blog.txt b/challenge-154/adam-russell/blog.txt new file mode 100644 index 0000000000..64e1e717e0 --- /dev/null +++ b/challenge-154/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/03/06 diff --git a/challenge-154/adam-russell/blog1.txt b/challenge-154/adam-russell/blog1.txt new file mode 100644 index 0000000000..aa029b1cc0 --- /dev/null +++ b/challenge-154/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2022/03/06 diff --git a/challenge-154/adam-russell/perl/ch-1.pl b/challenge-154/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..a77fe244c7 --- /dev/null +++ b/challenge-154/adam-russell/perl/ch-1.pl @@ -0,0 +1,54 @@ +use strict; +use warnings; +## +# You are given possible permutations of the string "PERL". +# Write a script to find any permutations missing from the list. +## +use Algorithm::Loops q/NestedLoops/; + +sub factorial{ + my($n) = @_; + return 1 if $n == 1; + $n * factorial($n - 1); +} + +sub missing_permutations{ + my($permutations, $s) = @_; + my @missing; + ## + # remove any duplicates + ## + my %permutations; + map {$permutations{$_}=undef} @{$permutations}; + $permutations = [keys %permutations]; + ## + # get the letters missing in each slot + ## + my @missing_letters; + for my $i (0 .. length($s) - 1){ + my %slot_counts; + my @ith_letters = map {my @a = split(//, $_); $a[$i]} @{$permutations}; + map{$slot_counts{$_}++} @ith_letters; + $missing_letters[$i] = [grep {$slot_counts{$_} != factorial(length($s) - 1)} keys %slot_counts]; + } + ## + # determine which missing letters form missing permutations + ## + my $nested = NestedLoops(\@missing_letters); + while (my @set = $nested->()){ + my $candidate = join("", @set); + my @matched = grep {$candidate eq $_} @{$permutations}; + push @missing, $candidate if !@matched; + } + return @missing; +} + + +MAIN:{ + my @missing = missing_permutations( + ["PELR", "PREL", "PERL", "PRLE", "PLER", "PLRE", "EPRL", "EPLR", "ERPL", + "ERLP", "ELPR", "ELRP", "RPEL", "RPLE", "REPL", "RELP", "RLPE", "RLEP", + "LPER", "LPRE", "LEPR", "LRPE", "LREP"], "PERL" + ); + print join(", ", @missing) . "\n"; +}
\ No newline at end of file diff --git a/challenge-154/adam-russell/perl/ch-2.pl b/challenge-154/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..c9d8b1ce71 --- /dev/null +++ b/challenge-154/adam-russell/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# Write a script to compute the first 10 distinct Padovan Primes. +## +use Math::Primality qw/is_prime/; + +sub first_n_padovan_primes{ + my($n) = @_; + my @padovan_primes; + my @padovans = (1, 1, 1); + { + push @padovans, $padovans[@padovans - 2] + $padovans[@padovans - 3]; + push @padovan_primes, $padovans[@padovans - 1] if is_prime($padovans[@padovans - 1]); + redo if @padovan_primes <= $n; + } + return @padovan_primes[1..@padovan_primes - 1]; +} + +MAIN:{ + print join(", ", first_n_padovan_primes(10)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-154/adam-russell/prolog/ch-1.p b/challenge-154/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..f1214b5c69 --- /dev/null +++ b/challenge-154/adam-russell/prolog/ch-1.p @@ -0,0 +1,23 @@ +:-initialization(main). +/* + * You are given possible permutations of the string 'PERL'. + * Write a script to find any permutations missing from the list. +*/ +make_lists([], []). +make_lists([Word|Words], [List|Rest]):- + atom_chars(Word, List), + make_lists(Words, Rest). + +missing_permutation(Word, Permutations, Missing):- + atom_chars(Word, Chars), + permutation(Chars, Permutation), + \+ member(Permutation, Permutations), + atom_chars(Missing, Permutation). + +main:- + make_lists(['PELR', 'PREL', 'PERL', 'PRLE', 'PLER', 'PLRE', 'EPRL', 'EPLR', 'ERPL', + 'ERLP', 'ELPR', 'ELRP', 'RPEL', 'RPLE', 'REPL', 'RELP', 'RLPE', 'RLEP', + 'LPER', 'LPRE', 'LEPR', 'LRPE', 'LREP'], Permutations), + missing_permutation('PERL', Permutations, Missing), + write(Missing), nl, + halt.
\ No newline at end of file diff --git a/challenge-154/adam-russell/prolog/ch-2.p b/challenge-154/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..64b09dc745 --- /dev/null +++ b/challenge-154/adam-russell/prolog/ch-2.p @@ -0,0 +1,8 @@ +padovan_primes(Size, Primes, PrimesAccum, A, B, C) --> {D is B + A, fd_not_prime(D)}, [A], padovan_primes(Size, Primes, PrimesAccum, B, C, D). +padovan_primes(Size, Primes, PrimesAccum, A, B, C) --> {D is B + A, fd_prime(D), append(PrimesAccum, [D], NewPrimesAccum), length(NewPrimesAccum, L), L < Size}, [A], padovan_primes(Size, Primes, NewPrimesAccum, B, C, D). +padovan_primes(Size, Primes, PrimesAccum, A, B, _) --> {D is B + A, fd_prime(D), append(PrimesAccum, [D], NewPrimesAccum), length(NewPrimesAccum, L), L >= Size, Primes = NewPrimesAccum}, [D]. + +n_padovan_primes(N, Primes):- + succ(N, X), + phrase(padovan_primes(X, PadovanPrimes, [], 1, 1, 1), _), + [_|Primes] = PadovanPrimes.
\ No newline at end of file diff --git a/challenge-154/arne-sommer/blog.txt b/challenge-154/arne-sommer/blog.txt new file mode 100644 index 0000000000..4d9f0b9224 --- /dev/null +++ b/challenge-154/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/padovan-missing.html diff --git a/challenge-154/arne-sommer/perl/ch-1.pl b/challenge-154/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..b2b1b85742 --- /dev/null +++ b/challenge-154/arne-sommer/perl/ch-1.pl @@ -0,0 +1,29 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use Algorithm::Combinatorics 'permutations'; +use List::Util 'uniq'; +use Getopt::Long; + +my $verbose = 0; GetOptions("verbose" => \$verbose); + +my $string = shift(@ARGV) || 'PERL'; +my $permutations = shift(@ARGV) || "PELR PREL PERL PRLE PLER PLRE EPRL EPLR ERPL + ERLP ELPR ELRP RPEL RPLE REPL RELP RLPE RLEP + LPER LPRE LEPR LRPE LREP"; + +my @letters = split("", $string); +my %permuations = map { $_ => 1} split(/\s+/, $permutations); + +my @missing; + +for my $candidate (permutations(\@letters)) +{ + my $as_string = join("", @$candidate); + say ": Checking candidate: $as_string" if $verbose; + push(@missing, $as_string) unless $permuations{$as_string}; +} + +say join(", ", uniq @missing) if @missing; diff --git a/challenge-154/arne-sommer/perl/ch-2.pl b/challenge-154/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..1f9708a216 --- /dev/null +++ b/challenge-154/arne-sommer/perl/ch-2.pl @@ -0,0 +1,37 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; +use feature 'state'; +use Math::Prime::Util 'is_prime'; + +no warnings qw(experimental::signatures); + +my $count = $ARGV[0] || 10; + +sub next_padovan +{ + state @padovan = (1, 1, 1); + state $index = 0; + + while (@padovan <= $index) + { + push(@padovan, $padovan[-2] + $padovan[-3]); + } + + return $padovan[$index++]; +} + +my @pp; + +while (@pp < $count) +{ + my $next = next_padovan; + next if @pp && $next eq $pp[-1]; + next unless is_prime($next); + push(@pp, $next); +} + +say join(", ", @pp); diff --git a/challenge-154/arne-sommer/perl/missing-permutations-perl b/challenge-154/arne-sommer/perl/missing-permutations-perl new file mode 100755 index 0000000000..b2b1b85742 --- /dev/null +++ b/challenge-154/arne-sommer/perl/missing-permutations-perl @@ -0,0 +1,29 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use Algorithm::Combinatorics 'permutations'; +use List::Util 'uniq'; +use Getopt::Long; + +my $verbose = 0; GetOptions("verbose" => \$verbose); + +my $string = shift(@ARGV) || 'PERL'; +my $permutations = shift(@ARGV) || "PELR PREL PERL PRLE PLER PLRE EPRL EPLR ERPL + ERLP ELPR ELRP RPEL RPLE REPL RELP RLPE RLEP + LPER LPRE LEPR LRPE LREP"; + +my @letters = split("", $string); +my %permuations = map { $_ => 1} split(/\s+/, $permutations); + +my @missing; + +for my $candidate (permutations(\@letters)) +{ + my $as_string = join("", @$candidate); + say ": Checking candidate: $as_string" if $verbose; + push(@missing, $as_string) unless $permuations{$as_string}; +} + +say join(", ", uniq @missing) if @missing; diff --git a/challenge-154/arne-sommer/perl/padovan-prime-bigint-perl b/challenge-154/arne-sommer/perl/padovan-prime-bigint-perl new file mode 100755 index 0000000000..b16d7befec --- /dev/null +++ b/challenge-154/arne-sommer/perl/padovan-prime-bigint-perl @@ -0,0 +1,43 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; +use feature 'state'; +use bigint; +use Math::Prime::Util 'is_prime'; + +no warnings qw(experimental::signatures); + +my $count = $ARGV[0] || 10; + +sub next_padovan +{ + state @padovan = (); + + if (@padovan < 2) + { + push(@padovan, 1); + } + else + { + push(@padovan, $padovan[-2] + $padovan[-3]); + } + + shift @padovan if @padovan == 4; + + return $padovan[-1]; +} + +my @pp; + +while (@pp < $count) +{ + my $next = next_padovan; + next if @pp && $next eq $pp[-1]; + next unless is_prime($next); + push(@pp, $next); +} + +say join(", ", @pp); diff --git a/challenge-154/arne-sommer/perl/padovan-prime-perl b/challenge-154/arne-sommer/perl/padovan-prime-perl new file mode 100755 index 0000000000..1f9708a216 --- /dev/null +++ b/challenge-154/arne-sommer/perl/padovan-prime-perl @@ -0,0 +1,37 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; +use feature 'state'; +use Math::Prime::Util 'is_prime'; + +no warnings qw(experimental::signatures); + +my $count = $ARGV[0] || 10; + +sub next_padovan +{ + state @padovan = (1, 1, 1); + state $index = 0; + + while (@padovan <= $index) + { + push(@padovan, $padovan[-2] + $padovan[-3]); + } + + return $padovan[$index++]; +} + +my @pp; + +while (@pp < $count) +{ + my $next = next_padovan; + next if @pp && $next eq $pp[-1]; + next unless is_prime($next); + push(@pp, $next); +} + +say join(", ", @pp); diff --git a/challenge-154/arne-sommer/perl/padovan-seq-perl b/challenge-154/arne-sommer/perl/padovan-seq-perl new file mode 100755 index 0000000000..acc613fb64 --- /dev/null +++ b/challenge-154/arne-sommer/perl/padovan-seq-perl @@ -0,0 +1,27 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; +use feature 'state'; + +no warnings qw(experimental::signatures); + +my $count = $ARGV[0] || 10; + +sub padovan ($index) +{ + state @padovan = (1, 1, 1); + + while (@padovan <= $index) + { + push(@padovan, $padovan[-2] + $padovan[-3]); + } + |
