aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-02 10:55:03 +0100
committerGitHub <noreply@github.com>2022-09-02 10:55:03 +0100
commit441b44873d9991c5bbd47d2262bf2cc53d64b07a (patch)
tree0c4e24716f8879fcfe3b76b9c0bdaa2bad47d645
parent3d4d7411c1f8d44ebcb3027b5d2bf520afdf3213 (diff)
parent8f48164b8ca4b8bf8db2c1a6e9350791841faed9 (diff)
downloadperlweeklychallenge-club-441b44873d9991c5bbd47d2262bf2cc53d64b07a.tar.gz
perlweeklychallenge-club-441b44873d9991c5bbd47d2262bf2cc53d64b07a.tar.bz2
perlweeklychallenge-club-441b44873d9991c5bbd47d2262bf2cc53d64b07a.zip
Merge pull request #6682 from 0rir/180
180
-rw-r--r--challenge-180/0rir/raku/ch-1.raku50
-rw-r--r--challenge-180/0rir/raku/ch-2.raku46
2 files changed, 96 insertions, 0 deletions
diff --git a/challenge-180/0rir/raku/ch-1.raku b/challenge-180/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..19d1a03046
--- /dev/null
+++ b/challenge-180/0rir/raku/ch-1.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢
+use v6.d;
+use Test;
+constant TEST=False;
+
+=begin comment
+Task 1: First Unique Character Submitted by: Mohammad S Anwar
+
+Given a string, $s, find the first unique character in the
+string and print its index (0-based).
+
+Examples:
+Input: $s = "Perl Weekly Challenge"
+Output: 0 as 'P' is the first unique character
+
+Input: $s = "Long Live Perl"
+Output: 1 as 'o' is the first unique character
+=end comment
+
+sub first-unique-char ( Str $s is copy, Bool :$i --> Int ) {
+ $s = $s.fc if $i;
+ my $b = Bag.new: my @c = $s.comb();
+ @c.first( { $b{$_} === 1}, :k );
+}
+
+if TEST {
+ my @Test =
+ { in => 'abcdeA', exp => 0, iexp => 1 },
+ { in => 'abcdeabcD', exp => 3, iexp => 4 },
+ { in => 'abcddefabcEfg', exp => 5, iexp => 12 },
+ ;
+
+ plan 2 × @Test.elems;
+ for @Test -> %t {
+ is first-unique-char(%t<in>), %t<exp>, "%t<in>";
+ is first-unique-char(%t<in>, :i ), %t<iexp>, "%t<in> :i";
+ }
+ done-testing;
+ exit;
+}
+
+sub MAIN( Str $s = 'Raku Yearly Challenge' ) {
+ my $k = first-unique-char($s);
+ say qq{Input: \$s = "$s"\n},
+ qq{Output: $k as '$s.substr($k, 1)' is the first unique character};
+ $k = first-unique-char($s, :i );
+ say qq{Or : $k as '$s.substr($k, 1)' is 1st if case is ignored.};
+}
+
diff --git a/challenge-180/0rir/raku/ch-2.raku b/challenge-180/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..acaffda37b
--- /dev/null
+++ b/challenge-180/0rir/raku/ch-2.raku
@@ -0,0 +1,46 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢
+use v6.d;
+use Test;
+
+constant TEST=False;
+
+=begin comment
+Task 2: Trim List Submitted by: Mohammad S Anwar
+Given a list of numbers, @n and an integer $i, trim the list where
+element is less than or equal to the given integer.
+
+NOTE: The 1st example conflicts with the specification given; the second
+doesn't clarify. Precedence is given to the specs.
+
+Example 1
+Input: @n = (1,4,2,3,5) and $i = 3
+Output: (4,3,5)
+Example 2
+Input: @n = (9,0,6,2,3,8,5) and $i = 4
+Output: (9,6,8,5)
+=end comment
+
+sub gt-grep( Int $limit, List $bucket) {
+ $bucket.grep: { $_ > $limit};
+}
+
+if TEST {
+ my @Test =
+ { in => ( 5, ( 5,)) , exp => () },
+ { in => ( 3, (1, 4, 2, 3, 5)), exp => ( 4, 5) },
+ { in => ( 4, (9, 0, 6, 2, 3, 8, 5)) , exp => ( 9, 6, 8, 5) }, ;
+
+ plan @Test.elems;
+ for @Test -> %t {
+ is gt-grep( %t<in>[0], %t<in>[1]), %t<exp>, "%t<in>";
+ }
+ done-testing;
+ exit;
+}
+
+sub MAIN( $i = 5, @n = (1,9,0,6,2,3,8,5,104,) ) {
+ say 'Input: @n = (', @n.join(','), ') and $i = ',$i ;
+ say 'Output: (', gt-grep( $i, @n).join(','), ')';
+}
+