diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-24 13:06:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-24 13:06:53 +0000 |
| commit | d717b4701a454d339cb76a31ac31d0b413099fa2 (patch) | |
| tree | a1edd14f2ed9d58ea31a9d2896a0697603c1813e | |
| parent | 1f225f355f3f5e75660d82e0b6827549d5967f7a (diff) | |
| parent | 4ad7031a5072b7d443dd1977ef7730194a9c01d0 (diff) | |
| download | perlweeklychallenge-club-d717b4701a454d339cb76a31ac31d0b413099fa2.tar.gz perlweeklychallenge-club-d717b4701a454d339cb76a31ac31d0b413099fa2.tar.bz2 perlweeklychallenge-club-d717b4701a454d339cb76a31ac31d0b413099fa2.zip | |
Merge pull request #9635 from LubosKolouch/master
feat(challenge-257/lubos-kolouch/perl,python,raku): Challenge 257 LK Perl Python Raku
| -rw-r--r-- | challenge-257/lubos-kolouch/perl/ch-1.pl | 15 | ||||
| -rw-r--r-- | challenge-257/lubos-kolouch/perl/ch-2.pl | 98 | ||||
| -rw-r--r-- | challenge-257/lubos-kolouch/python/ch-1.py | 12 | ||||
| -rw-r--r-- | challenge-257/lubos-kolouch/python/ch-2.py | 54 | ||||
| -rw-r--r-- | challenge-257/lubos-kolouch/raku/ch-1.raku | 10 | ||||
| -rw-r--r-- | challenge-257/lubos-kolouch/raku/ch-2.raku | 49 |
6 files changed, 238 insertions, 0 deletions
diff --git a/challenge-257/lubos-kolouch/perl/ch-1.pl b/challenge-257/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..3a07e478cc --- /dev/null +++ b/challenge-257/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,15 @@ +use strict; +use warnings; +use feature 'say'; + +sub smaller_than_current { + my (@ints) = @_; + return map { my $x = $_; scalar grep { $_ < $x } @ints } @ints; +} + +# Test cases +use Test::More tests => 4; +is_deeply([smaller_than_current(5, 2, 1, 6)], [2, 1, 0, 3]); +is_deeply([smaller_than_current(1, 2, 0, 3)], [1, 2, 0, 3]); +is_deeply([smaller_than_current(0, 1)], [0, 1]); +is_deeply([smaller_than_current(9, 4, 9, 2)], [2, 1, 2, 0]); diff --git a/challenge-257/lubos-kolouch/perl/ch-2.pl b/challenge-257/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..684138a83d --- /dev/null +++ b/challenge-257/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,98 @@ +use strict; +use warnings; + +sub is_reduced_row_echelon { + my ($matrix_ref) = @_; + my @matrix = @{ $matrix_ref->[0] }; # Adjusting matrix access + my $last_leading = -1; + my $zero_row_encountered = 0; + + for my $row_index ( 0 .. $#matrix ) { + my $row = $matrix[$row_index]; + my $leading = -1; + + for my $i ( 0 .. $#$row ) { + my $element = $row->[$i]; + if ( $element != 0 ) { + if ( $element == 1 ) { + $leading = $i; + last; + } + else { + return 0; + } + } + } + + if ( $leading == -1 ) { + $zero_row_encountered = 1; + next; + } + + if ($zero_row_encountered) { + return 0; + } + + if ( $leading <= $last_leading ) { + return 0; + } + + for my $r_index ( 0 .. $#matrix ) { + next if $r_index == $row_index; + if ( $matrix[$r_index][$leading] != 0 ) { + return 0; + } + } + + $last_leading = $leading; + } + + return 1; +} + +# Test cases +use Test::More; +is( is_reduced_row_echelon( [ [ [ 1, 1, 0 ], [ 0, 1, 0 ], [ 0, 0, 0 ] ] ] ), + 0, "Test 1" ); +is( + is_reduced_row_echelon( + [ + [ + [ 0, 1, -2, 0, 1 ], + [ 0, 0, 0, 1, 3 ], + [ 0, 0, 0, 0, 0 ], + [ 0, 0, 0, 0, 0 ] + ] + ] + ), + 1, "Test 2" +); +is( + is_reduced_row_echelon( + [ [ [ 1, 0, 0, 4 ], [ 0, 1, 0, 7 ], [ 0, 0, 1, -1 ] ] ] + ), + 1, "Test 3" +); +is( + is_reduced_row_echelon( + [ + [ + [ 0, 1, -2, 0, 1 ], + [ 0, 0, 0, 0, 0 ], + [ 0, 0, 0, 1, 3 ], + [ 0, 0, 0, 0, 0 ] + ] + ] + ), + 0, "Test 4" +); +is( is_reduced_row_echelon( [ [ [ 0, 1, 0 ], [ 1, 0, 0 ], [ 0, 0, 0 ] ] ] ), + 0, "Test 5" ); +is( + is_reduced_row_echelon( + [ [ [ 4, 0, 0, 0 ], [ 0, 1, 0, 7 ], [ 0, 0, 1, -1 ] ] ] + ), + 0, "Test 6" +); + +done_testing(); diff --git a/challenge-257/lubos-kolouch/python/ch-1.py b/challenge-257/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..8d4ad6b36e --- /dev/null +++ b/challenge-257/lubos-kolouch/python/ch-1.py @@ -0,0 +1,12 @@ +from typing import List + + +def smaller_than_current(ints: list[int]) -> list[int]: + return [sum(x < y for x in ints) for y in ints] + + +# Test cases +assert smaller_than_current([5, 2, 1, 6]) == [2, 1, 0, 3] +assert smaller_than_current([1, 2, 0, 3]) == [1, 2, 0, 3] +assert smaller_than_current([0, 1]) == [0, 1] +assert smaller_than_current([9, 4, 9, 2]) == [2, 1, 2, 0] diff --git a/challenge-257/lubos-kolouch/python/ch-2.py b/challenge-257/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..a81381f06a --- /dev/null +++ b/challenge-257/lubos-kolouch/python/ch-2.py @@ -0,0 +1,54 @@ +from typing import List + + +def is_reduced_row_echelon(matrix: list[list[int]]) -> int: + last_leading = -1 + zero_row_encountered = False + + for row in matrix: + if all(x == 0 for x in row): + zero_row_encountered = True + continue + + if zero_row_encountered: + return 0 # Non-zero row after a zero row + + try: + leading = next(i for i, x in enumerate(row) if x != 0) + except StopIteration: + continue # Should not happen as zero rows are handled above + + # Check for leading 1 and correct position + if row[leading] != 1 or leading <= last_leading: + return 0 + + # Check other elements in leading 1's column + if any( + matrix[i][leading] != 0 + for i in range(len(matrix)) + if i != matrix.index(row) + ): + return 0 + + last_leading = leading + + return 1 + + +# Test cases +assert is_reduced_row_echelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]) == 0 +assert ( + is_reduced_row_echelon( + [[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] + ) + == 1 +) +assert is_reduced_row_echelon([[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]) == 1 +assert ( + is_reduced_row_echelon( + [[0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0]] + ) + == 0 +) +assert is_reduced_row_echelon([[0, 1, 0], [1, 0, 0], [0, 0, 0]]) == 0 +assert is_reduced_row_echelon([[4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1]]) == 0 diff --git a/challenge-257/lubos-kolouch/raku/ch-1.raku b/challenge-257/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..0b1c404e3e --- /dev/null +++ b/challenge-257/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,10 @@ +sub smaller-than-current(@ints) { + return @ints.map({ my $x = $_; @ints.grep(* < $x).elems }); +} + +# Test cases +use Test; +is-deeply smaller-than-current([5, 2, 1, 6]), (2, 1, 0, 3); +is-deeply smaller-than-current([1, 2, 0, 3]), (1, 2, 0, 3); +is-deeply smaller-than-current([0, 1]), (0, 1); +is-deeply smaller-than-current([9, 4, 9, 2]), (2, 1, 2, 0); diff --git a/challenge-257/lubos-kolouch/raku/ch-2.raku b/challenge-257/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..d48d0f5fa5 --- /dev/null +++ b/challenge-257/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,49 @@ +sub is-reduced-row-echelon(@matrix) { + my $last-leading = -1; + my $zero-row-encountered = False; + + for @matrix.kv -> $row-index, @row { + my $leading = -1; + + for @row.kv -> $i, $element { + if $element != 0 { + $leading = $i; + last if $element == 1; + return 0; # First non-zero element is not 1 + } + } + + if $leading == -1 { + $zero-row-encountered = True; + next; + } + + if $zero-row-encountered { + return 0; # Non-zero row after zero row + } + + if $leading <= $last-leading { + return 0; # Leading 1 in the wrong position + } + + for @matrix -> @r { + next if @r === @row; + return 0 if @r[$leading] != 0; + } + + $last-leading = $leading; + } + + return 1; +} + +# Test cases +use Test; +is is-reduced-row-echelon([ [1, 1, 0], [0, 1, 0], [0, 0, 0] ]), 0, "Test 1"; +is is-reduced-row-echelon([ [0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0] ]), 1, "Test 2"; +is is-reduced-row-echelon([ [1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1] ]), 1, "Test 3"; +is is-reduced-row-echelon([ [0, 1, -2, 0, 1], [0, 0, 0, 0, 0], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0] ]), 0, "Test 4"; +is is-reduced-row-echelon([ [0, 1, 0], [1, 0, 0], [0, 0, 0] ]), 0, "Test 5"; +is is-reduced-row-echelon([ [4, 0, 0, 0], [0, 1, 0, 7], [0, 0, 1, -1] ]), 0, "Test 6"; + +done-testing; |
