diff options
| author | rir <rirans@comcast.net> | 2024-04-03 22:50:41 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-04-03 22:50:41 -0400 |
| commit | 647b0febf6185cdd38d7638f8893f106e5da2974 (patch) | |
| tree | 3b4f20dc0a142f26dfeb6da2d4094dca4525e921 /challenge-263 | |
| parent | c2aa18d20881e4079d3858c0218ff99a07032d33 (diff) | |
| download | perlweeklychallenge-club-647b0febf6185cdd38d7638f8893f106e5da2974.tar.gz perlweeklychallenge-club-647b0febf6185cdd38d7638f8893f106e5da2974.tar.bz2 perlweeklychallenge-club-647b0febf6185cdd38d7638f8893f106e5da2974.zip | |
263
Diffstat (limited to 'challenge-263')
| -rw-r--r-- | challenge-263/0rir/raku/ch-1.raku | 51 | ||||
| -rw-r--r-- | challenge-263/0rir/raku/ch-2.raku | 68 |
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-263/0rir/raku/ch-1.raku b/challenge-263/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..2ea2a4515b --- /dev/null +++ b/challenge-263/0rir/raku/ch-1.raku @@ -0,0 +1,51 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +263-1: Target Index Submitted by: Mohammad Sajid Anwar +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. + +Example 1 +Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +Output: (1, 2) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 +Example 2 +Input: @ints = (1, 2, 4, 3, 5), $k = 6 +Output: () + +No element in the given array matching the given target. +Example 3 +Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +Output: (4) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target index: (4) as $ints[4] = 4 +=end comment + +my @Test = + # in k result + (1, 5, 3, 2, 4, 2), 2, (1, 2), + (1, 2, 4, 3, 5), 6, (), + (5, 3, 2, 4, 2, 1), 4, (4), +; +plan @Test ÷ 3; + +sub loca-of-value($a, $k) { + $a.sort.grep( $k, :k).List; +} + +for @Test -> $in, $k, $exp { + is loca-of-value($in, $k), $exp, "$exp <- $k «- $in"; +} + +done-testing; + +my @int = (5, 3, 2, 4, 2, 1); +constant \K = 4; +say "\nInput: @int = @int.raku(), K = $(K)\nOutput: ", loca-of-value( @int, K); diff --git a/challenge-263/0rir/raku/ch-2.raku b/challenge-263/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..e2683fe243 --- /dev/null +++ b/challenge-263/0rir/raku/ch-2.raku @@ -0,0 +1,68 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +263-2: Merge Items Submitted by: Mohammad Sajid Anwar + +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. + +Example 1 +Input: $items1 = [ [1,1], [2,1], [3,2] ] + $items2 = [ [2,2], [1,3] ] +Output: [ [1,4], [2,3], [3,2] ] + +Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +Item id (3) appears 1 time: [3,2] +Example 2 +Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] + $items2 = [ [3,1], [1,3] ] +Output: [ [1,8], [2,3], [3,3] ] +Example 3 +Input: $items1 = [ [1,1], [2,2], [3,3] ] + $items2 = [ [2,3], [2,4] ] +Output: [ [1,1], [2,9], [3,3] ] +=end comment + +my @Test = + # itemsA itemsB combined + [], [], [], + [ [1,1], [2,1], [3,2] ], + [ [2,2], [1,3] ], + [ [1,4], [2,3], [3,2] ], + + [ [1,2], [2,3], [1,3], [3,2] ], + [ [3,1], [1,3] ], + [ [1,8], [2,3], [3,3] ], + + [ [1,1], [2,2], [3,3] ], + [ [2,3], [2,4] ], + [ [1,1], [2,9], [3,3] ], +; + +plan @Test ÷ 3; + +sub inventory-merge( $a, $b) { + my %p{Int}; + for @$a.append( @$b) -> @e { %p{ @e[0]} += @e[1] } + return (%p<>:kv).rotor(2)».Array .sort ; +} + +for @Test -> $a, $b, $exp { + is inventory-merge( $a, $b), $exp, + $exp.gist + ~ "\n <- " ~ $a.gist ~ " ∪ " ~ $b.gist; +} +done-testing; + +my @ii = [ [1,1], [1,2], [1,90], [3,1000], [5,4]]; +my @jj = [ [1,10], [10,2], [1,980], [6,1000], [5,5]]; + +say +"\nInput: \$items1 = @ii.gist()\n \$items2 = @jj.gist() +Output: &inventory-merge(@ii,@jj).gist()"; |
