aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Köhler <jean-luc@picard.franken.de>2025-09-01 20:17:26 +0200
committerThomas Köhler <jean-luc@picard.franken.de>2025-09-01 20:17:26 +0200
commit3aca35618acfce8d7a8d904814462fae5e8d1b19 (patch)
treea67723ab4f2c11b93c0d015100f80891649376b4
parent87b63d3827da9980b2a3fb29f28b477a62b67cbd (diff)
downloadperlweeklychallenge-club-3aca35618acfce8d7a8d904814462fae5e8d1b19.tar.gz
perlweeklychallenge-club-3aca35618acfce8d7a8d904814462fae5e8d1b19.tar.bz2
perlweeklychallenge-club-3aca35618acfce8d7a8d904814462fae5e8d1b19.zip
Add solution 337.
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
-rw-r--r--challenge-337/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-337/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-337/jeanluc2020/perl/ch-1.pl83
-rwxr-xr-xchallenge-337/jeanluc2020/perl/ch-2.pl287
4 files changed, 372 insertions, 0 deletions
diff --git a/challenge-337/jeanluc2020/blog-1.txt b/challenge-337/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..124cc12ccc
--- /dev/null
+++ b/challenge-337/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-337-1.html
diff --git a/challenge-337/jeanluc2020/blog-2.txt b/challenge-337/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..d1777c6ed2
--- /dev/null
+++ b/challenge-337/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-337-2.html
diff --git a/challenge-337/jeanluc2020/perl/ch-1.pl b/challenge-337/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..66c648cae3
--- /dev/null
+++ b/challenge-337/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/#TASK1
+#
+# Task 1: Smaller Than Current
+# ============================
+#
+# You are given an array of numbers, @num1.
+#
+# Write a script to return an array, @num2, where $num2[i] is the count of all
+# numbers less than or equal to $num1[i].
+#
+## Example 1
+##
+## Input: @num1 = (6, 5, 4, 8)
+## Output: (2, 1, 0, 3)
+##
+## index 0: numbers <= 6 are 5, 4 => 2
+## index 1: numbers <= 5 are 4 => 1
+## index 2: numbers <= 4, none => 0
+## index 3: numbers <= 8 are 6, 5, 4 => 3
+#
+#
+## Example 2
+##
+## Input: @num1 = (7, 7, 7, 7)
+## Output: (3, 3, 3, 3)
+#
+#
+## Example 3
+##
+## Input: @num1 = (5, 4, 3, 2, 1)
+## Output: (4, 3, 2, 1, 0)
+#
+#
+## Example 4
+##
+## Input: @num1 = (-1, 0, 3, -2, 1)
+## Output: (1, 2, 4, 0, 3)
+#
+#
+## Example 5
+##
+## Input: @num1 = (0, 1, 1, 2, 0)
+## Output: (1, 3, 3, 4, 1)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we generate a descendingly sorted copy of @num1. Then, for
+# each element of @num1, we search for the first element inside
+# the sorted copy that is equal to the current number. From there we can
+# calculate the number of elements that are less than or equal to the
+# number by substracting the current index from the index of the last
+# element in the sorted list.
+
+use v5.36;
+use Data::Dumper;
+
+smaller_than_current(6, 5, 4, 8);
+smaller_than_current(7, 7, 7, 7);
+smaller_than_current(5, 4, 3, 2, 1);
+smaller_than_current(-1, 0, 3, -2, 1);
+smaller_than_current(0, 1, 1, 2, 0);
+
+sub smaller_than_current( @num1 ) {
+ say "Input: (" . join(", ", @num1) . ")";
+ my @numsorted = sort { $b <=> $a } @num1;
+ my @num2 = ();
+ foreach my $n (@num1) {
+ foreach my $i (0..$#numsorted) {
+ if($n == $numsorted[$i]) {
+ push @num2, $#numsorted-$i;
+ last;
+ }
+ }
+ }
+
+ say "Output: (" . join(", ", @num2) . ")";
+}
+
diff --git a/challenge-337/jeanluc2020/perl/ch-2.pl b/challenge-337/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..0029ea2433
--- /dev/null
+++ b/challenge-337/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,287 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/#TASK2
+#
+# Task 2: Odd Matrix
+# ==================
+#
+# You are given row and col, also a list of positions in the matrix.
+#
+# Write a script to perform action on each location (0-indexed) as provided in
+# the list and find out the total odd valued cells.
+#
+# For each location (r, c), do both of the following:
+#
+# a) Increment by 1 all the cells on row r.
+# b) Increment by 1 all the cells on column c.
+#
+#
+## Example 1
+##
+## Input: $row = 2, $col = 3, @locations = ([0,1],[1,1])
+## Output: 6
+##
+## Initial:
+## [ 0 0 0 ]
+## [ 0 0 0 ]
+##
+## Apply [0,1]:
+## Increment row 0:
+## Before After
+## [ 0 0 0 ] [ 1 1 1 ]
+## [ 0 0 0 ] [ 0 0 0 ]
+## Increment col 1:
+## Before After
+## [ 1 1 1 ] [ 1 2 1 ]
+## [ 0 0 0 ] [ 0 1 0 ]
+##
+## Apply [1,1]:
+## Increment row 1:
+## Before After
+## [ 1 2 1 ] [ 1 2 1 ]
+## [ 0 1 0 ] [ 1 2 1 ]
+## Increment col 1:
+## Before After
+## [ 1 2 1 ] [ 1 3 1 ]
+## [ 1 2 1 ] [ 1 3 1 ]
+##
+## Final:
+## [ 1 3 1 ]
+## [ 1 3 1 ]
+#
+#
+## Example 2
+##
+## Input: $row = 2, $col = 2, @locations = ([1,1],[0,0])
+## Output: 0
+##
+## Initial:
+## [ 0 0 ]
+## [ 0 0 ]
+##
+## Apply [1,1]:
+## Increment row 1:
+## Before After
+## [ 0 0 ] [ 0 0 ]
+## [ 0 0 ] [ 1 1 ]
+## Increment col 1:
+## Before After
+## [ 0 0 ] [ 0 1 ]
+## [ 1 1 ] [ 1 2 ]
+##
+## Apply [0,0]:
+## Increment row 0:
+## Before After
+## [ 0 1 ] [ 1 2 ]
+## [ 1 2 ] [ 1 2 ]
+## Increment col 0:
+## Before After
+## [ 1 2 ] [ 2 2 ]
+## [ 1 2 ] [ 2 2 ]
+##
+## Final:
+## [ 2 2 ]
+## [ 2 2 ]
+#
+#
+## Example 3
+##
+## Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1])
+## Output: 0
+##
+## Initial:
+## [ 0 0 0 ]
+## [ 0 0 0 ]
+## [ 0 0 0 ]
+##
+## Apply [0,0]:
+## Increment row 0:
+## Before After
+## [ 0 0 0 ] [ 1 1 1 ]
+## [ 0 0 0 ] [ 0 0 0 ]
+## [ 0 0 0 ] [ 0 0 0 ]
+## Increment col 0:
+## Before After
+## [ 1 1 1 ] [ 2 1 1 ]
+## [ 0 0 0 ] [ 1 0 0 ]
+## [ 0 0 0 ] [ 1 0 0 ]
+##
+## Apply [1,2]:
+## Increment row 1:
+## Before After
+## [ 2 1 1 ] [ 2 1 1 ]
+## [ 1 0 0 ] [ 2 1 1 ]
+## [ 1 0 0 ] [ 1 0 0 ]
+## Increment col 2:
+## Before After
+## [ 2 1 1 ] [ 2 1 2 ]
+## [ 2 1 1 ] [ 2 1 2 ]
+## [ 1 0 0 ] [ 1 0 1 ]
+##
+## Apply [2,1]:
+## Increment row 2:
+## Before After
+## [ 2 1 2 ] [ 2 1 2 ]
+## [ 2 1 2 ] [ 2 1 2 ]
+## [ 1 0 1 ] [ 2 1 2 ]
+## Increment col 1:
+## Before After
+## [ 2 1 2 ] [ 2 2 2 ]
+## [ 2 1 2 ] [ 2 2 2 ]
+## [ 2 1 2 ] [ 2 2 2 ]
+##
+## Final:
+## [ 2 2 2 ]
+## [ 2 2 2 ]
+## [ 2 2 2 ]
+#
+#
+## Example 4
+##
+## Input: $row = 1, $col = 5, @locations = ([0,2],[0,4])
+## Output: 2
+##
+## Initial:
+## [ 0 0 0 0 0 ]
+##
+## Apply [0,2]:
+## Increment row 0:
+## Before After
+## [ 0 0 0 0 0 ] [ 1 1 1 1 1 ]
+## Increment col 2:
+## Before After
+## [ 1 1 1 1 1 ] [ 1 1 2 1 1 ]
+##
+## Apply [0,4]:
+## Increment row 0:
+## Before After
+## [ 1 1 2 1 1 ] [ 2 2 3 2 2 ]
+## Increment col 4:
+## Before After
+## [ 2 2 3 2 2 ] [ 2 2 3 2 3 ]
+##
+## Final:
+## [ 2 2 3 2 3 ]
+#
+#
+## Example 5
+##
+## Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1])
+## Output: 8
+##
+## Initial:
+## [ 0 0 ]
+## [ 0 0 ]
+## [ 0 0 ]
+## [ 0 0 ]
+##
+## Apply [1,0]:
+## Increment row 1:
+## Before After
+## [ 0 0 ] [ 0 0 ]
+## [ 0 0 ] [ 1 1 ]
+## [ 0 0 ] [ 0 0 ]
+## [ 0 0 ] [ 0 0 ]
+## Increment col 0:
+## Before After
+## [ 0 0 ] [ 1 0 ]
+## [ 1 1 ] [ 2 1 ]
+## [ 0 0 ] [ 1 0 ]
+## [ 0 0 ] [ 1 0 ]
+##
+## Apply [3,1]:
+## Increment row 3:
+## Before After
+## [ 1 0 ] [ 1 0 ]
+## [ 2 1 ] [ 2 1 ]
+## [ 1 0 ] [ 1 0 ]
+## [ 1 0 ] [ 2 1 ]
+## Increment col 1:
+## Before After
+## [ 1 0 ] [ 1 1 ]
+## [ 2 1 ] [ 2 2 ]
+## [ 1 0 ] [ 1 1 ]
+## [ 2 1 ] [ 2 2 ]
+##
+## Apply [2,0]:
+## Increment row 2:
+## Before After
+## [ 1 1 ] [ 1 1 ]
+## [ 2 2 ] [ 2 2 ]
+## [ 1 1 ] [ 2 2 ]
+## [ 2 2 ] [ 2 2 ]
+## Increment col 0:
+## Before After
+## [ 1 1 ] [ 2 1 ]
+## [ 2 2 ] [ 3 2 ]
+## [ 2 2 ] [ 3 2 ]
+## [ 2 2 ] [ 3 2 ]
+##
+## Apply [0,1]:
+## Increment row 0:
+## Before After
+## [ 2 1 ] [ 3 2 ]
+## [ 3 2 ] [ 3 2 ]
+## [ 3 2 ] [ 3 2 ]
+## [ 3 2 ] [ 3 2 ]
+## Increment col 1:
+## Before After
+## [ 3 2 ] [ 3 3 ]
+## [ 3 2 ] [ 3 3 ]
+## [ 3 2 ] [ 3 3 ]
+## [ 3 2 ] [ 3 3 ]
+##
+## Final:
+## [ 3 3 ]
+## [ 3 3 ]
+## [ 3 3 ]
+## [ 3 3 ]
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we create the intial Matrix filled with 0's. Then we check
+# each element of the @locations array: Add 1 to each element of the
+# corresponding column and row. Then just walk the matrix once, counting
+# the odd elements.
+#
+
+use v5.36;
+
+odd_matrix(2, 3, [0,1],[1,1]);
+odd_matrix(2, 2, [1,1],[0,0]);
+odd_matrix(3, 3, [0,0],[1,2],[2,1]);
+odd_matrix(1, 5, [0,2],[0,4]);
+odd_matrix(4, 2, [1,0],[3,1],[2,0],[0,1]);
+
+sub odd_matrix($row, $col, @locations) {
+ say "Input: $row, $col, (" . join(", ", map { "[$_->[0],$_->[1]]" } @locations), ")";
+ my $matrix;
+ foreach my $r (0..$row-1) {
+ my $tmp = [];
+ foreach my $c (0..$col-1) {
+ push @$tmp, 0;
+ }
+ push @$matrix, $tmp;
+ }
+ foreach my $loc (@locations) {
+ my ($r, $c) = @$loc;
+ foreach my $col1 (0..scalar(@{$matrix->[0]})-1) {
+ $matrix->[$r]->[$col1]++;
+ }
+ foreach my $row1 (0..scalar(@{$matrix})-1) {
+ $matrix->[$row1]->[$c]++;
+ }
+ }
+ my $output = 0;
+ foreach my $r (0..$row-1) {
+ foreach my $c (0..$col-1) {
+ $output++ if $matrix->[$r]->[$c] % 2;
+ }
+ }
+ say "Output: $output";
+}
+
+