diff options
| author | rir <rirans@comcast.net> | 2022-09-01 22:41:21 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2022-09-01 22:41:21 -0400 |
| commit | 8f48164b8ca4b8bf8db2c1a6e9350791841faed9 (patch) | |
| tree | 4a2d13d844cb9fa96f01452fc3e0170948cf8fa9 | |
| parent | d6d94116137085b32d33203abd12cc9caaf1a2b9 (diff) | |
| download | perlweeklychallenge-club-8f48164b8ca4b8bf8db2c1a6e9350791841faed9.tar.gz perlweeklychallenge-club-8f48164b8ca4b8bf8db2c1a6e9350791841faed9.tar.bz2 perlweeklychallenge-club-8f48164b8ca4b8bf8db2c1a6e9350791841faed9.zip | |
180
| -rw-r--r-- | challenge-180/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-180/0rir/raku/ch-2.raku | 46 |
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(','), ')'; +} + |
