aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-28 13:25:38 +0000
committerGitHub <noreply@github.com>2024-01-28 13:25:38 +0000
commit6e58a96789fc10935482b93c5dfaefc93df092ad (patch)
treef57715944d4daf62e0aae3cd8bc9a50801e6f38e
parent25febf7782e877e67f19a11f53aedb82256714a7 (diff)
parent7018e1db2d362e4b31cee9ec0c62d43ad0439bd2 (diff)
downloadperlweeklychallenge-club-6e58a96789fc10935482b93c5dfaefc93df092ad.tar.gz
perlweeklychallenge-club-6e58a96789fc10935482b93c5dfaefc93df092ad.tar.bz2
perlweeklychallenge-club-6e58a96789fc10935482b93c5dfaefc93df092ad.zip
Merge pull request #9470 from simongreen-net/master
Simon's solution to challenge 253
-rw-r--r--challenge-253/sgreen/README.md4
-rw-r--r--challenge-253/sgreen/blog.txt1
-rwxr-xr-xchallenge-253/sgreen/perl/ch-1.pl30
-rwxr-xr-xchallenge-253/sgreen/perl/ch-2.pl53
-rwxr-xr-xchallenge-253/sgreen/python/ch-1.py20
-rwxr-xr-xchallenge-253/sgreen/python/ch-2.py41
6 files changed, 147 insertions, 2 deletions
diff --git a/challenge-253/sgreen/README.md b/challenge-253/sgreen/README.md
index b1dcdb1358..4f9fe5e116 100644
--- a/challenge-253/sgreen/README.md
+++ b/challenge-253/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 252
+# The Weekly Challenge 253
-Blog: [Special zeros](https://dev.to/simongreennet/special-zeros-17i5)
+Blog: [The weakest split](https://dev.to/simongreennet/the-weakest-split-5ad7)
diff --git a/challenge-253/sgreen/blog.txt b/challenge-253/sgreen/blog.txt
new file mode 100644
index 0000000000..08d956de83
--- /dev/null
+++ b/challenge-253/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/the-weakest-split-5ad7 \ No newline at end of file
diff --git a/challenge-253/sgreen/perl/ch-1.pl b/challenge-253/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..3b90ef1fdc
--- /dev/null
+++ b/challenge-253/sgreen/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub split_strings ( $words, $sep ) {
+ # Join the words together by the separator
+ my $combined = join( $sep, @$words );
+
+ # Some characters next to be escaped in perl. From
+ # https://perldoc.perl.org/perlrequick
+ if ( index( '{}[]()^$.|*+?\\', $sep ) != -1 ) {
+ $sep = "\\$sep";
+ }
+
+ # Split them by the separator excluding empty strings
+ my @result = grep { length($_) } split( /$sep/, $combined );
+ return \@result;
+}
+
+sub main (@inputs) {
+ # The separator is the last character
+ my $sep = pop(@inputs);
+ my $result = split_strings( \@inputs, $sep );
+ say '"', join( '", "', @$result ), '"';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-253/sgreen/perl/ch-2.pl b/challenge-253/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..690d3aad22
--- /dev/null
+++ b/challenge-253/sgreen/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use JSON 'decode_json';
+use List::Util qw(any sum);
+
+sub validate_matrix ($matrix) {
+ # Calculate the size of the matrix
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+
+ foreach my $r ( 0 .. $#$matrix ) {
+ # Check that all columns are of equal length
+ if ( scalar( @{ $matrix->[$r] } ) != $cols ) {
+ die "Row $r has different number of columns\n";
+ }
+
+ # Check all values are 0 or 1
+ if ( any { $_ != 0 and $_ != 1 } @{ $matrix->[$r] } ) {
+ die "Row $r has a value other than 0 or 1\n";
+ }
+
+ # Check there are no ones zeros after zero.
+ if ( any { $matrix->[$r][ $_ - 1 ] == 0 and $matrix->[$r][$_] == 1 }
+ ( 1 .. $cols - 1 ) )
+ {
+ die "Row $r has a 1 after 0\n";
+ }
+ }
+}
+
+sub weakest_row ($matrix) {
+ # Start by validating any issues with the input
+ validate_matrix($matrix);
+
+ # Sort by the rows with the most 1s, followed by a numerical sort
+ my @sorted = sort {
+ sum( @{ $matrix->[$a] } ) <=> sum( @{ $matrix->[$b] } ) || $a <=> $b
+ } ( 0 .. $#$matrix );
+ return \@sorted;
+}
+
+sub main ($json_string) {
+ my $matrix = decode_json($json_string);
+ my $result = weakest_row($matrix);
+ say '(', join( ', ', @$result ), ')';
+}
+
+main( $ARGV[0] ); \ No newline at end of file
diff --git a/challenge-253/sgreen/python/ch-1.py b/challenge-253/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..661c767134
--- /dev/null
+++ b/challenge-253/sgreen/python/ch-1.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def split_strings(words, sep):
+ # Join the words together by the separator
+ combined = sep.join(words)
+
+ # Split them by the separator excluding empty strings
+ return [ w for w in combined.split(sep) if w != '']
+
+def main(inputs):
+ # The separator is the last character
+ sep = inputs.pop()
+ result = split_strings(inputs, sep)
+ print('"' + '", "'.join(result) + '"')
+
+if __name__ == '__main__':
+ main(sys.argv[1:]) \ No newline at end of file
diff --git a/challenge-253/sgreen/python/ch-2.py b/challenge-253/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..aad97b55d0
--- /dev/null
+++ b/challenge-253/sgreen/python/ch-2.py
@@ -0,0 +1,41 @@
+#!/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(0, rows):
+ # Check that all columns are of equal length
+ if len(matrix[r]) != cols:
+ raise ValueError(f'Row {r} has different number of columns')
+
+ # Check all values are 0 or 1
+ if any(True for i in matrix[r] if i not in (0, 1)):
+ raise ValueError(f'Row {r} has a value other than 0 or 1')
+
+ # Check there are no ones zeros after zero.
+ if any(True for c in range(1, cols) if matrix[r][c-1] == 0 and matrix[r][c] == 1):
+ raise ValueError(f'Row {r} has a 1 after 0')
+
+
+def weakest_row(matrix):
+ # Start by validating any issues with the input
+ validate_matrix(matrix)
+
+ # Sort by the rows with the most 1s, followed by a numerical sort
+ return sorted(range(len(matrix)), key=lambda i: sum(matrix[i]))
+
+
+def main(json_string):
+ matrix = json.loads(json_string)
+ result = weakest_row(matrix)
+ print('(' + ', '.join(map(str, result)) + ')')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1])