aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-26 19:46:20 +0100
committerGitHub <noreply@github.com>2024-05-26 19:46:20 +0100
commit0675ab1c8df98d505d25bb6de074a6ab06eb0ede (patch)
tree027b967530b410505bb42f738f03aa15f2813b07
parenta92584edf3a0d0b4f743adfe7fdc88aff0d56ec4 (diff)
parent127ca5556cce26c1a069f95af1908995b7562fa5 (diff)
downloadperlweeklychallenge-club-0675ab1c8df98d505d25bb6de074a6ab06eb0ede.tar.gz
perlweeklychallenge-club-0675ab1c8df98d505d25bb6de074a6ab06eb0ede.tar.bz2
perlweeklychallenge-club-0675ab1c8df98d505d25bb6de074a6ab06eb0ede.zip
Merge pull request #10150 from simongreen-net/master
sgreen solutions to challenge 270
-rw-r--r--challenge-270/sgreen/README.md4
-rw-r--r--challenge-270/sgreen/blog.txt1
-rwxr-xr-xchallenge-270/sgreen/perl/ch-1.pl49
-rwxr-xr-xchallenge-270/sgreen/perl/ch-2.pl47
-rwxr-xr-xchallenge-270/sgreen/python/ch-1.py43
-rwxr-xr-xchallenge-270/sgreen/python/ch-2.py49
-rwxr-xr-xchallenge-270/sgreen/python/test.py23
7 files changed, 214 insertions, 2 deletions
diff --git a/challenge-270/sgreen/README.md b/challenge-270/sgreen/README.md
index f214c9fa60..f279ce1225 100644
--- a/challenge-270/sgreen/README.md
+++ b/challenge-270/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 269
+# The Weekly Challenge 270
-Blog: [Elements or something](https://dev.to/simongreennet/elements-or-something-2bl0)
+Blog: [Equalizing positions](https://dev.to/simongreennet/equalizing-positions-2057)
diff --git a/challenge-270/sgreen/blog.txt b/challenge-270/sgreen/blog.txt
new file mode 100644
index 0000000000..6ffe481a90
--- /dev/null
+++ b/challenge-270/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/equalizing-positions-2057 \ No newline at end of file
diff --git a/challenge-270/sgreen/perl/ch-1.pl b/challenge-270/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..ba7b3a5813
--- /dev/null
+++ b/challenge-270/sgreen/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use JSON 'decode_json';
+
+sub main ($matrix) {
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+ my $special_position = 0;
+
+ # Count the number of ones in each row and column
+ my @row_count = ( (0) x $rows );
+ my @col_count = ( (0) x $cols );
+
+ foreach my $row ( 0 .. $rows - 1 ) {
+ # Check this has the same number of columns as the first row
+ if ( scalar( @{ $matrix->[$row] } ) != $cols ) {
+ die "Row $row has the wrong number of columns\n";
+ }
+
+ foreach my $col ( 0 .. $cols - 1 ) {
+ if ( $matrix->[$row][$col] ) {
+ $row_count[$row]++;
+ $col_count[$col]++;
+ }
+ }
+ }
+
+ # Find the number of special positions. This is true if the value is one
+ # and the row_count and col_count is one
+ foreach my $row ( 0 .. $rows - 1 ) {
+ foreach my $col ( 0 .. $cols - 1 ) {
+ if ( $matrix->[$row][$col]
+ and $row_count[$row] == 1
+ and $col_count[$col] == 1 )
+ {
+ $special_position++;
+ }
+ }
+ }
+
+ say $special_position;
+}
+
+main( decode_json( $ARGV[0] ) ); \ No newline at end of file
diff --git a/challenge-270/sgreen/perl/ch-2.pl b/challenge-270/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..e3e56ba934
--- /dev/null
+++ b/challenge-270/sgreen/perl/ch-2.pl
@@ -0,0 +1,47 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw(max sum);
+
+sub main (@ints) {
+ # The last two values are $x and $y
+ my $y = pop(@ints);
+ my $x = pop(@ints);
+ my $score = 0;
+
+ # Calculate the needed values
+ my $max_value = max(@ints);
+ my @needed = map { $max_value - $_ } @ints;
+
+ # If we have at least two items, and y is less than 2 × x, lets remove two
+ # values at a time while we can
+ if ( $#ints > 0 and $y < $x * 2 ) {
+ while (1) {
+ # Sort the positions by the values they hold
+ my @sorted_index =
+ sort { $needed[$b] <=> $needed[$a] } ( 0 .. $#ints );
+
+ # If we don't have two non zero values, ww are done with level 2
+ if ( $needed[ $sorted_index[1] ] == 0 ) {
+ last;
+ }
+
+ # Take one off each number at that position
+ $needed[ $sorted_index[0] ] -= 1;
+ $needed[ $sorted_index[1] ] -= 1;
+ $score += $y;
+ }
+ }
+
+ # As level one takes one off each number, we simply multiple the remaining
+ # needed values by the x value
+ $score += sum(@needed) * $x;
+
+ say $score;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-270/sgreen/python/ch-1.py b/challenge-270/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..3cfe7dbe3e
--- /dev/null
+++ b/challenge-270/sgreen/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def special_positions(matrix: list) -> int:
+ rows = len(matrix)
+ cols = len(matrix[0])
+ special_position = 0
+
+ # Count the number of ones in each row and column
+ row_count = [0] * rows
+ col_count = [0] * cols
+
+ for row in range(rows):
+ # Check this has the same number of columns as the first row
+ if len(matrix[row]) != cols:
+ raise ValueError("Row %s has the wrong number of columns", row)
+
+ for col in range(cols):
+ if matrix[row][col]:
+ row_count[row] += 1
+ col_count[col] += 1
+
+ # Find the number of special positions. This is true if the value is one
+ # and the row_count and col_count is one
+ for row in range(rows):
+ for col in range(cols):
+ if matrix[row][col] and row_count[row] == 1 and col_count[col] == 1:
+ special_position += 1
+
+ return special_position
+
+
+def main():
+ matrix = json.loads(sys.argv[1])
+ result = special_positions(matrix)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-270/sgreen/python/ch-2.py b/challenge-270/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..d285799375
--- /dev/null
+++ b/challenge-270/sgreen/python/ch-2.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def equalize_array(ints: list, x: int, y: int) -> str:
+ score = 0
+ # Calculate the needed values
+ max_value = max(ints)
+ needed = [max_value - i for i in ints]
+
+ # If we have at least two items, and y is less than 2 × x, lets remove two
+ # values at a time while we can
+ if len(ints) > 1 and y < x * 2:
+ while True:
+ # Sort the positions by the values they hold
+ sorted_index = sorted(
+ range(len(ints)),
+ key=lambda index: needed[index],
+ reverse=True
+ )
+
+ # If we don't have two non zero values, we are done with level 2
+ if needed[sorted_index[1]] == 0:
+ break
+
+ # Take one off each number at that position
+ needed[sorted_index[0]] -= 1
+ needed[sorted_index[1]] -= 1
+ score += y
+
+ # As level one takes one off each number, we simply multiple the remaining
+ # needed values by the x value
+ score += sum(needed) * x
+
+ return score
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ y = array.pop()
+ x = array.pop()
+ result = equalize_array(array, x, y)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-270/sgreen/python/test.py b/challenge-270/sgreen/python/test.py
new file mode 100755
index 0000000000..5e279ab74f
--- /dev/null
+++ b/challenge-270/sgreen/python/test.py
@@ -0,0 +1,23 @@
+#!/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.special_positions([[1, 0, 0], [0, 0, 1], [1, 0, 0]]), 1
+ )
+ self.assertEqual(
+ ch_1.special_positions([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), 3
+ )
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.equalize_array([4, 1], 3, 2), 9)
+ self.assertEqual(ch_2.equalize_array([2, 3, 3, 3, 5], 2, 1), 6)
+
+
+if __name__ == '__main__':
+ unittest.main()