aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-15 09:29:50 +0100
committerGitHub <noreply@github.com>2023-10-15 09:29:50 +0100
commit32b93a8b3c24abc4728c461d8caf65351629a9b7 (patch)
tree246bc47aafe617e01a695722470709869488c36c
parent89c66871b2b57061fd6f12d71e016979e41e23a2 (diff)
parentaddc0a6172ab071c4c3c5c7f539ab7da5fef7f13 (diff)
downloadperlweeklychallenge-club-32b93a8b3c24abc4728c461d8caf65351629a9b7.tar.gz
perlweeklychallenge-club-32b93a8b3c24abc4728c461d8caf65351629a9b7.tar.bz2
perlweeklychallenge-club-32b93a8b3c24abc4728c461d8caf65351629a9b7.zip
Merge pull request #8862 from jo-37/contrib
Solutions to challenge 238
-rwxr-xr-xchallenge-238/jo-37/perl/ch-1.pl54
-rwxr-xr-xchallenge-238/jo-37/perl/ch-2.pl79
2 files changed, 133 insertions, 0 deletions
diff --git a/challenge-238/jo-37/perl/ch-1.pl b/challenge-238/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..c52aefa7b6
--- /dev/null
+++ b/challenge-238/jo-37/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0 '!float';
+use PDL;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-verbose] [--] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say "(@{running_sum(@ARGV)})";
+
+
+### Implementation
+
+sub running_sum {
+ pdl(@_)->cumusumover->unpdl;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is running_sum(1, 2, 3, 4, 5), [1, 3, 6, 10, 15], 'example 1';
+ is running_sum(1, 1, 1, 1, 1), [1, 2, 3, 4, 5], 'example 2';
+ is running_sum(0, -1, 1, 2), [0, -1, 0, 2], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-238/jo-37/perl/ch-2.pl b/challenge-238/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..4f4613c153
--- /dev/null
+++ b/challenge-238/jo-37/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+use Math::Prime::Util qw(todigits vecprod);
+
+our ($tests, $examples, $base);
+$base ||= 10;
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [-base=B] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-base=B
+ use base B for step calculation. Default: 10
+
+N...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say "(@{persistence_sort($base, @ARGV)})";
+
+
+### Implementation
+
+# Generalizing the task to an arbitrary base.
+# A "Schwartzian transform" that calculates the steps for each number
+# only once in the sort process is useful for this task: First build a
+# 2xN piddle consisting of pairs [steps, number], then perform a vector
+# sort on this data and finally return the second column - the numbers
+# itself.
+
+sub persistence_sort {
+ my $base = shift;
+
+ long(
+ map {
+ my $steps = 0;
+ for (my $n = $_; $n >= $base; $n = vecprod todigits $n, $base) {
+ $steps++;
+ }
+ [$steps, $_];
+ } @_
+ )->qsortvec->((1))->unpdl;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is persistence_sort(10, 15, 99, 1, 34), [1, 15, 34, 99], 'example 1';
+ is persistence_sort(10, 50, 25, 33, 22), [22, 33, 50, 25], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is persistence_sort(3, 3, 8, 9), [3, 9, 8], 'ternary';
+ is persistence_sort(10, 796, 8222, 9111), [9111, 8222, 796], 'reverse';
+ }
+
+ done_testing;
+ exit;
+}