aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2022-09-30 10:44:56 -0400
committerrir <rirans@comcast.net>2022-09-30 11:34:04 -0400
commit00884a1e9f55ff74648baa1a6377a5ec321fdf71 (patch)
tree28392dbf61745d92a3be0cb1a6db9d239330bfb1
parent29012912d8a265f75ca3eca4c79390182f9cac93 (diff)
downloadperlweeklychallenge-club-00884a1e9f55ff74648baa1a6377a5ec321fdf71.tar.gz
perlweeklychallenge-club-00884a1e9f55ff74648baa1a6377a5ec321fdf71.tar.bz2
perlweeklychallenge-club-00884a1e9f55ff74648baa1a6377a5ec321fdf71.zip
184
-rw-r--r--challenge-184/0rir/raku/ch-1.raku76
-rw-r--r--challenge-184/0rir/raku/ch-2.raku93
2 files changed, 169 insertions, 0 deletions
diff --git a/challenge-184/0rir/raku/ch-1.raku b/challenge-184/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..af333de197
--- /dev/null
+++ b/challenge-184/0rir/raku/ch-1.raku
@@ -0,0 +1,76 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢
+use v6.d;
+use Test;
+
+=begin comment
+184-1: Sequence Number Submitted by: Mohammad S Anwar
+Given a list of strings in the format aa9999, i.e. the first 2 characters are
+'a-z' followed by 4 digits '0-9'.
+
+Replace the first two characters with sequence starting with '00', '01', '02',
+etc.
+
+Example 1
+Input: @list = ( 'ab1234', 'cd5678', 'ef1342')
+Output: ('001234', '015678', '021342')
+Example 2
+Input: @list = ( 'pq1122', 'rs3334')
+Output: ('001122', '013334')
+=end comment
+
+=begin spec
+The first occurrence of a prefix establishes its place in the order of prefixes.
+=end spec
+
+constant TEST=True;
+if TEST {
+ my @Test =
+ { in => [ qw<ab1234>], exp => [ qw<001234>] },
+ { in => [ qw<ab1234 cd5678>], exp => [ qw<001234 015678 >] },
+ { in => [ qw<cd5678 ab1234>], exp => [ qw<005678 011234 >] },
+ { in => [ qw<ab1234 cd5678 ef1342>],
+ exp => [ qw<001234 015678 021342>] },
+ { in => [ qw<pq1122 rs3334>], exp => [qw<001122 013334>] },
+ { in => [ qw<aa1234 ab5678 bb1342 ac1234 ab1234> ],
+ exp => [ qw<001234 015678 021342 031234 011234> ] },
+ { in => [ qw<aa0001 ab0002 bb0004 ac0005 ab0003 > ],
+ exp => [ qw<000001 010002 020004 030005 010003> ] },
+ ;
+ my @Poison = 'Empty' => Empty ;
+
+ plan +@Test + @Poison;
+ for @Test -> %t {
+ my @g = convert( %t<in>);
+ is-deeply @g, %t<exp>, " %t<in> --> @g[]";
+ }
+ for @Poison -> %t {
+ dies-ok { convert @%t.value }, "%t.key()";
+ }
+ done-testing;
+}
+
+sub MAIN( @list = ( 'ab1234', 'cd5678', 'ef1342') ) {
+ say "Input: \@list = @list.raku()";
+ say "Output: ", convert(@list).raku();
+}
+
+sub convert( @a where (.defined and (+$_ ≠ 0) ) --> List ) {
+ my @return;
+ my %seen;
+ my $current = -1;
+ my sub see( $s --> Str ) { %seen{$s} = sprintf "%02d", ++$current; }
+ my regex head { <:Ll>**2}
+ my regex tail { <:Nd>**4}
+ for @a -> Str $e {
+ if $e ~~ /:r ^ <head> <tail> $ / {
+ $_ = %seen{$<head>}:exists ?? %seen{$<head>}
+ !! see $<head>;
+ @return.push: $_ ~ $<tail>;
+ } else {
+ die qq{Ill formed } ~ $e;
+ }
+ }
+ return @return.List;
+}
+
diff --git a/challenge-184/0rir/raku/ch-2.raku b/challenge-184/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..5c948ff252
--- /dev/null
+++ b/challenge-184/0rir/raku/ch-2.raku
@@ -0,0 +1,93 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢
+use v6.d;
+use Test;
+
+=begin comment
+184-2: Split Array Submitted by: Mohammad S Anwar
+Given a list of strings containing 0-9 and a-z separated by space only;
+split the data into two arrays, one for integers and one for letters only.
+
+Example 1
+Input: @list = ( 'a 1 2 b 0', '3 c 4 d')
+Output: [[1,2,0], [3,4]] and [['a','b'], ['c','d']]
+Example 2
+Input: @list = ( '1 2', 'p q r', 's 3', '4 5 t')
+Output: [[1,2], [3], [4,5]] and [['p','q','r'], ['s'], ['t']]
+=end comment
+
+sub bifurcatable( @a --> Bool) {
+ my regex bifur { ^ <[ a..z \d]>* % \s $ }
+ for @a -> $s {
+ return False if $s eq '' or $s !~~ /<bifur>/;
+ }
+ True;
+}
+
+sub bifurcate( @a --> Pair ) {
+ return [ [] ] => [ [] ] if @a eqv ();
+ my (@n, @l);
+ for @a -> $s {
+ my @x = ($s.comb.grep: { m/<:Ll>/ } ).Array;
+ @l.push( @x ) if @x.elems;
+
+ my @y = map *.Int, $s.comb.grep( { m/<:Nd>/ } ).Array;
+ @n.push( @y) if @y.elems;
+ }
+ return $@n => @l;
+}
+
+constant TEST=True;
+if TEST {
+ my @Test =
+ { in => (),
+ exp => [] => [], },
+ { in => ( 'a', ),
+ exp => [] => [['a'],], },
+ { in => ( '1', ),
+ exp => [[1],] => [], },
+ { in => ( 'a b c', 'd e f' ),
+ exp => [] => [['a','b','c'],['d','e','f']], },
+ { in => ( '1 2 3', '4 5 6' ),
+ exp => [[1,2,3],[4,5,6]] => [], },
+ { in => ( 'a 1', ),
+ exp => [[1],] => [['a'],], },
+ { in => ( 'a 1 2 b 0', '3 c 4 d'),
+ exp => [[1,2,0], [3,4],] => [['a','b'], ['c','d'],], },
+ { in => ( '1 2', 'p q r', 's 3', '4 5 t'),
+ exp => [[1,2], [3], [4,5],] => [['p','q','r'], ['s'], ['t'],], },
+ ;
+ my @Unbifurcatable =
+ ( '',),
+ ( ' a',),
+ ( 'a ',),
+ ( ' 1',),
+ ( '1 ',),
+ ( '1 2', 'p q r', 's 3', '4 5 t '),
+ ( ',',),
+ ( 'a ] c', 'd e f'),
+ ( 'a *',),
+ ( 'a 1 2 b 0', '3 c ) d'),
+ ( '1 2', 'p + r', 's 3', '4 5 t'),
+ ;
+ plan 2 × @Test.elems + @Unbifurcatable;
+
+ for @Unbifurcatable -> @u {
+ is bifurcatable( @u), False, "unbifurcatable: @u[].raku()";
+ }
+ for @Test -> %t {
+ is bifurcatable( %t<in>), True, "bifurcatable: %t<in><>.raku()";
+ is-deeply bifurcate(%t<in>), %t<exp>, "Pair &bifurcate(%t<in>).raku()";
+ }
+ done-testing;
+}
+
+sub MAIN( @list = ( '1 2', 'p q r', 's 3', '4 5 t')) {
+ die "bad data" unless bifurcatable( @list);
+ my Pair $nl = bifurcate( @list);
+
+ say 'Input: @list = ', @list.raku;
+ say 'Output: ', $nl.key().raku,
+ ' and ', $nl.value().raku;
+}
+