diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-17 23:29:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-17 23:29:03 +0100 |
| commit | b64d7cc997e44a52d797c1040883cdab309fc458 (patch) | |
| tree | eb33c188678d7e8459ef97bd02ab41014620850a | |
| parent | ece1ce26167bd64733c4a96535d72f1f84855429 (diff) | |
| parent | 85b5521e55a233585a5c4ea830921e70169f6a59 (diff) | |
| download | perlweeklychallenge-club-b64d7cc997e44a52d797c1040883cdab309fc458.tar.gz perlweeklychallenge-club-b64d7cc997e44a52d797c1040883cdab309fc458.tar.bz2 perlweeklychallenge-club-b64d7cc997e44a52d797c1040883cdab309fc458.zip | |
Merge pull request #8717 from BarrOff/barroff-234
feat: add solutions for challenge 234 from BarrOff
| -rw-r--r-- | challenge-234/barroff/julia/ch-1.jl | 20 | ||||
| -rw-r--r-- | challenge-234/barroff/julia/ch-2.jl | 29 | ||||
| -rw-r--r-- | challenge-234/barroff/raku/ch-1.raku | 28 | ||||
| -rw-r--r-- | challenge-234/barroff/raku/ch-2.raku | 25 |
4 files changed, 102 insertions, 0 deletions
diff --git a/challenge-234/barroff/julia/ch-1.jl b/challenge-234/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..ccec86bfe6 --- /dev/null +++ b/challenge-234/barroff/julia/ch-1.jl @@ -0,0 +1,20 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function common_characters(words) + splitted_words = map(x -> split(x, ""), words) + characer_sets = map(x -> Set(x), splitted_words) + common_chars = reduce(intersect, characer_sets) + result = "" + for i in common_chars + result *= i ^ minimum(map(x -> count(y -> y == i, x), splitted_words)) + end + return split(result, "") +end + +@testset "common characters" begin + @test common_characters(["java", "javascript", "julia"]) == ["j", "a"] + @test common_characters(["bella", "label", "roller"]) == ["e", "l", "l"] + @test common_characters(["cool", "lock", "cook"]) == ["c", "o"] +end diff --git a/challenge-234/barroff/julia/ch-2.jl b/challenge-234/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..40a75decf6 --- /dev/null +++ b/challenge-234/barroff/julia/ch-2.jl @@ -0,0 +1,29 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function is_unequal(a, b, c) + if a != b && a != c && b != c + # println("$(a), $(b), $(c)") + return 1 + end + return 0 +end + +function unequal_triplets(ints) + result = 0 + for i in 1:length(ints) - 2 + for j in i:length(ints) - 1 + for k in j:length(ints) + result += is_unequal(ints[i], ints[j], ints[k]) + end + end + end + return result +end + +@testset "unequal triplets" begin + @test unequal_triplets([4, 4, 2, 4, 3]) == 3 + @test unequal_triplets([1, 1, 1, 1, 1]) == 0 + @test unequal_triplets([4, 7, 1, 10, 7, 4, 1, 1]) == 28 +end diff --git a/challenge-234/barroff/raku/ch-1.raku b/challenge-234/barroff/raku/ch-1.raku new file mode 100644 index 0000000000..e095bb24c2 --- /dev/null +++ b/challenge-234/barroff/raku/ch-1.raku @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub common-characters(*@words --> Seq) { + my $wb = [∩] map({ Bag($_.comb) }, @words); + my $final-string = [~] map({ $_ x $wb{$_} }, keys $wb); + return (sort $final-string.comb); +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is common-characters("java", "javascript", "julia"), ("a", "j"), + 'works for ("java", "javascript", "julia")'; + is common-characters("bella", "label", "roller"), ("e", "l", "l"), + 'works for ("bella", "label", "roller")'; + is common-characters("cool", "lock", "cook"), ("c", "o"), + 'works for ("cool", "lock", "cook")'; +} + +#| Take user provided list like aba aabb abcd bac aabc +multi sub MAIN(*@words where @words.elems ≥ 1) { + my Str @swords = @words; + say common-characters(@swords); +} diff --git a/challenge-234/barroff/raku/ch-2.raku b/challenge-234/barroff/raku/ch-2.raku new file mode 100644 index 0000000000..cde97c4507 --- /dev/null +++ b/challenge-234/barroff/raku/ch-2.raku @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub unequal-triplets(@ints --> Int:D) { + my $ints-bag = Bag(@ints); + return 0 if $ints-bag.elems < 3; + [+] map({ [*] $ints-bag{$_} }, $ints-bag.keys.combinations: 3); +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is unequal-triplets([4, 4, 2, 4, 3]), 3, 'works for (4, 4, 2, 4, 3)'; + is unequal-triplets([1, 1, 1, 1, 1]), 0, 'works for (1, 1, 1, 1, 1)'; + is unequal-triplets([4, 7, 1, 10, 7, 4, 1, 1]), 28, + 'works for (4, 7, 1, 10, 7, 4, 1, 1)'; +} + +#| Take user provided list like 1 1 2 2 2 3 +multi sub MAIN(*@ints where @ints.elems ≥ 1) { + say unequal-triplets(@ints); +} |
