aboutsummaryrefslogtreecommitdiff
path: root/challenge-154
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-03-07 06:20:06 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-03-07 06:20:06 +0000
commit6345ed685fc976500248cac5a0485f4c94d15e2b (patch)
treee9cfd3c060475da9d5281f4725ec8f94956289ff /challenge-154
parentc6a4ac485deb19dc74928fb571321af11bfa8f63 (diff)
parentaaa4bc2c92501f932cd31cfc2c7e986874682ae5 (diff)
downloadperlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.tar.gz
perlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.tar.bz2
perlweeklychallenge-club-6345ed685fc976500248cac5a0485f4c94d15e2b.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-154')
-rw-r--r--challenge-154/abigail/perl/ch-2.pl28
-rw-r--r--challenge-154/adam-russell/blog.txt1
-rw-r--r--challenge-154/adam-russell/blog1.txt1
-rw-r--r--challenge-154/adam-russell/perl/ch-1.pl54
-rw-r--r--challenge-154/adam-russell/perl/ch-2.pl22
-rw-r--r--challenge-154/adam-russell/prolog/ch-1.p23
-rw-r--r--challenge-154/adam-russell/prolog/ch-2.p8
-rw-r--r--challenge-154/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-154/arne-sommer/perl/ch-1.pl29
-rwxr-xr-xchallenge-154/arne-sommer/perl/ch-2.pl37
-rwxr-xr-xchallenge-154/arne-sommer/perl/missing-permutations-perl29
-rwxr-xr-xchallenge-154/arne-sommer/perl/padovan-prime-bigint-perl43
-rwxr-xr-xchallenge-154/arne-sommer/perl/padovan-prime-perl37
-rwxr-xr-xchallenge-154/arne-sommer/perl/padovan-seq-perl27
-rwxr-xr-xchallenge-154/arne-sommer/perl/padovan-seq2-perl28
-rwxr-xr-xchallenge-154/arne-sommer/raku/ch-1.raku19
-rwxr-xr-xchallenge-154/arne-sommer/raku/ch-2.raku7
-rwxr-xr-xchallenge-154/arne-sommer/raku/missing-permutations19
-rwxr-xr-xchallenge-154/arne-sommer/raku/padovan-prime7
-rwxr-xr-xchallenge-154/arne-sommer/raku/padovan-seq7
-rw-r--r--challenge-154/athanasius/perl/ch-1.pl138
-rw-r--r--challenge-154/athanasius/perl/ch-2.pl100
-rw-r--r--challenge-154/athanasius/raku/ch-1.raku131
-rw-r--r--challenge-154/athanasius/raku/ch-2.raku98
-rw-r--r--challenge-154/bruce-gray/blog.txt1
-rwxr-xr-xchallenge-154/bruce-gray/perl/ch-1.pl16
-rw-r--r--challenge-154/bruce-gray/perl/ch-2.pl13
-rw-r--r--challenge-154/bruce-gray/python/ch-1.py9
-rw-r--r--challenge-154/bruce-gray/python/ch-2.py25
-rw-r--r--challenge-154/bruce-gray/raku/ch-1.raku9
-rw-r--r--challenge-154/bruce-gray/raku/ch-2.raku3
-rw-r--r--challenge-154/cheok-yin-fung/perl/ch-1.pl60
-rw-r--r--challenge-154/cheok-yin-fung/perl/ch-2-test-pre_calc.pl52
-rw-r--r--challenge-154/cheok-yin-fung/perl/ch-2.pl52
-rw-r--r--challenge-154/colin-crain/blog.txt1
-rw-r--r--challenge-154/colin-crain/blog1.txt1
-rwxr-xr-xchallenge-154/colin-crain/perl/ch-1.pl97
-rwxr-xr-xchallenge-154/colin-crain/perl/ch-2.pl179
-rwxr-xr-xchallenge-154/colin-crain/raku/ch-1.raku27
-rwxr-xr-xchallenge-154/colin-crain/raku/ch-2.raku26
-rw-r--r--challenge-154/duncan-c-white/README56
-rw-r--r--challenge-154/duncan-c-white/perl/MakePrimes.pm97
-rw-r--r--challenge-154/duncan-c-white/perl/Perms.pm46
-rwxr-xr-xchallenge-154/duncan-c-white/perl/ch-1.pl44
-rwxr-xr-xchallenge-154/duncan-c-white/perl/ch-2.pl104
-rwxr-xr-xchallenge-154/e-choroba/perl5/ch-1.pl2
-rwxr-xr-xchallenge-154/jo-37/perl/ch-1.pl86
-rwxr-xr-xchallenge-154/jo-37/perl/ch-2.pl100
-rw-r--r--challenge-154/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-154/laurent-rosenfeld/perl/ch-1.pl23
-rw-r--r--challenge-154/laurent-rosenfeld/perl/ch-2.pl50
-rw-r--r--challenge-154/laurent-rosenfeld/raku/ch-1.raku7
-rw-r--r--challenge-154/laurent-rosenfeld/raku/ch-1a.raku6
-rw-r--r--challenge-154/laurent-rosenfeld/raku/ch-2.raku15
-rw-r--r--challenge-154/lubos-kolouch/perl/ch-1.pl35
-rw-r--r--challenge-154/lubos-kolouch/perl/ch-2.pl34
-rw-r--r--challenge-154/lubos-kolouch/python/ch-1.py46
-rw-r--r--challenge-154/lubos-kolouch/python/ch-2.py55
-rw-r--r--challenge-154/mark-senn/blog.txt1
-rw-r--r--challenge-154/mark-senn/blog1.txt1
-rw-r--r--challenge-154/mark-senn/raku/ch-1.raku8
-rw-r--r--challenge-154/mark-senn/raku/ch-2.raku2
-rwxr-xr-xchallenge-154/mattneleigh/perl/ch-1.pl107
-rwxr-xr-xchallenge-154/mattneleigh/perl/ch-2.pl103
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/README.md17
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/deps.edn7
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/src/c154/t1.clj20
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/src/c154/t2.clj21
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/test/c154/t1_test.clj7
-rw-r--r--challenge-154/tyler-wardhaugh/clojure/test/c154/t2_test.clj10
-rw-r--r--challenge-154/wambash/raku/ch-2.raku15
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]);
+ }
+