aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-15 09:34:14 +0100
committerGitHub <noreply@github.com>2023-10-15 09:34:14 +0100
commit5b9480bb3ccf3b51c80b99ec5531ce32bff3d176 (patch)
treef1522ce6827816c2b36a04a0808a959e2c1a6b4a
parent4bbde24e3d663c412acc249855f7689e405b9c7b (diff)
parent4e539bfac765a44d8ab3c6aa1673dfe067955fa7 (diff)
downloadperlweeklychallenge-club-5b9480bb3ccf3b51c80b99ec5531ce32bff3d176.tar.gz
perlweeklychallenge-club-5b9480bb3ccf3b51c80b99ec5531ce32bff3d176.tar.bz2
perlweeklychallenge-club-5b9480bb3ccf3b51c80b99ec5531ce32bff3d176.zip
Merge pull request #8865 from rcmlz/ch-238
ch-238 Raku solution
-rw-r--r--challenge-238/rcmlz/raku/task-one.rakumod15
-rw-r--r--challenge-238/rcmlz/raku/task-two.rakumod35
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-238/rcmlz/raku/task-one.rakumod b/challenge-238/rcmlz/raku/task-one.rakumod
new file mode 100644
index 0000000000..040fe5e6b7
--- /dev/null
+++ b/challenge-238/rcmlz/raku/task-one.rakumod
@@ -0,0 +1,15 @@
+unit module rcmlz::raku::task-one:ver<0.0.1>:auth<github:rcmlz>:api<1>;
+
+# run in terminal: raku --optimize=3 -I challenge-nr238/rcmlz/raku/ -- test/challenge-nr238/raku/task-one.rakutest
+# or raku --optimize=3 -I challenge-nr238 -- test/benchmark-scalabiity.raku --task=task-one --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr238; cat /tmp/nr238_task-one.csv
+
+#|[
+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].
+]
+our sub solution( @input ) is export {
+ [\+] @input
+} \ No newline at end of file
diff --git a/challenge-238/rcmlz/raku/task-two.rakumod b/challenge-238/rcmlz/raku/task-two.rakumod
new file mode 100644
index 0000000000..5f9d48274b
--- /dev/null
+++ b/challenge-238/rcmlz/raku/task-two.rakumod
@@ -0,0 +1,35 @@
+unit module rcmlz::raku::task-two:ver<0.0.1>:auth<github:rcmlz>:api<1>;
+
+# run in terminal: raku --optimize=3 -I challenge-nr238/rcmlz/raku/ -- test/challenge-nr238/raku/task-two.rakutest
+# or raku --optimize=3 -I challenge-nr238 -- test/benchmark-scalabiity.raku --task=task-two --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr238; cat /tmp/nr238_task-two.csv
+
+#|[
+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.
+
+Input: @int = (15, 99, 1, 34)
+Output: (1, 15, 34, 99)
+
+15 => 1 x 5 => 5 (1 step)
+99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+1 => 0 step
+34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+]
+our sub solution(@input) is export {
+ sort &persistence-order, @input
+}
+
+sub split-and-multiply(UInt $x --> UInt){
+ return 0 if $x < 10;
+ my $r = [*] $x.Str.comb.map: *.UInt;
+ 1 + samewith($r)
+}
+
+sub persistence-order(UInt $a, UInt $b --> Order){
+ my ($x, $y) of UInt = ($a, $b).map: *.&split-and-multiply;
+ my Order $ord = $x cmp $y;
+ $ord eqv Same ?? $a cmp $b !! $ord
+}