diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-09-24 22:26:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-24 22:26:03 +0100 |
| commit | d7b6db2ca3002002a4b87ff8e344b520a888f061 (patch) | |
| tree | 8d0ebc270a9c16cd846a6f287c1e888ac82f43ed | |
| parent | e454cd12843354e880cab8a361c6357357f8c785 (diff) | |
| parent | 1421cd1ee638aaa6eecc48c0cb0dcd41deec5e85 (diff) | |
| download | perlweeklychallenge-club-d7b6db2ca3002002a4b87ff8e344b520a888f061.tar.gz perlweeklychallenge-club-d7b6db2ca3002002a4b87ff8e344b520a888f061.tar.bz2 perlweeklychallenge-club-d7b6db2ca3002002a4b87ff8e344b520a888f061.zip | |
Merge pull request #8760 from Util/c235
Add TWC 235 solutions by Bruce Gray (Raku only).
| -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; +} |
