aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2023-04-08 20:09:13 +1000
committerSimon Green <mail@simon.green>2023-04-08 20:09:13 +1000
commit179e4b69b5060f752454bf14008c4a859345b0fe (patch)
tree6c25e85257a03e127f2787526d62a5c69fc5f392
parented17a0bef83e3276a3949814dab37a8a51871041 (diff)
downloadperlweeklychallenge-club-179e4b69b5060f752454bf14008c4a859345b0fe.tar.gz
perlweeklychallenge-club-179e4b69b5060f752454bf14008c4a859345b0fe.tar.bz2
perlweeklychallenge-club-179e4b69b5060f752454bf14008c4a859345b0fe.zip
Simon's solution to challenge 211
-rw-r--r--challenge-211/sgreen/README.md4
-rw-r--r--challenge-211/sgreen/blog.txt1
-rwxr-xr-xchallenge-211/sgreen/perl/ch-1.pl47
-rwxr-xr-xchallenge-211/sgreen/perl/ch-2.pl38
-rwxr-xr-xchallenge-211/sgreen/python/ch-1.py40
-rwxr-xr-xchallenge-211/sgreen/python/ch-2.py31
6 files changed, 159 insertions, 2 deletions
diff --git a/challenge-211/sgreen/README.md b/challenge-211/sgreen/README.md
index 0f4559dc5a..1c9faf1810 100644
--- a/challenge-211/sgreen/README.md
+++ b/challenge-211/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 210
+# The Weekly Challenge 211
-Blog: [Numbers Challenges](https://dev.to/simongreennet/numbers-challenges-32k1)
+Blog: [Weekly Challenge 211](https://dev.to/simongreennet/weekly-challenge-211-1np1)
diff --git a/challenge-211/sgreen/blog.txt b/challenge-211/sgreen/blog.txt
new file mode 100644
index 0000000000..12a949a17b
--- /dev/null
+++ b/challenge-211/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-211-1np1 \ No newline at end of file
diff --git a/challenge-211/sgreen/perl/ch-1.pl b/challenge-211/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..efee5c8349
--- /dev/null
+++ b/challenge-211/sgreen/perl/ch-1.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'min';
+use JSON;
+
+sub main ($matrix) {
+ # Let's define some variables
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+
+ # We always check from the top left
+ my @offsets = ( [ 0, 0 ] );
+
+ # If the matrix isn't square, we also use an offset to hit the bottom right
+ if ( $rows > $cols ) {
+ push @offsets, [ $rows - $cols, 0 ];
+ }
+ elsif ( $rows < $cols ) {
+ push @offsets, [ 0, $cols - $rows ];
+ }
+
+ # The number of checks we need to make
+ my $counts = min( $rows, $cols );
+
+ foreach my $o (@offsets) {
+ # Define the offsets, and the first value
+ my ( $row_offset, $col_offset ) = @$o;
+ my $value = $matrix->[$row_offset][$col_offset];
+
+ foreach my $i ( 1 .. $counts - 1 ) {
+ # Compare the value until we find one that doesn't match
+ if ( $value != $matrix->[ $row_offset + $i ][ $col_offset + $i ] ) {
+ say 'false';
+ return;
+ }
+ }
+ }
+
+ say 'true';
+}
+
+main( decode_json( $ARGV[0] ) ); \ No newline at end of file
diff --git a/challenge-211/sgreen/perl/ch-2.pl b/challenge-211/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..42e2533746
--- /dev/null
+++ b/challenge-211/sgreen/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util 'sum';
+use Algorithm::Combinatorics 'combinations';
+
+sub main(@n) {
+ # Calculate average
+ my $avg = sum(@n) / scalar(@n);
+
+ # A single element is always true
+ if ($#n == 0) {
+ say 'true';
+ return;
+ }
+
+
+ # Consider combination of size 1 to half the length
+ foreach my $i (1 .. scalar(@n)/2) {
+ # Work through each combination
+ my $iter = combinations(\@n, $i);
+ while ( my $c = $iter->next ) {
+ # If combination average is same, the remaining items also will be
+ if (sum(@$c) / $i == $avg) {
+ say 'true';
+ return;
+ }
+ }
+ }
+
+ say 'false';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-211/sgreen/python/ch-1.py b/challenge-211/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..334aeeb4bf
--- /dev/null
+++ b/challenge-211/sgreen/python/ch-1.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def main(matrix):
+ # Let's define some variables
+ rows = len(matrix)
+ cols = len(matrix[0])
+
+ # We always check from the top left
+ offsets = [[0, 0]]
+
+ # If the matrix isn't square, we also use an offset to hit the bottom right
+ if rows > cols:
+ offsets.append([rows-cols, 0])
+ elif rows < cols:
+ offsets.append([0, cols-rows])
+
+ # The number of checks we need to make
+ counts = min(rows, cols)
+
+ for o in offsets:
+ # Define the offsets, and the first value
+ row_offset, col_offset = o
+ value = matrix[row_offset][col_offset]
+
+ for i in range(1, counts):
+ # Compare the value until we find one that doesn't match
+ if value != matrix[row_offset+i][col_offset+i]:
+ print('false')
+ return
+
+ # Print results
+ print('true')
+
+
+if __name__ == '__main__':
+ main(json.loads(sys.argv[1]))
diff --git a/challenge-211/sgreen/python/ch-2.py b/challenge-211/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..57226ad4bf
--- /dev/null
+++ b/challenge-211/sgreen/python/ch-2.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+from itertools import combinations
+import sys
+
+
+def main(n):
+ # Calculate average
+ avg = sum(n) / len(n)
+
+ # A single element is always true
+ if len(n) == 1:
+ print('true')
+ return
+
+ # Consider combination of size 1 to half the length
+ for i in range(1, len(n)//2+1):
+ # Work through each combination
+ for c in combinations(n, i):
+ # If combination average is same, the remaining items also will be
+ if sum(c) / i == avg:
+ print('true')
+ return
+
+ print('false')
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)