diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-03 09:02:02 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-03 09:02:02 +0000 |
| commit | 8e2841eea27caa7be1a2f925c425935c345d2c18 (patch) | |
| tree | 136f5e11d6b64ee803fd5a2c476bcc92b5788a85 | |
| parent | f2f7baf7ce7a718353d485b765dac3eb15851a9a (diff) | |
| parent | 0437ee5479926b86b3efa67990764b02d17667fa (diff) | |
| download | perlweeklychallenge-club-8e2841eea27caa7be1a2f925c425935c345d2c18.tar.gz perlweeklychallenge-club-8e2841eea27caa7be1a2f925c425935c345d2c18.tar.bz2 perlweeklychallenge-club-8e2841eea27caa7be1a2f925c425935c345d2c18.zip | |
Merge pull request #9681 from 0rir/work
257 258
| -rw-r--r-- | challenge-257/0rir/raku/ch-1.raku | 53 | ||||
| -rw-r--r-- | challenge-257/0rir/raku/ch-2.raku | 170 | ||||
| -rw-r--r-- | challenge-258/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-258/0rir/raku/ch-2.raku | 60 |
4 files changed, 333 insertions, 0 deletions
diff --git a/challenge-257/0rir/raku/ch-1.raku b/challenge-257/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..50d26218c1 --- /dev/null +++ b/challenge-257/0rir/raku/ch-1.raku @@ -0,0 +1,53 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +257-1: Smaller than Current Submitted by: Mohammad Sajid Anwar +You are given a array of integers, @ints. + +Write a script to find out how many integers are smaller than current i.e. foreach ints[i], count ints[j] < ints[i] where i != j. + +Example 1 +Input: @ints = (5, 2, 1, 6) +Output: (2, 1, 0, 3) + +For $ints[0] = 5, there are two integers (2,1) smaller than 5. +For $ints[1] = 2, there is one integer (1) smaller than 2. +For $ints[2] = 1, there is none integer smaller than 1. +For $ints[3] = 6, there are three integers (5,2,1) smaller than 6. +Example 2 +Input: @ints = (1, 2, 0, 3) +Output: (1, 2, 0, 3) +Example 3 +Input: @ints = (0, 1) +Output: (0, 1) +Example 4 +Input: @ints = (9, 4, 9, 2) +Output: (2, 1, 2, 0) +=end comment + +my @Test = + # @int out + (5, 2, 1, 6), (2, 1, 0, 3), + (1, 2, 0, 3), (1, 2, 0, 3), + (0, 1), (0, 1), + (9, 4, 9, 2), (2, 1, 2, 0), + (0, 9, -9, 4, 9, 2), (1, 4, 0, 3, 4, 2), +; +plan @Test ÷ 2; + +sub func( $a -->List ) { + do for @$a -> $c { +grep * < $c, @$a } +} + +for @Test -> @in, @exp { + is func(@in), @exp, "@exp[] <- @in[]"; +} + +done-testing; + +my $int = @Test[0]; +say "\nInput: @int = @$int.raku()\nOutput: ", &func($int).raku; + diff --git a/challenge-257/0rir/raku/ch-2.raku b/challenge-257/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..18cddbe140 --- /dev/null +++ b/challenge-257/0rir/raku/ch-2.raku @@ -0,0 +1,170 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +257-2: Reduced Row Echelon Submitted by: Ali Moradi + +Given a matrix M, check whether the matrix is in reduced row echelon form. + +A matrix must have the following properties to be in reduced row echelon form: + +1. If a row does not consist entirely of zeros, then the first + nonzero number in the row is a 1. We call this the leading 1. +2. If there are any rows that consist entirely of zeros, then + they are grouped together at the bottom of the matrix. +3. In any two successive rows that do not consist entirely of zeros, + the leading 1 in the lower row occurs farther to the right than + the leading 1 in the higher row. +4. Each column that contains a leading 1 has zeros everywhere else + in that column. + +For example: + +[ + [1,0,0,1], + [0,1,0,2], + [0,0,1,3] +] +The above matrix is in reduced row echelon form since the first nonzero number in each row is a 1, leading 1s in each successive row are farther to the right, and above and below each leading 1 there are only zeros. + +For more information check out this wikipedia article. + +Example 1 + Input: $M = [ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] + ] + Output: 0 +Example 2 + Input: $M = [ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ] + Output: 1 +Example 3 + Input: $M = [ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] + ] + Output: 1 +Example 4 + Input: $M = [ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] + ] + Output: 0 +Example 5 + Input: $M = [ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] + ] + Output: 0 +Example 6 + Input: $M = [ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] + ] + Output: 0 + +=end comment + +my @Test = + # $M out + [ [0], [0], [0] ], True, + [ [0,0,0], [0,0,0], [0,0,0], [0,0,0] ], True, + [ [0,0,0], [0,0,0], [0,0,0], [0,0,1] ], False, + [ [0, 1,-2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ], + True, + [ [1,0,0], [0,1,0], [0,0,1], [0,0,0] ], True, + [ [1,0,0], [0,1,0], [0,0,1], [0,0,1] ], False, + [ [1,0,1,1], [0,0,1,5], [0,0,0,0] ], False, + + [ [0, 1,-2, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 1], [0, 0, 0, 0, 0] ], + False, + [ [1, 1, 0], [0, 1, 0], [0, 0, 0] ], False, + [ [0, 1,-2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0] ], + False, + [ [0, 1, 0], [1, 0, 0], [0, 0, 0] ], + False, + [ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1,-1] ], False, + [ [1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1,-1] ], True, +; + +my @Die = + [], + [[],], + [ [0,0], [0,0,0],] +; +plan @Test ÷ 2 + @Die; + +multi func( @a where * ~~ Empty) { die 'Empty matix.' } +multi func( @a where @a[0] ~~ Empty) { die 'Matix of Empty.' } # not Whatever +multi func( @a, $state? -->Bool) { + die 'Not a matrix.' unless @a[0].end == all @a[0..^@a]».end; + my $prev-first = -1; # last index for valid leading non-zed + my @leader-col; # index of columns w/ valid leading non-zed + my $idx = 0; # current work row + + for ^@a -> $i { # check row by row + my @row := @a[$i]; + my $k = @row.first: * ≠ 0, :k; + + if $k.defined { + return False if @row[$k] ≠ 1; # invalid 1st non-zed elem + return False if $k ≤ $prev-first; # stair broken + $prev-first = $k; + @leader-col.push: $k; # collect columns with non-zed leaders + return True if $i == @a.end; + } else { + $idx = $i; + last; + } + } + # hit an all zed row at $idx + for 1+$idx..^@a { + return False if @a[$_].first( * ≠ 0, :k ).defined; # all-zed ! at end + } + # row issues done + my @col = ( ( [Z] @a)>>.Array).Array; # rotate + + # fail if a leader column has multiple ones + return False if +@col[@leader-col].grep( { + .grep( * == 1 ) ≠ 1 }); + + True +} + + +for @Test -> @in, $exp { + is func(@in), $exp, "$exp <- @in.raku()"; +} + +for @Die -> @in { + dies-ok { func(@in)}, "@in.raku() dies ok"; +} + +done-testing; +my $M = [ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1,-1] ]; + +say "\n Input: \$M = [\n&rprint(12, @$M)" + ~" ]\n Output: &func(@$M)"; + +sub rprint( Int $indent, @a -->Str) { + my $s; + for @a -> @r { + $s ~= " " x $indent ~ @r.raku ~ "\n" ; + } + $s; +} +exit; + diff --git a/challenge-258/0rir/raku/ch-1.raku b/challenge-258/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..7d7f0e412c --- /dev/null +++ b/challenge-258/0rir/raku/ch-1.raku @@ -0,0 +1,50 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +258-1: Count Even Digits Number Submitted by: Mohammad Sajid Anwar +You are given a array of positive integers, @ints. + +Write a script to find out how many integers have even number of digits. + +Example 1 +Input: @ints = (10, 1, 111, 24, 1000) +Output: 3 + +There are 3 integers having even digits i.e. 10, 24 and 1000. +Example 2 +Input: @ints = (111, 1, 11111) +Output: 0 +Example 3 +Input: @ints = (2, 8, 1024, 256) +Output: 1 +=end comment + +my @Test = + # @int $exp + (), 0, # c/b Int + (1), 0, + (22), 1, + (10, 1, 111, 24, 1000), 3, + (111, 1, 11111), 0, + (2, 8, 1024, 256), 1, +; +plan @Test ÷ 2; + +sub func( $a) { + +@$a.grep( *.Str.chars %% 2 ) +} + +for @Test -> $in, $exp { + is func(@$in), $exp, "$exp <- $in"; +} + +done-testing; +my $X = (2, 8, 1024, 256); + +say "\nInput: @int = @$X.raku()\nOutput: &func($X)"; + +exit; + diff --git a/challenge-258/0rir/raku/ch-2.raku b/challenge-258/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..5974d7cca7 --- /dev/null +++ b/challenge-258/0rir/raku/ch-2.raku @@ -0,0 +1,60 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6; +use Test; + +=begin comment +258-2: Sum of Values Submitted by: Mohammad Sajid Anwar +You are given an array of integers, @int and an integer $k. + +Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set. + +Example 1 +Input: @ints = (2, 5, 9, 11, 3), $k = 1 +Output: 17 + +Binary representation of index 0 = 0 +Binary representation of index 1 = 1 +Binary representation of index 2 = 10 +Binary representation of index 3 = 11 +Binary representation of index 4 = 100 + +So the indices 1, 2 and 4 have total one 1-bit sets. +Therefore the sum, $ints[1] + $ints[2] + $ints[4] = 17 +Example 2 +Input: @ints = (2, 5, 9, 11, 3), $k = 2 +Output: 11 +Example 3 +Input: @ints = (2, 5, 9, 11, 3), $k = 0 +Output: 2 + +=end comment + +my @Test = + # @int k exp + (2, 5, 9, 11, 3), 1, 17, + (2, 5, 9, 11, 3), 2, 11, + (2, 5, 9, 11, 3), 0, 2, + (2, 5, 9, 11, 3), 3, 0, +; + +plan @Test ÷ 3; + +multi func( $k where * == 0, @a ) { @a[0] } + +multi func( $k, @a ) { + [+] @a[ ^@a .grep( $k == *.base(2).comb.grep('1')) ]; +} + +for @Test -> @in, $k, $exp { + is func($k, @in), $exp, "$exp\t<- $k K\t@in[]"; +} + +done-testing; +my @int = ( 0..25); +my $k = 4; + +say "\nInput: @int = @int.raku(), \$k = $k\nOutput: &func($k, @int)"; + +exit; + |
