aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-28 21:41:14 +0100
committerGitHub <noreply@github.com>2024-04-28 21:41:14 +0100
commit683ced9865489122141ff09bc3c1bff56e8117e9 (patch)
tree40e81e6a1a4d1efc463cde08f34dbf3ce991ce29
parenteb41dd40b2c2e8205ebba2bd8319d2278972991d (diff)
parentb7c5e5975457a502f15fbe9a171a760bc8f71433 (diff)
downloadperlweeklychallenge-club-683ced9865489122141ff09bc3c1bff56e8117e9.tar.gz
perlweeklychallenge-club-683ced9865489122141ff09bc3c1bff56e8117e9.tar.bz2
perlweeklychallenge-club-683ced9865489122141ff09bc3c1bff56e8117e9.zip
Merge pull request #10001 from BarrOff/barroff-266
feat: add solutions for challenge 266 from BarrOff
-rw-r--r--challenge-266/barroff/julia/ch-1.jl18
-rw-r--r--challenge-266/barroff/raku/ch-1.p632
-rw-r--r--challenge-266/barroff/raku/ch-2.p633
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-266/barroff/julia/ch-1.jl b/challenge-266/barroff/julia/ch-1.jl
new file mode 100644
index 0000000000..bfefcfa2f6
--- /dev/null
+++ b/challenge-266/barroff/julia/ch-1.jl
@@ -0,0 +1,18 @@
+#!/usr/bin/env julia
+
+using Test: @test, @testset
+
+function common_words(sentence1::String, sentence2::String)::Set{String}
+ word_counts = Dict{String,Int}()
+ map(x -> haskey(word_counts, x) ? word_counts[x] += 1 : word_counts[x] = 1, split(sentence1))
+ map(x -> haskey(word_counts, x) ? word_counts[x] += 1 : word_counts[x] = 1, split(sentence2))
+ result = filter(x -> word_counts[x] == 1, keys(word_counts))
+ return length(result) > 0 ? result : Set([""])
+
+end
+
+@testset "common words" begin
+ @test common_words("Mango is sweet", "Mango is sour") == Set(["sweet", "sour"])
+ @test common_words("Mango Mango", "Orange") == Set(["Orange"])
+ @test common_words("Mango is Mango", "Orange is Orange") == Set([""])
+end
diff --git a/challenge-266/barroff/raku/ch-1.p6 b/challenge-266/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..4b0c651668
--- /dev/null
+++ b/challenge-266/barroff/raku/ch-1.p6
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub common-words(Str:D $sentence1, Str:D $sentence2 --> Positional) {
+ my Bag $word-bag = $sentence1.split(/\s/).Bag (+) $sentence2.split(/\s/).Bag;
+ my @candidates = sort(grep({ $word-bag{$_} == 1 }, $word-bag.keys));
+ return @candidates ?? @candidates !! ('',);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is common-words('Mango is sweet', 'Mango is sour'), ('sweet', 'sour').sort,
+ 'works for "Mango is sweet"';
+ is common-words('Mango Mango', 'Orange'), ('Orange'),
+ 'works for "Mango Mango"';
+ is common-words('Mango is Mango', 'Orange is Orange'), (''),
+ 'works for "Mango is Mango"';
+}
+
+#| Take user provided word like CAT
+multi sub MAIN(Str:D $str) {
+ say common-words($str, '');
+}
+
+#| Take user provided words like CAT DOG CAT
+multi sub MAIN(*@str where @str.elems > 1) {
+ say common-words(@str[0], @str[1 .. * - 1].join(' '));
+}
diff --git a/challenge-266/barroff/raku/ch-2.p6 b/challenge-266/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..d600f722a2
--- /dev/null
+++ b/challenge-266/barroff/raku/ch-2.p6
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub x-matrix(@matrix --> Bool:D) {
+ so True == map(
+ { ($_[0] + $_[1] == @matrix.elems - 1) or ($_[0] == $_[1])
+ ?? @matrix[$_[0]][$_[1]] ≠ 0
+ !! @matrix[$_[0]][$_[1]] == 0
+ },
+ cross((0..@matrix.elems - 1), (0..@matrix.elems - 1))
+ ).all;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is x-matrix([ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1]
+ ]), True, 'works for matrix 1';
+ is x-matrix([ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]), False, 'works for matrix 2';
+ is x-matrix([ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]), True, 'works for matrix 3';
+}