aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-271/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-271/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-271/jeanluc2020/perl/ch-1.pl79
-rwxr-xr-xchallenge-271/jeanluc2020/perl/ch-2.pl65
4 files changed, 146 insertions, 0 deletions
diff --git a/challenge-271/jeanluc2020/blog-1.txt b/challenge-271/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..b8751ce5fb
--- /dev/null
+++ b/challenge-271/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-271-1.html
diff --git a/challenge-271/jeanluc2020/blog-2.txt b/challenge-271/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..9d091b4019
--- /dev/null
+++ b/challenge-271/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-271-2.html
diff --git a/challenge-271/jeanluc2020/perl/ch-1.pl b/challenge-271/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..b837ef24a0
--- /dev/null
+++ b/challenge-271/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-271/#TASK1
+#
+# Task 1: Maximum Ones
+# ====================
+#
+# You are given a m x n binary matrix.
+#
+# Write a script to return the row number containing maximum ones, in case of
+# more than one rows then return smallest row number.
+#
+## Example 1
+##
+## Input: $matrix = [ [0, 1],
+## [1, 0],
+## ]
+## Output: 1
+##
+## Row 1 and Row 2 have the same number of ones, so return row 1.
+#
+## Example 2
+##
+## Input: $matrix = [ [0, 0, 0],
+## [1, 0, 1],
+## ]
+## Output: 2
+##
+## Row 2 has the maximum ones, so return row 2.
+#
+## Example 3
+##
+## Input: $matrix = [ [0, 0],
+## [1, 1],
+## [0, 0],
+## ]
+## Output: 2
+##
+## Row 2 have the maximum ones, so return row 2.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We walk the matrix row by row. If the number of 1s is higher
+# than the previous maximum we have both a new maximum and the
+# new index of this maximum. In the end, return the index.
+
+use strict;
+use warnings;
+
+maximum_ones( [ [0, 1], [1, 0] ] );
+maximum_ones( [ [0, 0, 0], [1, 0, 1] ] );
+maximum_ones( [ [0, 0], [1, 1], [0, 0] ] );
+
+sub maximum_ones {
+ my $matrix = shift;
+ print "Input: [\n";
+ foreach my $row (@$matrix) {
+ print " [", join(", ", @$row), "],\n";
+ }
+ print " ]\n";
+ my $max_ones = 0;
+ my $max_index = 1;
+ my $index = 0;
+ foreach my $row (@$matrix) {
+ $index++;
+ my $count = 0;
+ foreach my $elem (@$row) {
+ $count++ if $elem == 1;
+ }
+ if($count > $max_ones) {
+ $max_index = $index;
+ $max_ones = $count;
+ }
+ }
+ print "Output: $max_ones\n";
+}
diff --git a/challenge-271/jeanluc2020/perl/ch-2.pl b/challenge-271/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..54e7ed0bbc
--- /dev/null
+++ b/challenge-271/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-271/#TASK2
+#
+# Task 2: Sort by 1 bits
+# ======================
+#
+# You are give an array of integers, @ints.
+#
+# Write a script to sort the integers in ascending order by the number of 1
+# bits in their binary representation. In case more than one integers have the
+# same number of 1 bits then sort them in ascending order.
+#
+## Example 1
+##
+## Input: @ints = (0, 1, 2, 3, 4, 5, 6, 7, 8)
+## Output: (0, 1, 2, 4, 8, 3, 5, 6, 7)
+##
+## 0 = 0 one bits
+## 1 = 1 one bits
+## 2 = 1 one bits
+## 4 = 1 one bits
+## 8 = 1 one bits
+## 3 = 2 one bits
+## 5 = 2 one bits
+## 6 = 2 one bits
+## 7 = 3 one bits
+#
+## Example 2
+##
+## Input: @ints = (1024, 512, 256, 128, 64)
+## Output: (64, 128, 256, 512, 1024)
+##
+## All integers in the given array have one 1-bits, so just sort them in ascending order.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# This one comes down to a slightly complex sort.
+# We calculate the bit sum of a number (number of 1 bits) and sort
+# by that - if the same, we sort by the numbers themselves.
+
+use strict;
+use warnings;
+
+sort_by_one_bits(0, 1, 2, 3, 4, 5, 6, 7, 8);
+sort_by_one_bits(1024, 512, 256, 128, 64);
+
+sub sort_by_one_bits {
+ my @ints = @_;
+ print "Input: (", join(", ", @ints), ")\n";
+ print "Output: (", join(", ", sort { bit_sum($a) <=> bit_sum($b) || $a <=> $b } @ints), ")\n";
+}
+
+sub bit_sum {
+ my $i = shift;
+ my $bits = sprintf("%b", $i);
+ my $sum = 0;
+ foreach my $bit (split //, $bits) {
+ $sum += $bit;
+ }
+ return $sum;
+}