aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-18 23:04:38 +0000
committerGitHub <noreply@github.com>2023-03-18 23:04:38 +0000
commit0cbd2cab1314655f8baec5d1cdf456600f5c7846 (patch)
treedd1ba2282cf24f793f3af98ab8a914199aaca830
parent87e50ae17dae2c587957134a5ec50ecd3a3c6fce (diff)
parentfc55624c5745cac720c40e9c10f6c67485a531dc (diff)
downloadperlweeklychallenge-club-0cbd2cab1314655f8baec5d1cdf456600f5c7846.tar.gz
perlweeklychallenge-club-0cbd2cab1314655f8baec5d1cdf456600f5c7846.tar.bz2
perlweeklychallenge-club-0cbd2cab1314655f8baec5d1cdf456600f5c7846.zip
Merge pull request #7748 from carlos157oliveira/challenge-208
solution to challenge 208
-rw-r--r--challenge-208/carlos-oliveira/raku/ch-1.raku30
-rw-r--r--challenge-208/carlos-oliveira/raku/ch-2.raku14
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-208/carlos-oliveira/raku/ch-1.raku b/challenge-208/carlos-oliveira/raku/ch-1.raku
new file mode 100644
index 0000000000..7eb011e6f8
--- /dev/null
+++ b/challenge-208/carlos-oliveira/raku/ch-1.raku
@@ -0,0 +1,30 @@
+use Test;
+
+sub minimum-index-sum (Str @array1, Str @array2 --> Hash(Seq)) {
+ return (@array1.pairs X @array2.pairs)
+ .grep({ @_[0].value eq @_[1].value })
+ .map({ [ @_[0].value, @_[0].key + @_[1].key ] })
+ .sort({ @^a[0] cmp @^b[0] || @^a[1] <=> @^b[1] })
+ .squish(:as({ @_[0] }))
+ .map({ |@_ });
+}
+
+is-deeply minimum-index-sum(
+ (my Str @ = ["Perl", "Raku", "Love"]),
+ (my Str @ = ["Raku", "Perl", "Hate"])
+), { Raku => 1, Perl => 1 };
+
+is-deeply minimum-index-sum(
+ (my Str @ = ["A", "B", "C"]),
+ (my Str @ = ["D", "E", "F"])
+), {};
+
+is-deeply minimum-index-sum(
+ (my Str @ = ["A", "B", "C"]),
+ (my Str @ = ["C", "A", "B"])
+), { A => 1, B => 3, C => 2 };
+
+is-deeply minimum-index-sum(
+ (my Str @ = ["A", "B", "C", "D"]),
+ (my Str @ = ["C", "D", "A", "B", "D"])
+), { A => 2, B => 4, C => 2, D => 4 }; \ No newline at end of file
diff --git a/challenge-208/carlos-oliveira/raku/ch-2.raku b/challenge-208/carlos-oliveira/raku/ch-2.raku
new file mode 100644
index 0000000000..f350ab2b4b
--- /dev/null
+++ b/challenge-208/carlos-oliveira/raku/ch-2.raku
@@ -0,0 +1,14 @@
+use Test;
+
+sub find-duplicate-and-missing (Int @numbers) {
+ my @normalized = 1 .. @numbers.elems;
+ for zip(@numbers, @normalized) -> @pair {
+ return @pair if @pair[0] != @pair[1];
+ }
+ return -1;
+}
+
+is find-duplicate-and-missing(my Int @ = [ 1, 2, 2, 4 ]), [2, 3];
+is find-duplicate-and-missing(my Int @ = [ 1, 2, 3, 4 ]), -1;
+is find-duplicate-and-missing(my Int @ = [ 1, 2, 3, 3 ]), [3, 4];
+is find-duplicate-and-missing(my Int @ = [ 1, 2, 3, 4, 5, 7, 7 ]), [7, 6];