diff options
| author | rir <rirans@comcast.net> | 2024-05-16 13:07:53 -0400 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-05-16 13:09:03 -0400 |
| commit | 2fe588efb985a305e5b218ab224c3f33145c63ce (patch) | |
| tree | 2a9d9d5588b8dacd5fb8083bdb958bad7fa99281 | |
| parent | 71c7bc98dd417eea21d8d731b35edb89bdd153f9 (diff) | |
| download | perlweeklychallenge-club-2fe588efb985a305e5b218ab224c3f33145c63ce.tar.gz perlweeklychallenge-club-2fe588efb985a305e5b218ab224c3f33145c63ce.tar.bz2 perlweeklychallenge-club-2fe588efb985a305e5b218ab224c3f33145c63ce.zip | |
269
| -rw-r--r-- | challenge-269/0rir/raku/ch-1.raku | 71 | ||||
| -rw-r--r-- | challenge-269/0rir/raku/ch-2.raku | 127 |
2 files changed, 198 insertions, 0 deletions
diff --git a/challenge-269/0rir/raku/ch-1.raku b/challenge-269/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..1bced781bf --- /dev/null +++ b/challenge-269/0rir/raku/ch-1.raku @@ -0,0 +1,71 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; +use Test; + +=begin comment +269-1: Bitwise OR +Submitted by: Mohammad Sajid Anwar +You are given an array of positive integers, @ints. + +Write a script to find out if it is possible to select two or more elements of the given array such that the bitwise OR of the selected elements has atlest one trailing zero in its binary representation. + +Example 1 +Input: @ints = (1, 2, 3, 4, 5) +Output: true + +Say, we pick 2 and 4, thier bitwise OR is 6. The binary representation of 6 is 110. +Return true since we have one trailing zero. +Example 2 +Input: @ints = (2, 3, 8, 16) +Output: true + +Say, we pick 2 and 8, thier bitwise OR is 10. The binary representation of 10 is 1010. +Return true since we have one trailing zero. +Example 3 +Input: @ints = (1, 2, 5, 7, 9) +Output: false + + +=end comment + +my @Test = + (), False, + (1,), False, + (2,), False, + (2,10), True, + (2,1), False, + (2, 4, 8, 16, 32), True, + (1, 3, 7, 16, 32), True, + (1, 2, 3, 4, 5), True, + (2, 3, 8, 16), True, + (1, 2, 5, 7, 9), False, + flat( (1,3,5,7...4999), 4999, (* + 2) ...9_999), False, # 1 + flat(2, (1,3,5,7...4999), 4999, (* + 2) ...9_997), False, # 1 +; +plan +@Test; + +sub func-first2( @a -->Bool) { + my $first = @a.first( * %% 2, :k ) // return False; + @a[$first^..^@a].first( * %% 2, :k ) // return False; + True; +} + +sub func-grep( @a -->Bool) { + return True if @a.grep( so * %% 2) > 1; + False; +} +sub summary( @a -->Str) { + @a.gist.chars < 26 ?? @a.gist + !! @a.gist.substr(0,25).trim ~ '…' +} +for @Test -> @in, $exp { + is func-first2(@in), $exp, "$exp <- &summary(@in)"; + is func-grep( @in), $exp, "$exp <- &summary(@in)"; +} +done-testing; + +my @int = (1, 2, 5, 7, 9, 11, 13, 15, 2000000); + +say "\nInput: @int = @int[]\nOutput: &func-grep( @int)"; + diff --git a/challenge-269/0rir/raku/ch-2.raku b/challenge-269/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..7fed40c894 --- /dev/null +++ b/challenge-269/0rir/raku/ch-2.raku @@ -0,0 +1,127 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ « » ∴ +use v6.d; use Test; + +=begin comment + +This spec is questionable regarding the reduction of @ints to an empty +array. Step 2 indicates preservation, which is contradicted by the following +rule; a trivial change in Raku. + +269-2: Distribute Elements Submitted by: Mohammad Sajid Anwar (Edited) + +You are given an array of distinct integers, @ints. + +Write a script to distribute the elements as described below: + +1) Put @ints[0] into @arr1. +2) Put @ints[1] into @arr2. + +Then follow the rule: If @arr1.tail > @arr2.tail then add @ints[0] to +@arr1 otherwise to @arr2. + +When done distribution, return the concatenated arrays. @arr1 and @arr2. + +Example 1 +Input: @ints = (2, 1, 3, 4, 5) +Output: (2, 3, 4, 5, 1) + +1st operation: +Add 1 to @arr1 = (2) + +2nd operation: +Add 2 to @arr2 = (1) + +3rd operation: +Now the last element of @arr1 is greater than the last element +of @arr2, add 3 to @arr1 = (2, 3). + +4th operation: +Again the last element of @arr1 is greate than the last element +of @arr2, add 4 to @arr1 = (2, 3, 4) + +5th operation: +Finally, the last element of @arr1 is again greater than the last +element of @arr2, add 5 to @arr1 = (2, 3, 4, 5) + +Mow we have two arrays: +@arr1 = (2, 3, 4, 5) +@arr2 = (1) + +Concatenate the two arrays and return the final array: (2, 3, 4, 5, 1). +Example 2 +Input: @ints = (3, 2, 4) +Output: (3, 4, 2) + +1st operation: +Add 1 to @arr1 = (3) + +2nd operation: +Add 2 to @arr2 = (2) + +3rd operation: +Now the last element of @arr1 is greater than the last element +of @arr2, add 4 to @arr1 = (3, 4). + +Mow we have two arrays: +@arr1 = (3, 4) +@arr2 = (2) + +Concatenate the two arrays and return the final array: (3, 4, 2). +Example 3 +Input: @ints = (5, 4, 3 ,8) +Output: (5, 3, 4, 8) + +1st operation: +Add 1 to @arr1 = (5) + +2nd operation: +Add 2 to @arr2 = (4) + +3rd operation: +Now the last element of @arr1 is greater than the last element +of @arr2, add 3 to @arr1 = (5, 3). + +4th operation: +Again the last element of @arr2 is greate than the last element +of @arr1, add 8 to @arr2 = (4, 8) + +Mow we have two arrays: +@arr1 = (5, 3) +@arr2 = (4, 8) + +Concatenate the two arrays and return the final array: (5, 3, 4, 8). + +=end comment + +my @Test = + [], [], + [0], [0], + [1], [1], + [1,2], [1,2], + [5, 4, 3, 8], [5, 3, 4, 8], + [3, 2, 4], [3, 4, 2], + [2, 1, 3, 4, 5], [2, 3, 4, 5, 1], +; + +plan @Test ÷ 2; + +multi func( [] ) { []} +multi func( @a is copy where +@a ≤ 1 ) { @a } + +multi func( @a is copy where @a ~~ @a.unique -->Array) { + $_ := @a; + my @l .push: .shift; + my @r .push: .shift; + while @a { ( @l.tail > @r.tail ?? @l !! @r ).push: .shift; } + @l.append: @r; +} + +for @Test -> @in, @exp { + is func(@in), @exp, "@exp.raku() <- @in.raku()"; +} +done-testing; + +my @x = [2, 1, 3, 7, 30, 4, 9, -100, 5]; + +say "\nInput: @int = @x.raku()\nOutput: &func( @x).raku()"; |
