aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-10-31 13:57:35 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-03 15:09:28 +0100
commit954143d8a4440b15eb3872d33309aac5d6b65321 (patch)
treee724ab2377d36e77e78d0fc6ce91a4e75f98c7cf
parent190edb56a911dc64a29b27643f0ca553f90b7e7e (diff)
downloadperlweeklychallenge-club-954143d8a4440b15eb3872d33309aac5d6b65321.tar.gz
perlweeklychallenge-club-954143d8a4440b15eb3872d33309aac5d6b65321.tar.bz2
perlweeklychallenge-club-954143d8a4440b15eb3872d33309aac5d6b65321.zip
Solution to task 2
-rwxr-xr-xchallenge-241/jo-37/perl/ch-2.pl61
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-241/jo-37/perl/ch-2.pl b/challenge-241/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..a6b45b5be0
--- /dev/null
+++ b/challenge-241/jo-37/perl/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use Math::Prime::Util 'factor';
+use PDL;
+use PDL::NiceSlice;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ list of positive integers
+
+EOS
+
+
+### Input and Output
+
+say "(@{prime_order(@ARGV)})";
+
+
+### Implementation
+
+# This task is very similar to task 2 from week 238. Following the same
+# approach.
+
+sub prime_order {
+ long(map [scalar factor $_, $_], @_)->qsortvec->((1))->unpdl;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is prime_order(11, 8, 27, 4), [11, 4, 8, 27], 'example 1';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is prime_order(27, 8, 121, 49, 167, 131),
+ [131, 167, 49, 121, 8, 27], 'another example';
+ }
+
+ done_testing;
+ exit;
+}