diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-02 10:52:00 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-02 10:52:00 +0100 |
| commit | 25dbb3c8d62dbad3e2af04ca7b06d65af0aea1f2 (patch) | |
| tree | 7ce8e58815153a60315a2cd255d7b8cf8572a522 | |
| parent | b3e815d751ee0ece3e50bd47904407a3aae0929c (diff) | |
| parent | e49612f330b50e07777cafcaac5ea65c85a6e641 (diff) | |
| download | perlweeklychallenge-club-25dbb3c8d62dbad3e2af04ca7b06d65af0aea1f2.tar.gz perlweeklychallenge-club-25dbb3c8d62dbad3e2af04ca7b06d65af0aea1f2.tar.bz2 perlweeklychallenge-club-25dbb3c8d62dbad3e2af04ca7b06d65af0aea1f2.zip | |
Merge pull request #9855 from massa/master
Challenge 263/2 one-liner, explained
| -rw-r--r-- | challenge-263/massa/raku/ch-1.raku | 64 | ||||
| -rw-r--r-- | challenge-263/massa/raku/ch-2.raku | 69 |
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!) + + |
