diff options
| -rw-r--r-- | challenge-280/barroff/julia/ch-1.jl | 19 | ||||
| -rw-r--r-- | challenge-280/barroff/julia/ch-2.jl | 14 | ||||
| -rw-r--r-- | challenge-280/barroff/raku/ch-1.p6 | 24 | ||||
| -rw-r--r-- | challenge-280/barroff/raku/ch-2.p6 | 25 |
4 files changed, 82 insertions, 0 deletions
diff --git a/challenge-280/barroff/julia/ch-1.jl b/challenge-280/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..056e092cc3 --- /dev/null +++ b/challenge-280/barroff/julia/ch-1.jl @@ -0,0 +1,19 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function twice_appearance(str::AbstractString)::Char + str_set = Set{Char}() + for c in str + if in(c, str_set) + return c + end + push!(str_set, c) + end +end + +@testset "twice appearance" begin + @test twice_appearance("acbddbca") == 'd' + @test twice_appearance("abccd") == 'c' + @test twice_appearance("abcdabbb") == 'a' +end diff --git a/challenge-280/barroff/julia/ch-2.jl b/challenge-280/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..f1115fdc28 --- /dev/null +++ b/challenge-280/barroff/julia/ch-2.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function count_asterisks(str::AbstractString)::Int + pairless = replace(str, r"\|.*?\|" => "") + count(x -> x == '*', pairless) +end + +@testset "count asterisks" begin + @test count_asterisks("p|*e*rl|w**e|*ekly|") == 2 + @test count_asterisks("perl") == 0 + @test count_asterisks("th|ewe|e**|k|l***ych|alleng|e") == 5 +end diff --git a/challenge-280/barroff/raku/ch-1.p6 b/challenge-280/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..5c9bb77311 --- /dev/null +++ b/challenge-280/barroff/raku/ch-1.p6 @@ -0,0 +1,24 @@ +#!/usr/bin/env raku + +use v6.d; + +sub twice-appearance(Str $str --> Str) { + my %key-dict; + my $res = map({ %key-dict{$_} ?? return $_ !! %key-dict{$_} = 1 }, $str.comb); + dd $res; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is twice-appearance("acbddbca"), "d", 'works for "acbddbca"'; + is twice-appearance("abccd"), "c", 'works for "abccd"'; + is twice-appearance("abcdabbb"), "a", 'works for "abcdabbb"'; +} + +#| Take user provided string like "bcdabbb" +multi sub MAIN(Str $str where $str ~ /^<[a..z]>$/) { + say twice-appearance($str); +} diff --git a/challenge-280/barroff/raku/ch-2.p6 b/challenge-280/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..c6b255b0f6 --- /dev/null +++ b/challenge-280/barroff/raku/ch-2.p6 @@ -0,0 +1,25 @@ +#!/usr/bin/env raku + +use v6.d; + +sub count-asterisks(Str $str --> Int) { + my $pairless = $str.subst(/\|.*?\|/, :g); + grep({ $_ eq '*' }, $pairless.comb).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 3; + + is count-asterisks("p|*e*rl|w**e|*ekly|"), 2, + 'works for "p|*e*rl|w**e|*ekly|"'; + is count-asterisks("perl"), 0, 'works for "perl"'; + is count-asterisks("th|ewe|e**|k|l***ych|alleng|e"), 5, + 'works for "th|ewe|e**|k|l***ych|alleng|e"'; +} + +#| Take user provided number like "Perl Weekly Challenge" l a +multi sub MAIN(Str $str) { + say count-asterisks($str); +} |
