From 00884a1e9f55ff74648baa1a6377a5ec321fdf71 Mon Sep 17 00:00:00 2001 From: rir Date: Fri, 30 Sep 2022 10:44:56 -0400 Subject: 184 --- challenge-184/0rir/raku/ch-1.raku | 76 ++++++++++++++++++++++++++++++++ challenge-184/0rir/raku/ch-2.raku | 93 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 challenge-184/0rir/raku/ch-1.raku create mode 100644 challenge-184/0rir/raku/ch-2.raku 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], exp => [ qw<001234>] }, + { in => [ qw], exp => [ qw<001234 015678 >] }, + { in => [ qw], exp => [ qw<005678 011234 >] }, + { in => [ qw], + exp => [ qw<001234 015678 021342>] }, + { in => [ qw], exp => [qw<001122 013334>] }, + { in => [ qw ], + exp => [ qw<001234 015678 021342 031234 011234> ] }, + { in => [ qw ], + exp => [ qw<000001 010002 020004 030005 010003> ] }, + ; + my @Poison = 'Empty' => Empty ; + + plan +@Test + @Poison; + for @Test -> %t { + my @g = convert( %t); + is-deeply @g, %t, " %t --> @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 ^ $ / { + $_ = %seen{$}:exists ?? %seen{$} + !! see $; + @return.push: $_ ~ $; + } 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 !~~ //; + } + 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), True, "bifurcatable: %t<>.raku()"; + is-deeply bifurcate(%t), %t, "Pair &bifurcate(%t).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; +} + -- cgit