aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-08-01 20:16:19 +0100
committerGitHub <noreply@github.com>2024-08-01 20:16:19 +0100
commit6e4cf44ad4272bd7377ba1c4287c914b2f7d3338 (patch)
tree3ce6daa2b8b3ac82162b2a35726a088d1c142fd2
parent2d7c1420a5936abe2fd9d77112234be561f3baf0 (diff)
parent2b914a90cbd99c6be35b210efb893fac9ea95bfe (diff)
downloadperlweeklychallenge-club-6e4cf44ad4272bd7377ba1c4287c914b2f7d3338.tar.gz
perlweeklychallenge-club-6e4cf44ad4272bd7377ba1c4287c914b2f7d3338.tar.bz2
perlweeklychallenge-club-6e4cf44ad4272bd7377ba1c4287c914b2f7d3338.zip
Merge pull request #10529 from 0rir/work
280
-rw-r--r--challenge-280/0rir/raku/ch-1.raku68
-rw-r--r--challenge-280/0rir/raku/ch-2.raku140
2 files changed, 208 insertions, 0 deletions
diff --git a/challenge-280/0rir/raku/ch-1.raku b/challenge-280/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..be79984f82
--- /dev/null
+++ b/challenge-280/0rir/raku/ch-1.raku
@@ -0,0 +1,68 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+280 1: Twice Appearance Submitted by: Mohammad Sajid Anwar
+You are given a string, $str, containing lowercase English letters only.
+
+Write a script to print the first letter that appears twice.
+
+Example 1
+Input: $str = "acbddbca"
+Output: "d"
+Example 2
+Input: $str = "abccd"
+Output: "c"
+Example 3
+Input: $str = "abcdabbb"
+Output: "a"
+=end comment
+
+my @Test =
+ # $in $exp
+ "acbddbca", "d",
+ "abccd", "c",
+ "abcdabbb", "a",
+ "abcdefgh", Str,
+ "", Str,
+;
+my @Mix =
+ "abcdefghiFi", 'f',
+;
+plan @Test * 1.5 + @Mix;
+
+multi task( Str:D $in, Bool:D :$case-force! ) { task( $in.lc); }
+multi task( Str:D $in, Bool:D :$case-ck! ) {
+ die qq{ERROR: "$in" is not lowercase.} if $in ne $in.lc;
+ task( $in);
+}
+multi task( Str:D $in --> Str ) {
+ my $s = ().SetHash;
+ my $e = $s.elems;
+ for 0..$in.chars -> \i {
+ my $c = $in.substr( i, 1);
+ $s.set: $c;
+ return $c if $s.elems == $e;
+ ++$e ;
+ }
+ Str;
+}
+
+for @Test -> $in, $exp {
+ is task($in), $exp, ($exp // "(Str)") ~ " <- $in";
+ is task($in, :case-ck),
+ $exp, ":case-ck " ~ ($exp // "(Str)") ~ " <- $in";
+ is task($in, :case-force),
+ $exp, ":case-force " ~ ($exp // "(Str)") ~ " <- $in";
+}
+for @Mix -> $in, $exp {
+ dies-ok { task($in, :case-ck)}, ":case-ck dies <- $in";
+ is task($in, :case-force), $exp, ":case-force $exp <- $in";
+}
+done-testing;
+
+my $str = 'abcdefghijklmnopqrstuvwxyzs';
+say qq{\nInput: \$str = "$str"\nOutput: "&task($str)"};
+
diff --git a/challenge-280/0rir/raku/ch-2.raku b/challenge-280/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..c60a29bdcc
--- /dev/null
+++ b/challenge-280/0rir/raku/ch-2.raku
@@ -0,0 +1,140 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+Task 2: Count Asterisks
+Submitted by: Mohammad Sajid Anwar
+ REPHASED
+Given a string, $str, where two consecutive vertical bars group the chars
+between them; a bar can only pair with one or none other bar.
+Return the number of asterisks, *, which are not within such pairs.
+
+Example 1
+Input: $str = "p|*e*rl|w**e|*ekly|"
+Ouput: 2
+
+The characters we are looking here are "p" and "w**e".
+Example 2
+Input: $str = "perl"
+Ouput: 0
+Example 3
+Input: $str = "th|ewe|e**|k|l***ych|alleng|e"
+Ouput: 5
+
+The characters we are looking here are "th", "e**", "l***ych" and "e".
+=end comment
+
+my @Test =
+ # str exp
+ '', 0,
+ # ^ sep $
+ '|', 0,
+ # ^ outer $
+ 'x', 0,
+ '*', 1,
+ # ^ outer sep $
+ '*|', 1,
+ 'x|', 0,
+ # ^ sep outer $
+ '|*', 1,
+ '|x', 0,
+
+ # ^ outer sep outer $
+ '*|*', 2,
+
+ # ^ outer pair …
+ 'x||', 0,
+ 'x|i|', 0,
+ 'x|*|', 0,
+ '*||', 1,
+ '*|i|', 1,
+ '*|*|', 1,
+ '*|*||i|', 1,
+ 'x||x', 0,
+ 'x|i|x', 0,
+ 'x|*|x', 0,
+ 'x|*||i|x', 0,
+ '*||x', 1,
+ '*|i|x', 1,
+ '*|*|x', 1,
+ '*|*||i|x', 1,
+ 'x|*||i|', 0,
+ '*|*|*', 2,
+ '*|a|a', 1,
+ 'a|*|a', 0,
+ 'a|a|*', 1,
+ '*|*|x', 1,
+
+ # ^ pair …
+ '||', 0,
+ '|i|', 0,
+ '|*|', 0,
+
+ '||x', 0,
+ '|i|x', 0,
+ '|*|x', 0,
+ '|*||i|', 0,
+ '|*|*', 1,
+ '|*||i|x', 0,
+
+ # ^ pair … trailing sep
+ 'p|*e*rl|w**e|*ekly|*x*|', 4,
+ 'p|*e*rl|w**e|*ekly||', 2,
+ 'th|ewe|e**|k|l***ych|alleng|e|', 5,
+ 'th|ewe|e**|k|l***ych|alleng|*e|', 6,
+ 'th|ewe|e**|k|l***ych|alleng|*e|e', 6,
+ 'th|ewe|e**|k|l***ych|alleng|*e|*', 7,
+
+ # given
+ 'p|*e*rl|w**e|*ekly|', 2,
+ 'perl', 0,
+ 'th|ewe|e**|k|l***ych|alleng|e', 5,
+;
+plan @Test ÷ 2;
+
+#use Grammar::Tracer;
+
+grammar pair-exclus {
+ rule TOP { # The idea is to never re-traverse an <outer> …
+ ^ [ # shorts
+ || $
+ || <sep> $
+ || <sep> <outer> $
+ || <outer> [ $ || <sep> $ || <sep> <outer> ] $
+ ]
+ ||
+ [
+ || [ <pair>+ <outer>? ]+ [ <sep> <outer>? ]? $
+ || <outer> [ <pair>+ <outer>? ]+ [ <sep> <outer>? ]? $
+ ]
+ }
+ rule pair { <sep> <inner>? <sep> }
+ token inner { <-[ | ]>+ }
+ token outer { <-[ | ]>+ } # Rename/alias text w/o capture?
+ rule text { <-[ | ]>+ } # How to use named regex here?
+ token sep { '|' }
+}
+
+class Count {
+ # … so that no containing rule needs to filter <outer>.made
+ my $char-sot = '*';
+ method TOP($/) { make sum $<outer>».made }
+ method outer($/) { make $/.comb.grep( $char-sot ).elems }
+}
+
+multi task( Any:U $data ) { die 'No match' }
+multi task( $data -->Int) {
+ my $match = pair-exclus.parse( $data, :actions(Count.new ));
+ $match.made
+}
+for @Test -> $data, $exp {
+ is task( $data), $exp, "$exp <~∘ $data";
+}
+
+done-testing;
+
+my $str = 'th|ewe|e**|k|l***ych|alleng|*e|';
+
+say "\nInput: \$str = $str.raku()\nOutput: &task( $str)"