diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-16 22:42:44 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-16 22:42:44 +0100 |
| commit | 0d63d2bdafd20af0c01dad157eca4deeced27c5f (patch) | |
| tree | 8df6f6ee754b1e34c7b04e301256d1169524c666 | |
| parent | 358805e9bcea836557c3d6052c04ef118d4ec393 (diff) | |
| parent | 4cbef3a0c11190d1945c2350c76c3ddde61cdb0f (diff) | |
| download | perlweeklychallenge-club-0d63d2bdafd20af0c01dad157eca4deeced27c5f.tar.gz perlweeklychallenge-club-0d63d2bdafd20af0c01dad157eca4deeced27c5f.tar.bz2 perlweeklychallenge-club-0d63d2bdafd20af0c01dad157eca4deeced27c5f.zip | |
Merge pull request #10277 from BarrOff/barroff-273
feat: add solutions for challenge 273 from BarrOff
| -rw-r--r-- | challenge-273/barroff/julia/ch-1.jl | 20 | ||||
| -rw-r--r-- | challenge-273/barroff/julia/ch-2.jl | 14 | ||||
| -rw-r--r-- | challenge-273/barroff/perl/ch-2.pl | 27 | ||||
| -rw-r--r-- | challenge-273/barroff/raku/ch-1.p6 | 26 | ||||
| -rw-r--r-- | challenge-273/barroff/raku/ch-2.p6 | 23 |
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-273/barroff/julia/ch-1.jl b/challenge-273/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..791e0f8690 --- /dev/null +++ b/challenge-273/barroff/julia/ch-1.jl @@ -0,0 +1,20 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function percentage_of_character(str::AbstractString, char::Char)::Int + if char in str + ceil(Int, 100 * count(x -> x == char, str) / lastindex(str)) + else + return 0 + end +end + +@testset "percentage of character" begin + @test percentage_of_character("perl", 'e') == 25 + @test percentage_of_character("java", 'a') == 50 + @test percentage_of_character("python", 'm') == 0 + @test percentage_of_character("ada", 'a') == 67 + @test percentage_of_character("ballerina", 'l') == 23 + @test percentage_of_character("analitik", 'k') == 13 +end diff --git a/challenge-273/barroff/julia/ch-2.jl b/challenge-273/barroff/julia/ch-2.jl new file mode 100644 index 0000000000..a8c2400ce7 --- /dev/null +++ b/challenge-273/barroff/julia/ch-2.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function b_after_a(str::AbstractString)::Bool + occursin(r"^a*b+$", str) ? true : false +end + +@testset "b after a" begin + @test b_after_a("aabb") == true + @test b_after_a("abab") == false + @test b_after_a("aaa") == false + @test b_after_a("bbb") == true +end diff --git a/challenge-273/barroff/perl/ch-2.pl b/challenge-273/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..1c1830cc00 --- /dev/null +++ b/challenge-273/barroff/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use v5.38; + +sub b_after_a ($str) { + return $str =~ /^a*b+$/ ? 1 : 0; +} + +sub MAIN() { + if (@ARGV) { + + #| Run on command line argument + say b_after_a( $ARGV[0] ); + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 4; + is b_after_a('aabb'), 1, 'works for "aabb'; + is b_after_a('abab'), 0, 'works for "abab'; + is b_after_a('aaa'), 0, 'works for "aaa'; + is b_after_a('bbb'), 1, 'works for "bbb'; + } +} + +MAIN(); + diff --git a/challenge-273/barroff/raku/ch-1.p6 b/challenge-273/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..96a5e7e77f --- /dev/null +++ b/challenge-273/barroff/raku/ch-1.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +use v6.d; + +sub percentage-of-character(Str $str, Str $char --> Int) { + return 0 unless $str.contains($char); + ceiling(100 * Bag($str.comb){$char} รท $str.chars) +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 6; + + is percentage-of-character("perl", "e"), 25, 'works for "perl" and "e"'; + is percentage-of-character("java", "a"), 50, 'works for "perl" and "a"'; + is percentage-of-character("python", "m"), 0, 'works for "python" and "m"'; + is percentage-of-character("ada", "a"), 67, 'works for "ada" and "a"'; + is percentage-of-character("ballerina", "l"), 23, 'works for "ballerina" and "l"'; + is percentage-of-character("analitik", "k"), 13, 'works for "analitik" and "k"'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Str $str, Str $char) { + say percentage-of-character($str, $char); +} diff --git a/challenge-273/barroff/raku/ch-2.p6 b/challenge-273/barroff/raku/ch-2.p6 new file mode 100644 index 0000000000..53f386f904 --- /dev/null +++ b/challenge-273/barroff/raku/ch-2.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env raku + +use v6.d; + +sub b-after-a(Str $str where $str ~~ /^<[ab]>*$/ --> Bool) { + return $str ~~ /^a*b+$/ ?? True !! False; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 4; + + is b-after-a("aabb"), True, 'works for "aabb"'; + is b-after-a("abab"), False, 'works for "abab"'; + is b-after-a("aaa"), False, 'works for "aaa"'; + is b-after-a("bbb"), True, 'works for "bbb"'; +} + +#| Take user provided number like 10 1 111 24 1000 +multi sub MAIN(Str $str) { + say b-after-a($str); +} |
