aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2024-06-02 16:44:05 +1000
committerSimon Green <mail@simon.green>2024-06-02 16:44:05 +1000
commit9570cd789d0f24fcfba65675b371c5feae0e3dc8 (patch)
tree5c3aea0411b8c26b5c9c1e000decba498789a4ed
parent5ea56aa37a9f0b7098302e2acb76c73907c70bde (diff)
downloadperlweeklychallenge-club-9570cd789d0f24fcfba65675b371c5feae0e3dc8.tar.gz
perlweeklychallenge-club-9570cd789d0f24fcfba65675b371c5feae0e3dc8.tar.bz2
perlweeklychallenge-club-9570cd789d0f24fcfba65675b371c5feae0e3dc8.zip
sgreen solutions to challenge 271
-rw-r--r--challenge-271/sgreen/README.md4
-rw-r--r--challenge-271/sgreen/blog.txt1
-rwxr-xr-xchallenge-271/sgreen/perl/ch-1.pl37
-rwxr-xr-xchallenge-271/sgreen/perl/ch-2.pl21
-rwxr-xr-xchallenge-271/sgreen/python/ch-1.py47
-rwxr-xr-xchallenge-271/sgreen/python/ch-2.py29
-rwxr-xr-xchallenge-271/sgreen/python/test.py26
7 files changed, 163 insertions, 2 deletions
diff --git a/challenge-271/sgreen/README.md b/challenge-271/sgreen/README.md
index f279ce1225..c76a8f7987 100644
--- a/challenge-271/sgreen/README.md
+++ b/challenge-271/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 270
+# The Weekly Challenge 271
-Blog: [Equalizing positions](https://dev.to/simongreennet/equalizing-positions-2057)
+Blog: [Maximizing the 1 bits](https://dev.to/simongreennet/maximizing-the-1-bits-m8a)
diff --git a/challenge-271/sgreen/blog.txt b/challenge-271/sgreen/blog.txt
new file mode 100644
index 0000000000..040182a2f1
--- /dev/null
+++ b/challenge-271/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/maximizing-the-1-bits-m8a \ No newline at end of file
diff --git a/challenge-271/sgreen/perl/ch-1.pl b/challenge-271/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..6ab1c588cf
--- /dev/null
+++ b/challenge-271/sgreen/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use JSON 'decode_json';
+use List::Util 'sum';
+
+sub main ($matrix) {
+ my $rows = scalar(@$matrix);
+ my $cols = scalar( @{ $matrix->[0] } );
+ my $special_position = 0;
+
+ # The row that has the maximum values
+ my $max_row = 0;
+
+ # The number of ones in that row
+ my $max_count = 0;
+
+ 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";
+ }
+
+ if ( sum( @{ $matrix->[$row] } ) > $max_count ) {
+ $max_row = $row;
+ $max_count = sum( @{ $matrix->[$row] } );
+ }
+ }
+ # Return the rows as a 1-based index value
+ say $max_row + 1;
+}
+
+main( decode_json( $ARGV[0] ) ); \ No newline at end of file
diff --git a/challenge-271/sgreen/perl/ch-2.pl b/challenge-271/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..170d3c7e9b
--- /dev/null
+++ b/challenge-271/sgreen/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub one_bits($int) {
+ # Convert the integer to binary, and count the number of 1s
+ my $bin = sprintf( "%b", $int );
+ return ($bin =~ tr/1/1/);
+}
+
+sub sort_by_1_bits (@ints) {
+ # Sort the integers by the number of 1 bits, and by the integer value if
+ # the number of 1 bits is the same
+ my @sorted = sort { one_bits($a) <=> one_bits($b) || $a <=> $b } @ints;
+ say '(' . join( ', ', @sorted ) . ')';
+}
+
+sort_by_1_bits(@ARGV); \ No newline at end of file
diff --git a/challenge-271/sgreen/python/ch-1.py b/challenge-271/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..21f9744e1a
--- /dev/null
+++ b/challenge-271/sgreen/python/ch-1.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+import json
+import sys
+
+
+def maximum_ones(matrix: list) -> int:
+ """
+ Finds the row with the maximum number of ones in a binary matrix.
+
+ Args:
+ matrix (list): A binary matrix represented as a list of lists.
+
+ Returns:
+ int: The 1-based index of the row with the maximum number of ones.
+ """
+
+ rows = len(matrix)
+ cols = len(matrix[0])
+
+ # The row that has the maximum values
+ max_row = 0
+
+ # The number of ones in that row
+ max_count = 0
+
+ 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)
+
+ if sum(matrix[row]) > max_count:
+ max_row = row
+ max_count = sum(matrix[row])
+
+ # Return the rows as a 1-based index value
+ return max_row + 1
+
+
+def main():
+ matrix = json.loads(sys.argv[1])
+ result = maximum_ones(matrix)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-271/sgreen/python/ch-2.py b/challenge-271/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..7ebb7f412a
--- /dev/null
+++ b/challenge-271/sgreen/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def sort_by_1_bits(ints: list) -> list:
+ """
+ Sorts a list of integers based on the number of set bits (1s) in their
+ binary representation. If they are equal, they are sorted by their value.
+
+ Args:
+ ints (list): A list of integers to be sorted.
+
+ Returns:
+ list: A new list of integers sorted based on the criteria above.
+ """
+ sorted_ints = sorted(ints, key=lambda x: (bin(x).count('1'), x))
+ return sorted_ints
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = sort_by_1_bits(array)
+ print(tuple(result))
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-271/sgreen/python/test.py b/challenge-271/sgreen/python/test.py
new file mode 100755
index 0000000000..0e2fcf2486
--- /dev/null
+++ b/challenge-271/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.maximum_ones([[0, 1],[1, 0]]), 1)
+ self.assertEqual(ch_1.maximum_ones([[0, 0, 0],[1, 0, 1]]), 2)
+ self.assertEqual(ch_1.maximum_ones([[0, 0],[1, 1],[0, 0]]), 2)
+
+ def test_ch_2(self):
+ self.assertEqual(
+ ch_2.sort_by_1_bits([0, 1, 2, 3, 4, 5, 6, 7, 8]),
+ [0, 1, 2, 4, 8, 3, 5, 6, 7]
+ )
+ self.assertEqual(
+ ch_2.sort_by_1_bits([1024, 512, 256, 128, 64]),
+ [64, 128, 256, 512, 1024]
+ )
+
+
+if __name__ == '__main__':
+ unittest.main()