aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-238/perlboy1967/perl/ch1.pl37
-rwxr-xr-xchallenge-238/perlboy1967/perl/ch2.pl54
2 files changed, 91 insertions, 0 deletions
diff --git a/challenge-238/perlboy1967/perl/ch1.pl b/challenge-238/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..159ba18e4b
--- /dev/null
+++ b/challenge-238/perlboy1967/perl/ch1.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 238
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-238
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Running Sum
+Submitted by: Mohammad S Anwar
+
+You are given an array of integers.
+
+Write a script to return the running sum of the given array. The running
+sum can be calculated as sum[i] = num[0] + num[1] + ... + num[i].
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature 'signatures';
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+
+sub runningSum (@numbers) {
+ my $s = 0;
+ return map { $s += $_; } @numbers;
+}
+
+cmp_deeply([runningSum(1,2,3,4,5)],[1,3,6,10,15]);
+cmp_deeply([runningSum(1,1,1,1,1)],[1,2,3,4,5]);
+cmp_deeply([runningSum(0,-1,1,2)],[0,-1,0,2]);
+
+done_testing;
diff --git a/challenge-238/perlboy1967/perl/ch2.pl b/challenge-238/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..c1e3b5be91
--- /dev/null
+++ b/challenge-238/perlboy1967/perl/ch2.pl
@@ -0,0 +1,54 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 238
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-238
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Persistence Sort
+Submitted by: Mohammad S Anwar
+
+You are given an array of positive integers.
+
+Write a script to sort the given array in increasing order with respect
+to the count of steps required to obtain a single-digit number by multiplying
+its digits recursively for each array element. If any two numbers have the
+same count of steps, then print the smaller number first.
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature 'signatures';
+
+use Test::More;
+use Test::Deep qw(cmp_deeply);
+
+use List::MoreUtils qw(slide);
+use Memoize;
+
+memoize 'cnt_steps';
+
+sub cnt_steps($i) {
+ my $n = 0;
+
+ while (1) {
+ my @d = split(//,$i);
+ last if (@d == 1);
+ $i = slide { $a * $b } @d;
+ $n++;
+ }
+
+ return $n;
+}
+
+sub persistenceSort (@numbers) {
+ sort { cnt_steps($a) <=> cnt_steps($b) || $a <=> $b} @numbers;
+}
+
+cmp_deeply([persistenceSort(15,99,1,34)],[1,15,34,99]);
+cmp_deeply([persistenceSort(50,25,33,22)],[22,33,50,25]);
+
+done_testing;