aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-14 12:18:28 +0000
committerGitHub <noreply@github.com>2024-01-14 12:18:28 +0000
commita032c84e79bf314d6118fcd6ebb15fdcc63c8998 (patch)
tree92dc95c7aa0b288fe730fde93d3fde3899ee09f7
parent53668b050d994a10fdebda5fbafba13ce8f74bc5 (diff)
parent74305a8889574d47d371f158170892e1697fa13f (diff)
downloadperlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.tar.gz
perlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.tar.bz2
perlweeklychallenge-club-a032c84e79bf314d6118fcd6ebb15fdcc63c8998.zip
Merge pull request #9390 from simongreen-net/master
Simon's solution to challenge 251
-rw-r--r--challenge-251/sgreen/README.md4
-rw-r--r--challenge-251/sgreen/blog.txt1
-rwxr-xr-xchallenge-251/sgreen/perl/ch-1.pl26
-rwxr-xr-xchallenge-251/sgreen/perl/ch-2.pl49
-rwxr-xr-xchallenge-251/sgreen/python/ch-1.py25
-rwxr-xr-xchallenge-251/sgreen/python/ch-2.py40
6 files changed, 143 insertions, 2 deletions
diff --git a/challenge-251/sgreen/README.md b/challenge-251/sgreen/README.md
index 98fb8281b8..8323c1d923 100644
--- a/challenge-251/sgreen/README.md
+++ b/challenge-251/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 250
+# The Weekly Challenge 251
-Blog: [Small and large](https://dev.to/simongreennet/small-and-large-2ap)
+Blog: [Lucky values](https://dev.to/simongreennet/lucky-values-4bh5)
diff --git a/challenge-251/sgreen/blog.txt b/challenge-251/sgreen/blog.txt
new file mode 100644
index 0000000000..8feb7dfb98
--- /dev/null
+++ b/challenge-251/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/lucky-values-4bh5 \ No newline at end of file
diff --git a/challenge-251/sgreen/perl/ch-1.pl b/challenge-251/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..68bf90e7fc
--- /dev/null
+++ b/challenge-251/sgreen/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@ints) {
+ my $solution = 0;
+ my $half = int( scalar(@ints) / 2 );
+
+ # If we have an odd number of integers, use the middle value
+ if ( $#ints % 2 == 0 ) {
+ $solution += $ints[$half];
+ }
+
+ # Combine the concatenation of the remaining integers, starting with first
+ # and last, then second and second last, and so on.
+ foreach my $i ( 0 .. $half - 1 ) {
+ $solution += ( $ints[$i] . $ints[ -1 - $i ] );
+ }
+
+ say $solution;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-251/sgreen/perl/ch-2.pl b/challenge-251/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..c1a6c6d7ab
--- /dev/null
+++ b/challenge-251/sgreen/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use JSON 'decode_json';
+use List::Util qw(any max min);
+
+sub main ($matrix) {
+ # Calculate the size of the matrix
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+
+ # Check that all columns are of equal length
+ foreach my $r ( 0 .. $#$matrix ) {
+ if ( scalar( @{ $matrix->[$r] } ) != $cols ) {
+ die "Row $r has different number of columns\n";
+ }
+ }
+
+ # Calculate the column maximums
+ my @column_max = ();
+ foreach my $c ( 0 .. $cols - 1 ) {
+ push @column_max, max( map { $_->[$c] } @$matrix );
+ }
+
+ my $solution = -1;
+
+ # Go through each row
+ foreach my $r (@$matrix) {
+ # Get the value of all columns that are the minimum value for this row
+ # and the maximum for the column
+ my $row_min = min(@$r);
+ if ( any { $r->[$_] == $row_min and $column_max[$_] == $row_min }
+ ( 0 .. $cols - 1 ) )
+ {
+ # We have a lucky number (row_min)
+ $solution = $row_min;
+ last;
+ }
+ }
+
+ $solution = -1 if not defined $solution;
+ say $solution;
+}
+
+main(decode_json( $ARGV[0] ) ); \ No newline at end of file
diff --git a/challenge-251/sgreen/python/ch-1.py b/challenge-251/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..258ef4da0d
--- /dev/null
+++ b/challenge-251/sgreen/python/ch-1.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(ints):
+ solution = 0
+ half = len(ints) // 2
+
+ # If we have an odd number of integers, use the middle value
+ if len(ints) % 2 == 1:
+ solution += ints[half]
+
+ # Combine the concatenation of the remaining integers, starting with first
+ # and last, then second and second last, and so on.
+ for i in range(half):
+ solution += int(str(ints[i]) + str(ints[-1-i]))
+
+ print(solution)
+
+
+if __name__ == '__main__':
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ main(array)
diff --git a/challenge-251/sgreen/python/ch-2.py b/challenge-251/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..643760029c
--- /dev/null
+++ b/challenge-251/sgreen/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def main(matrix):
+ # Calculate the size of the matrix
+ rows = len(matrix)
+ cols = len(matrix[0])
+
+ # Check that all columns are of equal length
+ for r in range(1, rows):
+ if len(matrix[r]) != cols:
+ raise ValueError(f'Row {r} has different number of columns')
+
+ # Calculate the column maximums
+ column_max = []
+ for c in range(cols):
+ column_max.append(max(map(lambda i: i[c], matrix)))
+
+ solution = -1
+
+ # Go through each row
+ for r in matrix:
+ # Get the value of all columns that are the minimum value for this row
+ # and the maximum for the column
+ row_min = min(r)
+ if any(True for i, value in enumerate(r)
+ if value == row_min and column_max[i] == value):
+ # We have a lucky number (row_min)
+ solution = row_min
+ break
+
+ print(solution)
+
+
+if __name__ == '__main__':
+ # Read the input as JSON
+ main(json.loads(sys.argv[1]))