diff options
| author | Simon Green <mail@simon.green> | 2024-02-25 21:46:09 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2024-02-25 21:46:09 +1100 |
| commit | 34cd487293ab2d6fbe7c87773b616611e5fe5543 (patch) | |
| tree | db2d65ab830e5c6ca2da89bc1b7b5db1798ea97a | |
| parent | d56f5846adcf3864f7b9dd2426d85ae68579729e (diff) | |
| download | perlweeklychallenge-club-34cd487293ab2d6fbe7c87773b616611e5fe5543.tar.gz perlweeklychallenge-club-34cd487293ab2d6fbe7c87773b616611e5fe5543.tar.bz2 perlweeklychallenge-club-34cd487293ab2d6fbe7c87773b616611e5fe5543.zip | |
Simon's solution to challenge 257
| -rw-r--r-- | challenge-257/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-257/sgreen/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-257/sgreen/perl/ch-2.pl | 81 | ||||
| -rwxr-xr-x | challenge-257/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-257/sgreen/python/ch-2.py | 65 | ||||
| -rwxr-xr-x | challenge-257/sgreen/python/test.py | 26 |
6 files changed, 217 insertions, 0 deletions
diff --git a/challenge-257/sgreen/blog.txt b/challenge-257/sgreen/blog.txt new file mode 100644 index 0000000000..e861b203a1 --- /dev/null +++ b/challenge-257/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/the-current-echelon-33gn
\ No newline at end of file diff --git a/challenge-257/sgreen/perl/ch-1.pl b/challenge-257/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..561f556072 --- /dev/null +++ b/challenge-257/sgreen/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub smaller_ints ( $ints, $target ) { + # Count the number of integers that are less than the target + return scalar( grep { $_ < $target } @$ints ); +} + +sub main (@ints) { + my @results = map { smaller_ints( \@ints, $_ ) } @ints; + say '(', join( ', ', @results ), ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-257/sgreen/perl/ch-2.pl b/challenge-257/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..1a02e2fb86 --- /dev/null +++ b/challenge-257/sgreen/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use JSON 'decode_json'; +use List::MoreUtils 'first_index'; +use List::Util qw(all sum); + +sub validate_matrix ($matrix) { + # Calculate the size of the matrix + my $rows = scalar(@$matrix); + my $cols = scalar( @{ $matrix->[0] } ); + + foreach my $r ( 1 .. $#$matrix ) { + # Check that all columns are of equal length + if ( scalar( @{ $matrix->[$r] } ) != $cols ) { + die "Row $r has different number of columns\n"; + } + } +} + +sub echelon ($matrix) { + # Check that the matrix is valid + validate_matrix($matrix); + + # Get the position of the first one in each row + my @leading_one = map { + first_index { $_ == 1 } @$_ + } @$matrix; + + foreach my $row_num ( 0 .. $#$matrix ) { + my $row = $matrix->[$row_num]; + my $leading_one_pos = $leading_one[$row_num]; + + # If the row is all zeros, there is nothing to check + if ( all { $_ == 0 } @$row ) { + next; + } + + # Check the first non zero number is one + foreach my $value (@$row) { + if ( $value == 1 ) { + last; + } + if ( $value != 0 ) { + return 0; + } + } + + # Check if the leading one is further right + if ( $row_num != 0 ) { + if ( $leading_one[ $row_num - 1 ] == -1 ) { + # The previous row was all zeros. + return 0; + } + if ( $leading_one[ $row_num - 1 ] > $leading_one_pos ) { + # The previous first one was further to the right + return 0; + } + } + + # Count the number of non-zero values in the column that is the + # leading one. It should be one (this row) + if ( scalar( grep { $_->[$leading_one_pos] != 0 } @$matrix ) != 1 ) { + return 0; + } + } + + return 1; +} + +sub main ($json_string) { + my $matrix = decode_json($json_string); + say echelon($matrix); + +} + +main( $ARGV[0] );
\ No newline at end of file diff --git a/challenge-257/sgreen/python/ch-1.py b/challenge-257/sgreen/python/ch-1.py new file mode 100755 index 0000000000..f2ca928579 --- /dev/null +++ b/challenge-257/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def smaller_than_current(ints: list) -> list: + """For each integer, how many integers are smaller than current one + + Args: + ints (list): The input list of integers + + Returns: + list: The output list of integers + """ + return [sum(1 for j in ints if j < i) for i in ints] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = smaller_than_current(array) + print('(' + ', '.join(map(str, result)) + ')') + + +if __name__ == '__main__': + main() diff --git a/challenge-257/sgreen/python/ch-2.py b/challenge-257/sgreen/python/ch-2.py new file mode 100755 index 0000000000..04eccc0243 --- /dev/null +++ b/challenge-257/sgreen/python/ch-2.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def validate_matrix(matrix): + # Calculate the size of the matrix + rows = len(matrix) + cols = len(matrix[0]) + + for r in range(1, rows): + # Check that all columns are of equal length + if len(matrix[r]) != cols: + raise ValueError(f'Row {r} has different number of columns') + + +def echelon(matrix: list) -> int: + # Check that the matrix is valid + validate_matrix(matrix) + + # Get the position of the first one in each row + leading_one = [None if 1 not in row else row.index(1) for row in matrix] + + for row_num in range(len(matrix)): + row = matrix[row_num] + leading_one_pos = leading_one[row_num] + + # If the row is all zeros, there is nothing to check + if all(value == 0 for value in row): + continue + + # Check the first non zero number is one + for value in row: + if value == 1: + break + if value != 0: + return 0 + + # Check if the leading one is further right + if row_num != 0: + if leading_one[row_num - 1] is None: + # The previous row was all zeros. + return 0 + if leading_one[row_num - 1] > leading_one_pos: + # The previous first one was further to the right + return 0 + + # Count the number of non-zero values in the column that is the + # leading one. It should be one (this row) + if sum(1 for row in matrix if row[leading_one_pos] != 0) != 1: + return 0 + + return 1 + + +def main(): + # Convert input into a list of list of integers + matrix = json.loads(sys.argv[1]) + result = echelon(matrix) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-257/sgreen/python/test.py b/challenge-257/sgreen/python/test.py new file mode 100755 index 0000000000..2e50bc4231 --- /dev/null +++ b/challenge-257/sgreen/python/test.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual(ch_1.smaller_than_current([5, 2, 1, 6]), [2, 1, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([1, 2, 0, 3]), [1, 2, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([0, 1]), [0, 1]) + self.assertEqual(ch_1.smaller_than_current([9, 4, 9, 2]), [2, 1, 2, 0]) + + def test_ch_2(self): + self.assertEqual(ch_2.echelon( + [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3]]), 1) + self.assertEqual(ch_2.echelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]), 0) + self.assertEqual(ch_2.echelon( + [[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), 1) + self.assertEqual(ch_2.echelon( + [[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), 1) + + +if __name__ == '__main__': + unittest.main() |
