diff options
| -rw-r--r-- | challenge-239/barroff/julia/ch-1.jl | 13 | ||||
| -rw-r--r-- | challenge-239/barroff/julia/ch-2.jl | 16 | ||||
| -rw-r--r-- | challenge-239/barroff/nim/ch_1.nim | 26 | ||||
| -rw-r--r-- | challenge-239/barroff/nim/ch_2.nim | 31 | ||||
| -rw-r--r-- | challenge-239/barroff/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-239/barroff/raku/ch-1.p6 | 20 | ||||
| -rw-r--r-- | challenge-239/barroff/raku/ch-2.p6 | 28 |
7 files changed, 167 insertions, 0 deletions
diff --git a/challenge-239/barroff/julia/ch-1.jl b/challenge-239/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..b2ae66f1fc --- /dev/null +++ b/challenge-239/barroff/julia/ch-1.jl @@ -0,0 +1,13 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function same_string(arr1::Vector{String}, arr2::Vector{String}) + return join(arr1, "") == join(arr2, "") +end + +@testset "same string" begin + @test same_string(["ab", "c"], ["a", "bc"]) == true + @test same_string(["ab", "c"], ["ac", "b"]) == false + @test same_string(["ab", "cd", "e"], ["abcde"]) == true +end diff --git a/challenge-239/barroff/julia/ch-2.jl b/challenge-239/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..f9890127ec --- /dev/null +++ b/challenge-239/barroff/julia/ch-2.jl @@ -0,0 +1,16 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function consistent_strings(allowed::String, words::Vector{String}) + splitted_words = map(x -> split(x, ""), words) + characer_sets = map(x -> Set(x), splitted_words) + allowed_chars = Set(split(allowed, "")) + return length(filter(x -> x ⊆ allowed_chars, characer_sets)) +end + +@testset "consistent strings" begin + @test consistent_strings("ab", ["ad", "bd", "aaab", "baa", "badab"]) == 2 + @test consistent_strings("abc", ["a", "b", "c", "ab", "ac", "bc", "abc"]) == 7 + @test consistent_strings("cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"]) == 4 +end diff --git a/challenge-239/barroff/nim/ch_1.nim b/challenge-239/barroff/nim/ch_1.nim new file mode 100644 index 0000000000..3ada7ef83d --- /dev/null +++ b/challenge-239/barroff/nim/ch_1.nim @@ -0,0 +1,26 @@ +import std/[strutils, unittest] + +# run tests with following command: +# nim c -r ch_1.nim + +proc same_string(arr1, arr2: openArray[string]): bool = + result = join(arr1, "") == join(arr2, "") + +suite "same string": + test "[\"ab\", \"c\"] [\"a\", \"bc\"]": + let + arr1 = ["ab", "c"] + arr2 = ["a", "bc"] + check(same_string(arr1, arr2) == true) + + test "[\"ab\", \"c\"] [\"ac\", \"b\"]": + let + arr1 = ["ab", "c"] + arr2 = ["ac", "b"] + check(same_string(arr1, arr2) == false) + + test "[\"ab\", \"cd\", \"e\"] [\"abcde\"]": + let + arr1 = ["ab", "cd", "e"] + arr2 = ["abcde"] + check(same_string(arr1, arr2) == true) diff --git a/challenge-239/barroff/nim/ch_2.nim b/challenge-239/barroff/nim/ch_2.nim new file mode 100644 index 0000000000..34d06af620 --- /dev/null +++ b/challenge-239/barroff/nim/ch_2.nim @@ -0,0 +1,31 @@ +import std/[sequtils, setutils, sugar, unittest] + +# run tests with following command: +# nim c -r ch_2.nim + +proc consistent_strings(allowed: string, arr1: openArray[string]): int = + let + allowed_chars = toSet(allowed) + result = countIt(mapIt(arr1, toSet(it)), it <= allowed_chars) + +suite "consistent strings": + test "\"ab\", [\"ad\", \"bd\", \"aaab\", \"baa\", \"badab\"]": + let + allowed = "ab" + arr1 = ["ad", "bd", "aaab", "baa", "badab"] + + check(consistent_strings(allowed, arr1) == 2) + + test "\"abc\", [\"a\", \"b\", \"c\", \"ab\", \"ac\", \"bc\", \"abc\"]": + let + allowed = "abc" + arr1 = ["a", "b", "c", "ab", "ac", "bc", "abc"] + + check(consistent_strings(allowed, arr1) == 7) + + test "\"cad\", [\"cc\", \"acd\", \"b\", \"ba\", \"bac\", \"bad\", \"ac\", \"d\"]": + let + allowed = "cad" + arr1 = ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"] + + check(consistent_strings(allowed, arr1) == 4) diff --git a/challenge-239/barroff/perl/ch-1.pl b/challenge-239/barroff/perl/ch-1.pl new file mode 100644 index 0000000000..459d6814af --- /dev/null +++ b/challenge-239/barroff/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use v5.38; + +use List::Util qw/sum/; + +sub same_string ( $arr1, $arr2 ) { + join( '', @$arr1 ) eq join( '', @$arr2 ) ? 1 : 0; +} + +sub MAIN() { + + # if (@ARGV) { + # + # #| Run command line arguments + # say same_string(@ARGV); + # } + # else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 3; + + is same_string( [ "ab", "c" ], [ "a", "bc" ] ), 1, + 'works for ([ "ab", "c" ], [ "a", "bc" ])'; + is same_string( [ "ab", "c" ], [ "ac", "b" ] ), 0, + 'works for ([ "ab", "c" ], [ "ac", "b" ])'; + is same_string( [ "ab", "cd", "e" ], ["abcde"] ), 1, + 'works for ([ "ab", "cd", "e" ], ["abcde"])'; + + # } +} + +MAIN(); diff --git a/challenge-239/barroff/raku/ch-1.p6 b/challenge-239/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..9d2ba1f688 --- /dev/null +++ b/challenge-239/barroff/raku/ch-1.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +use v6.d; + +sub same-string(@arr1, @arr2) { + [~](@arr1) eq [~](@arr2); +} + +#| Run test cases +multi sub MAIN() { + use Test; + plan 3; + + is same-string(("ab", "c"), ("a", "bc")), True, + 'works for (("ab", "c"), ("a", "bc"))'; + is same-string(("ab", "c"), ("ac", "b")), False, + 'works for (("ab", "c"), ("ac", "b"))'; + is same-string(("ab", "cd", "e"), ("abcde",)), True, + 'works for (("ab", "cd", "e"), ("abcde",))'; +} diff --git a/challenge-239/barroff/raku/ch-2.p6 b/challenge-239/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..670baabf40 --- /dev/null +++ b/challenge-239/barroff/raku/ch-2.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env raku + +use v6.d; + +sub consistent-strings(Str:D $allowed, @str --> Int:D) { + my $allowed-chars = Set($allowed.comb); + return grep({ Set($_.comb) ⊆ $allowed-chars }, @str).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is consistent-strings("ab", ("ad", "bd", "aaab", "baa", "badab")), 2, + 'works for ("ab", ("ad", "bd", "aaab", "baa", "badab"))'; + is consistent-strings("abc", ("a", "b", "c", "ab", "ac", "bc", "abc")), 7, + 'works for ("abc", ("a", "b", "c", "ab", "ac", "bc", "abc"))'; + is consistent-strings("cad", ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d")), + 4, + 'works for ("cad", ("cc", "acd", "b", "ba", "bac", "bad", "ac", "d"))'; +} + +#| Take user provided list like aba aabb abcd bac aabc +multi sub MAIN(Str:D $allowed, *@words where @words.elems ≥ 1) { + my Str @swords = @words; + say consistent-strings($allowed, @swords); +} |
