diff options
| author | Humberto Massa <humbertomassa@gmail.com> | 2023-10-10 11:00:50 -0300 |
|---|---|---|
| committer | Humberto Massa <humbertomassa@gmail.com> | 2023-10-10 11:08:38 -0300 |
| commit | a46ff5fc2efcb44bbb4e4d40a842c84522bbc81e (patch) | |
| tree | 89765ad1a56f13c14556ef9634f1b05765ff253b | |
| parent | 3ce214ad99d99c1edcae374b6eac3c16448216c8 (diff) | |
| download | perlweeklychallenge-club-a46ff5fc2efcb44bbb4e4d40a842c84522bbc81e.tar.gz perlweeklychallenge-club-a46ff5fc2efcb44bbb4e4d40a842c84522bbc81e.tar.bz2 perlweeklychallenge-club-a46ff5fc2efcb44bbb4e4d40a842c84522bbc81e.zip | |
one-liner raku solutions
| -rw-r--r-- | challenge-238/massa/raku/ch-1.raku | 63 | ||||
| -rw-r--r-- | challenge-238/massa/raku/ch-2.raku | 74 |
2 files changed, 137 insertions, 0 deletions
diff --git a/challenge-238/massa/raku/ch-1.raku b/challenge-238/massa/raku/ch-1.raku new file mode 100644 index 0000000000..45f9339a4e --- /dev/null +++ b/challenge-238/massa/raku/ch-1.raku @@ -0,0 +1,63 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 1: Running Sum + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +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]. + +=head3 Example 1: + +Input: @int = (1, 2, 3, 4, 5) +Output: (1, 3, 6, 10, 15) + +=head3 Example 2: + +Input: @int = (1, 1, 1, 1, 1) +Output: (1, 2, 3, 4, 5) + +=head3 Example 3: + +Input: @int = (0, -1, 1, 2) +Output: (0, -1, 0, 2) + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub runinng-sum(@_) { + [\+] @_ +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (1, 2, 3, 4, 5), output => (1, 3, 6, 10, 15) }, + %{ input => (1, 1, 1, 1, 1), output => (1, 2, 3, 4, 5) }, + %{ input => (0, -1, 1, 2), output => (0, -1, 0, 2) }, + ]; + + for @tests { + runinng-sum( .<input> ).&is-deeply: .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (Bool :$test!) + + diff --git a/challenge-238/massa/raku/ch-2.raku b/challenge-238/massa/raku/ch-2.raku new file mode 100644 index 0000000000..19c9a805d4 --- /dev/null +++ b/challenge-238/massa/raku/ch-2.raku @@ -0,0 +1,74 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Persistence Sort + +=SUBTITLE +=head2 Submitted by massa + +=CHALLENGE +=head2 + +You are given an array of 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. + +=head3 Example 1: + +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) + +=head3 Example 2: + +Input: @int = (50, 25, 33, 22) +Output: (22, 33, 50, 25) + +50 => 5 x 0 => 0 (1 step) +25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps) +33 => 3 x 3 => 9 (1 step) +22 => 2 x 2 => 4 (1 step) + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +my sub persistence($_) { + .comb.elems > 1 and 1 + persistence([*] .comb) +} + +sub persistence-sort(@_) { + @_.sort.sort: *.&persistence +} + +multi MAIN (Bool :$test!) { + use Test; + + my @tests = [ + %{ input => (15, 99, 1, 34), output => (1, 15, 34, 99) }, + %{ input => (50, 25, 33, 22), output => (22, 33, 50, 25) }, + ]; + + for @tests { + persistence-sort( .<input> ).&is-deeply: .<output>, .<text>; + + } # end of for @tests +} # end of multi MAIN (:$test!) + + |
