aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-19 20:14:47 +0000
committerGitHub <noreply@github.com>2023-03-19 20:14:47 +0000
commit4788d0fe110c7801a9b5fb0fc769e288f9b00a8b (patch)
treece22b0848cd509bf39e7cb5f618bea4ab382449c
parentd6e66e57618ad6b8cad14f4ae9c438814679e2e2 (diff)
parent50b3f52ed16630c99e6a3dbaaedc9dd449ce40f6 (diff)
downloadperlweeklychallenge-club-4788d0fe110c7801a9b5fb0fc769e288f9b00a8b.tar.gz
perlweeklychallenge-club-4788d0fe110c7801a9b5fb0fc769e288f9b00a8b.tar.bz2
perlweeklychallenge-club-4788d0fe110c7801a9b5fb0fc769e288f9b00a8b.zip
Merge pull request #7756 from 0rir/208
208
-rw-r--r--challenge-208/0rir/raku/ch-1.raku95
-rw-r--r--challenge-208/0rir/raku/ch-2.raku101
2 files changed, 196 insertions, 0 deletions
diff --git a/challenge-208/0rir/raku/ch-1.raku b/challenge-208/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..71d5580a88
--- /dev/null
+++ b/challenge-208/0rir/raku/ch-1.raku
@@ -0,0 +1,95 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ ∋ « ␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+208-1: Minimum Index Sum Submitted by: Mohammad S Anwar
+Given two arrays of strings, find all common strings in the arrays with
+minimum index sum. If no common strings found returns an empty list.
+
+Example 1
+Input: @list1 = ("Perl", "Raku", "Love")
+ @list2 = ("Raku", "Perl", "Hate")
+
+Output: ("Perl", "Raku")
+
+There are two common strings "Perl" and "Raku".
+Index sum of "Perl": 0 + 1 = 1
+Index sum of "Raku": 1 + 0 = 1
+Example 2
+Input: @list1 = ("A", "B", "C")
+ @list2 = ("D", "E", "F")
+
+Output: ()
+
+No common string found, so no result.
+Example 3
+Input: @list1 = ("A", "B", "C")
+ @list2 = ("C", "A", "B")
+
+Output: ("A")
+
+There are three common strings "A", "B" and "C".
+Index sum of "A": 0 + 1 = 1
+Index sum of "B": 1 + 2 = 3
+Index sum of "C": 2 + 0 = 2
+
+=end comment
+
+my @Test =
+ ( (), (), (), ),
+ ( < i >, (), (), ),
+ ( (), < i >, (), ),
+ ( < A B C>, < D E F >, (), ),
+ ( < P R L P P>, < R P H >, < P R>, ),
+ ( < P R L P P>, < R P H >, < P R>, ),
+ ( < P R L P P>, < X X X R P H>, < P R>, ),
+ ( < P R L >, < R P H >, < P R>, ),
+ ( < a a a >, < a a a >, < a >, ),
+ ( < y y a b c>, < z z z z c b a >, < a b c >, ),
+ ( < y y a y b c>, < z z z z b c a >, < a b >, ),
+ ( < y c a y b y>, < z z z z b z a c >, < a b c >, ),
+;
+plan +@Test;
+
+sub min-sum-match( $l, $r --> List ) {
+ my $common = $l ∩ $r;
+ my ($a, $b, %c, $return);
+ for $l, $a, $r, $b -> $o, $d is rw {
+ my $i = 0;
+ my %seen;
+ $d = @$o.map({
+ if %seen{$_}:exists or $_ ∉ $common {
+ $i++;
+ Empty;
+ } else {
+ %seen{$_}++;
+ ($_ => $i++);
+ }
+ }).sort( *.key).Array;
+ }
+ %c.append: @$a;
+ for @$b -> (:key($k), :value($v)) {
+ %c{$k} += $v;
+ }
+ my $min = %c.values.min;
+ %c.keys.map: {
+ %c{$_} == $min
+ ?? ($return.push($_); Empty )
+ !! Empty };
+ $return.sort.List;
+}
+
+for @Test -> ( $l, $r, $exp) {
+ quietly is min-sum-match($l,$r), $exp,
+ "$l.raku() $r.raku() -> $exp.raku()";
+}
+done-testing;
+
+my @list1 = @Test[*-1][0];
+my @list2 = @Test[*-1][1];
+say "\nInput: @list1 = @list1[]\n @list2 = @list2[]";
+say "Output: &min-sum-match( @list1, @list2)";
+
+exit;
diff --git a/challenge-208/0rir/raku/ch-2.raku b/challenge-208/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..b3c410e894
--- /dev/null
+++ b/challenge-208/0rir/raku/ch-2.raku
@@ -0,0 +1,101 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+208-2: Duplicate and Missing Submitted by: Mohammad S Anwar
+
+Given an array of integers in sequence with one missing and one duplicate;
+find the duplicate and missing integer in the given array. Return -1 if none
+found. Assume the array contains no more than one duplicate and missing.
+
+Example 1:
+
+Input: @nums = (1,2,2,4)
+Output: (2,3)
+
+Duplicate is 2 and Missing is 3.
+Example 2:
+
+Input: @nums = (1,2,3,4)
+Output: -1
+
+No duplicate and missing found.
+=end comment
+
+my @Test-bad = [];
+my @Test =
+ ( (1,), Array, ),
+ ( (1,1), Array, ),
+ ( (1,2), Array, ),
+ ( (1,3), Array, ),
+ ( (1,2,3,4), Array, ),
+ ( (1,1,3), (1,2), ),
+ ( (1,3,3), (3,2), ),
+ ( (2,2,3), (2,1), ),
+ ( (2,3,3), (3,4), ),
+ ( (1,2,3,3), (3,4), ),
+ ( (1,2,2,4), (2,3), ),
+
+ (( -10, -10, Empty, |(-8…10)), (-10, -9)),
+ (( Empty, -9, |(-9…10)), ( -9, -10)),
+ (( |(-10…9), 9, Empty ), ( 9, 10)),
+ (( |(-10…8), Empty, 10, 10 ), ( 10, 9)),
+ (( |(-10…0), Empty, 2, |(2…10) ), ( 2, 1)),
+ (( |(-10…0), 0, Empty, |(2…10) ), ( 0, 1)),
+
+ (( -10000, -10000, Empty, |(-9998…10000)), (-10000, -9999)),
+ (( Empty, -9999, |(-9999…10000)), ( -9999, -10000)),
+ (( |(-10000…9999), 9999, Empty, ), ( 9999, 10000)),
+ (( |(-10000…9998), Empty, 10000, 10000 ), ( 10000, 9999)),
+ (( |(-10000…0), Empty, 2, |(2…10000) ), ( 2, 1)),
+ (( |(-10000…0), 0, Empty, |(2…10000) ), ( 0, 1)),
+
+ (( |(-100000…99998), Empty, 100000, 100000 ), ( 100000, 99999)),
+
+ ( (-100…100), Array),
+;
+
+# too small
+multi sub dupe-and-miss( @a where .end < 2 ) { Array }
+
+# simplify fenceposts on full pass TODO process tail not head micro-optim
+multi sub dupe-and-miss( @a where @a[0] == @a[1] --> Array ) {
+ when @a[2] > @a[1] + 1 {
+ return [ @a.head, @a.head+1 ]
+ }
+ return [@a.head, @a.head -1 ];
+}
+
+# common case # above TODO shave tail not head
+multi sub dupe-and-miss( @a --> Any ) {
+ for 1..^@a.end -> $i {
+ if @a[$i] == @a[$i + 1] {
+ if @a[$i-1] == @a[$i] -2 {
+ return [ @a[$i], @a[$i] -1]
+ } else {
+ return [ @a[$i], @a[$i] +1]
+ } } }
+ return Array;
+}
+
+plan +@Test-bad + @Test;
+
+for @Test-bad -> @t {
+ is dupe-and-miss( @t), Array, 'no match';
+}
+
+for @Test -> ( @in, @exp) {
+ is dupe-and-miss( @in), @exp, "&dupe-and-miss @in[0] to @in[@in.end]";
+}
+
+done-testing;
+
+my @n = (-7…7),
+ (|(-5…-2), 0, |(0…5));
+for @n -> @num {
+ print "\nInput: @num = @num[]\nOutput: ", &dupe-and-miss( @num) // -1;
+}
+
+