diff options
| -rw-r--r-- | challenge-235/bruce-gray/raku/ch-1.raku | 39 | ||||
| -rw-r--r-- | challenge-235/bruce-gray/raku/ch-2.raku | 14 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-235/bruce-gray/raku/ch-1.raku b/challenge-235/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..9bf0aec65a --- /dev/null +++ b/challenge-235/bruce-gray/raku/ch-1.raku @@ -0,0 +1,39 @@ +sub task1-multi-pass ( @ns --> Bool ) { + return False if [<] @ns; + + sub good_without ( UInt $i ) { [<] all_except($i) } + + sub all_except ( UInt $i ) { |@ns.head($i), + |@ns.skip($i + 1) } + + return @ns.keys + .first( &good_without ) + .defined; +} + +sub task1-single-pass ( @ns --> Bool ) { + return False if @ns.elems == 0; + return True if @ns.elems == 1|2; + + my ($bad_spot, @rest) = @ns.rotor( 2 => -1 ).grep( :k, { .[0] !< .[1] } ); + + return True if $bad_spot.defined.not; + return False if @rest; + + return True if $bad_spot == 0 + or $bad_spot+1 == @ns.end; + + return [<] @ns[$bad_spot-1, $bad_spot+1]; +} + + +my @tests = + ( (0, 2, 9, 4, 6) , True ), + ( (5, 1, 3, 2) , False ), + ( (2, 2, 3) , True ), +; +use Test; plan 2*@tests; +for @tests -> ( @in , $expected ) { + is task1-multi-pass( @in), $expected; + is task1-single-pass(@in), $expected; +} diff --git a/challenge-235/bruce-gray/raku/ch-2.raku b/challenge-235/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..f09bd8ea91 --- /dev/null +++ b/challenge-235/bruce-gray/raku/ch-2.raku @@ -0,0 +1,14 @@ +sub task2 ( @ns ) { + return @ns.map({ $_, (0 if .not) }).flat.head(+@ns); +} + + +my @tests = + ( (1, 0, 2, 3, 0, 4, 5, 0) , (1, 0, 0, 2, 3, 0, 0, 4) ), + ( (1, 2, 3) , (1, 2, 3) ), + ( (0, 3, 0, 4, 5) , (0, 0, 3, 0, 0) ), +; +use Test; plan +@tests; +for @tests -> ( @in , @expected ) { + is-deeply task2(@in), @expected; +} |
