aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-04 22:25:02 +0100
committerGitHub <noreply@github.com>2024-08-04 22:25:02 +0100
commit7dfdcf55083a51ca36da8eec4d242c64600319d6 (patch)
tree89dc7bd1683abb8cc433308f0c0f1d0c36f37f41
parentc4153a36ebcbfa4ac84e350435f1d00cc01b7c57 (diff)
parent3363a2e7f121eeab5712e83801137295fbb4be16 (diff)
downloadperlweeklychallenge-club-7dfdcf55083a51ca36da8eec4d242c64600319d6.tar.gz
perlweeklychallenge-club-7dfdcf55083a51ca36da8eec4d242c64600319d6.tar.bz2
perlweeklychallenge-club-7dfdcf55083a51ca36da8eec4d242c64600319d6.zip
Merge pull request #10541 from BarrOff/barroff-280
feat: add solutions for challenge 280 from BarrOff
-rw-r--r--challenge-280/barroff/julia/ch-1.jl19
-rw-r--r--challenge-280/barroff/julia/ch-2.jl14
-rw-r--r--challenge-280/barroff/raku/ch-1.p624
-rw-r--r--challenge-280/barroff/raku/ch-2.p625
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);
+}