aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHumberto Massa <humbertomassa@gmail.com>2024-04-01 22:05:31 -0300
committerHumberto Massa <humbertomassa@gmail.com>2024-04-01 22:05:31 -0300
commite49612f330b50e07777cafcaac5ea65c85a6e641 (patch)
treedfa6c41858df88f1d9392f52639aaf402086c49a
parentefd3143d09e745500d276abaa07619b2feed95bf (diff)
downloadperlweeklychallenge-club-e49612f330b50e07777cafcaac5ea65c85a6e641.tar.gz
perlweeklychallenge-club-e49612f330b50e07777cafcaac5ea65c85a6e641.tar.bz2
perlweeklychallenge-club-e49612f330b50e07777cafcaac5ea65c85a6e641.zip
Challenge 263/2 one-liner, explained
-rw-r--r--challenge-263/massa/raku/ch-1.raku64
-rw-r--r--challenge-263/massa/raku/ch-2.raku69
2 files changed, 133 insertions, 0 deletions
diff --git a/challenge-263/massa/raku/ch-1.raku b/challenge-263/massa/raku/ch-1.raku
new file mode 100644
index 0000000000..be750c429d
--- /dev/null
+++ b/challenge-263/massa/raku/ch-1.raku
@@ -0,0 +1,64 @@
+#! /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: Target index
+
+=SUBTITLE
+=head2 Submitted by massa
+
+=CHALLENGE
+=head2
+
+You are given an array of integers, `@ints` and a target element `$k`.
+
+Write a script to return the list of indices in the sorted array where
+the element is same as the given target element.
+
+=head3 Example 1:
+
+ Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2
+ Output: (1, 2)
+
+=head3 Example 2:
+
+ Input: @ints = (1, 2, 4, 3, 5), $k = 6
+ Output: ()
+
+=head3 Example 3:
+
+ Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4
+ Output: (4)
+
+=SOLUTION
+
+=end pod
+
+# always use the latest version of Raku
+use v6.*;
+
+sub SOLUTION(\k, +a) {
+ a.sort.grep: * == k, :k
+}
+
+multi MAIN (Bool :$test!) {
+ use Testo;
+
+ my @tests =
+ %{ input => (2, (1, 5, 3, 2, 4, 2)),
+ output => (1, 2) },
+ %{ input => (6, (1, 2, 4, 3, 5)),
+ output => () },
+ %{ input => (4, (5, 3, 2, 4, 2, 1)),
+ output => (4,) },
+ ;
+
+ SOLUTION(|.<input>).&is: .<output>, .<text> for @tests
+} # end of multi MAIN (Bool :$test!)
+
+
diff --git a/challenge-263/massa/raku/ch-2.raku b/challenge-263/massa/raku/ch-2.raku
new file mode 100644
index 0000000000..a60d2dbeb8
--- /dev/null
+++ b/challenge-263/massa/raku/ch-2.raku
@@ -0,0 +1,69 @@
+#! /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: Merge items
+
+=SUBTITLE
+=head2 Submitted by massa
+
+=CHALLENGE
+=head2
+
+You are given two 2-D array of positive integers, $items1 and $items2 where
+element is pair of (item_id, item_quantity).
+
+Write a script to return the merged items.
+
+=head3 Example 1:
+
+ Input: $items1 = ((1, 1), (2, 1), (3, 2))
+ $items2 = ((2, 2), (1, 3))
+ Output: ((1, 4), (2, 3), (3, 2))
+
+=head3 Example 2:
+
+ Input: $items1 = ( (1,2), (2,3), (1,3), (3,2) )
+ $items2 = ( (3,1), (1,3) )
+ Output: ( (1,8), (2,3), (3,3) )
+
+=head3 Example 3:
+
+ Input: $items1 = ( (1,1), (2,2), (3,3) )
+ $items2 = ( (2,3), (2,4) )
+ Output: ( (1,1), (2,9), (3,3) )
+
+=SOLUTION
+
+=end pod
+
+# always use the latest version of Raku
+use v6.*;
+
+sub SOLUTION(@x, @y) {
+ my &a2b = { @^a».pairup.Bag }
+ my &b2a = { %^b.list».kv.sort }
+ b2a( @x.&a2b ⊎ @y.&a2b )
+}
+
+multi MAIN (Bool :$test!) {
+ use Testo;
+
+ my @tests =
+ %{ input => ( ((1, 1), (2, 1), (3, 2)), ((2, 2), (1, 3)) ),
+ output => ((1, 4), (2, 3), (3, 2)) },
+ %{ input => ( ( (1,2), (2,3), (1,3), (3,2) ), ( (3,1), (1,3) ) ),
+ output => ( (1,8), (2,3), (3,3) ) },
+ %{ input => ( ( (1,1), (2,2), (3,3) ), ( (2,3), (2,4) ) ),
+ output => ( (1,1), (2,9), (3,3) ) },
+ ;
+
+ SOLUTION(|.<input>).&is: .<output>, .<text> for @tests
+} # end of multi MAIN (Bool :$test!)
+
+