aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-248/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-248/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-248/jeanluc2020/perl/ch-1.pl70
-rwxr-xr-xchallenge-248/jeanluc2020/perl/ch-2.pl74
-rwxr-xr-xchallenge-248/jeanluc2020/python/ch-1.py66
-rwxr-xr-xchallenge-248/jeanluc2020/python/ch-2.py71
6 files changed, 283 insertions, 0 deletions
diff --git a/challenge-248/jeanluc2020/blog-1.txt b/challenge-248/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..6f63c724bf
--- /dev/null
+++ b/challenge-248/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-248-1.html
diff --git a/challenge-248/jeanluc2020/blog-2.txt b/challenge-248/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..d66dd45736
--- /dev/null
+++ b/challenge-248/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-248-2.html
diff --git a/challenge-248/jeanluc2020/perl/ch-1.pl b/challenge-248/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..d843d3e10c
--- /dev/null
+++ b/challenge-248/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/#TASK1
+#
+# Task 1: Shortest Distance
+# =========================
+#
+# You are given a string and a character in the given string.
+#
+# Write a script to return an array of integers of size same as length of the
+# given string such that:
+#
+# distance[i] is the distance from index i to the closest occurence of
+# the given character in the given string.
+#
+# The distance between two indices i and j is abs(i - j).
+#
+## Example 1
+##
+## Input: $str = "loveleetcode", $char = "e"
+## Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+##
+## The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+## The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
+## The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
+## For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+## but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+## The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
+#
+## Example 2
+##
+## Input: $str = "aaab", $char = "b"
+## Output: (3,2,1,0)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's split the input string into its characters and fill an
+# array from that. Then we create a map using the positions of
+# all occurrences of the wanted character. Now we walk the array
+# and take the minimum of all possible differences found to
+# elements from the map, or -1 if the map is empty (this is
+# just extra error handling in case someone throws a character
+# into the function that doesn't exist in the string, we just
+# return -1 for all characters in that case).
+
+use strict;
+use warnings;
+use List::Util qw(min);
+
+shortest_distance("loveleetcode", "e");
+shortest_distance("aaab", "b");
+shortest_distance("aaab", "c");
+
+sub shortest_distance {
+ my ($string, $char) = @_;
+ print "Input: '$string', '$char'\n";
+ my @str_chars = split //, $string;
+ my @map;
+ foreach my $i (0..$#str_chars) {
+ push @map, $i if $str_chars[$i] eq $char;
+ }
+ my @result = ();
+ foreach my $i (0..$#str_chars) {
+ push @result, min( map { abs($i - $_), } @map ) // -1;
+ }
+ print "Output: (" . join(",", @result) . ")\n";
+}
diff --git a/challenge-248/jeanluc2020/perl/ch-2.pl b/challenge-248/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..31fda73f4f
--- /dev/null
+++ b/challenge-248/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/#TASK2
+#
+# Task 2: Submatrix Sum
+# =====================
+#
+# You are given a NxM matrix A of integers.
+#
+# Write a script to construct a (N-1)x(M-1) matrix B having elements that are
+# the sum over the 2x2 submatrices of A,
+#
+# b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]
+#
+#
+## Example 1
+##
+## Input: $a = [
+## [1, 2, 3, 4],
+## [5, 6, 7, 8],
+## [9, 10, 11, 12]
+## ]
+##
+## Output: $b = [
+## [14, 18, 22],
+## [30, 34, 38]
+## ]
+#
+## Example 2
+##
+## Input: $a = [
+## [1, 0, 0, 0],
+## [0, 1, 0, 0],
+## [0, 0, 1, 0],
+## [0, 0, 0, 1]
+## ]
+##
+## Output: $b = [
+## [2, 1, 0],
+## [1, 2, 1],
+## [0, 1, 2]
+## ]
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Just some index arithmetic
+
+submatrix_sum( [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] );
+submatrix_sum( [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ] );
+
+sub submatrix_sum {
+ my $matrix = shift;
+ print "Input: [\n";
+ foreach my $array (@$matrix) {
+ print " [" . join(",", @$array) . "],\n";
+ }
+ print " ]\n";
+ my $l = scalar(@$matrix);
+ my $m = scalar(@{$matrix->[0]});
+ my $resultmatrix = [];
+ foreach my $i (0..$l-2) {
+ foreach my $k (0..$m-2) {
+ $resultmatrix->[$i]->[$k] = $matrix->[$i]->[$k] + $matrix->[$i]->[$k+1] + $matrix->[$i+1]->[$k] + $matrix->[$i+1]->[$k+1];
+ }
+ }
+ print "Output: [\n";
+ foreach my $array (@$resultmatrix) {
+ print " [" . join(",", @$array) . "],\n";
+ }
+ print " ]\n";
+}
diff --git a/challenge-248/jeanluc2020/python/ch-1.py b/challenge-248/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..30c51061e9
--- /dev/null
+++ b/challenge-248/jeanluc2020/python/ch-1.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/#TASK1
+#
+# Task 1: Shortest Distance
+# =========================
+#
+# You are given a string and a character in the given string.
+#
+# Write a script to return an array of integers of size same as length of the
+# given string such that:
+#
+# distance[i] is the distance from index i to the closest occurence of
+# the given character in the given string.
+#
+# The distance between two indices i and j is abs(i - j).
+#
+## Example 1
+##
+## Input: $str = "loveleetcode", $char = "e"
+## Output: (3,2,1,0,1,0,0,1,2,2,1,0)
+##
+## The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
+## The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
+## The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
+## For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5,
+## but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
+## The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.
+#
+## Example 2
+##
+## Input: $str = "aaab", $char = "b"
+## Output: (3,2,1,0)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's split the input string into its characters and fill an
+# array from that. Then we create a map using the positions of
+# all occurrences of the wanted character. Now we walk the array
+# and take the minimum of all possible differences found to
+# elements from the map, or -1 if the map is empty (this is
+# just extra error handling in case someone throws a character
+# into the function that doesn't exist in the string, we just
+# return -1 for all characters in that case).
+
+def shortest_distance(string: str, char: str) -> list:
+ print(f"Input: '{string}', '{char}'")
+ str_chars = list(string)
+ map = []
+ for i in range(len(str_chars)):
+ if str_chars[i] == char:
+ map.append(i)
+ result = []
+ for i in range(len(str_chars)):
+ result.append(min([abs(i-x) for x in map], default = -1))
+ print(f"Output: (", ",".join(str(x) for x in result), ")")
+ return result
+
+
+shortest_distance("loveleetcode", "e")
+shortest_distance("aaab", "b")
+shortest_distance("aaab", "c")
+
diff --git a/challenge-248/jeanluc2020/python/ch-2.py b/challenge-248/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..e2177fe4b0
--- /dev/null
+++ b/challenge-248/jeanluc2020/python/ch-2.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-248/#TASK2
+#
+# Task 2: Submatrix Sum
+# =====================
+#
+# You are given a NxM matrix A of integers.
+#
+# Write a script to construct a (N-1)x(M-1) matrix B having elements that are
+# the sum over the 2x2 submatrices of A,
+#
+# b[i,k] = a[i,k] + a[i,k+1] + a[i+1,k] + a[i+1,k+1]
+#
+#
+## Example 1
+##
+## Input: $a = [
+## [1, 2, 3, 4],
+## [5, 6, 7, 8],
+## [9, 10, 11, 12]
+## ]
+##
+## Output: $b = [
+## [14, 18, 22],
+## [30, 34, 38]
+## ]
+#
+## Example 2
+##
+## Input: $a = [
+## [1, 0, 0, 0],
+## [0, 1, 0, 0],
+## [0, 0, 1, 0],
+## [0, 0, 0, 1]
+## ]
+##
+## Output: $b = [
+## [2, 1, 0],
+## [1, 2, 1],
+## [0, 1, 2]
+## ]
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Just some index arithmetic
+
+def submatrix_sum(matrix: list) -> list:
+ print("Input: [")
+ for array in matrix:
+ print(" [", ",".join([str(x) for x in array]), "],")
+ print(" ]")
+ l = len(matrix)
+ m = len(matrix[0])
+ resultmatrix = []
+ for i in range(l-1):
+ resultmatrix.append([])
+ for k in range(m-1):
+ resultmatrix[i].append( matrix[i][k] + matrix[i][k+1] + matrix[i+1][k] + matrix[i+1][k+1] )
+ print("Output: [")
+ for array in resultmatrix:
+ print(" [", ",".join([str(x) for x in array]), "],")
+ print(" ]")
+ return resultmatrix
+
+submatrix_sum( [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12] ] )
+submatrix_sum( [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ] )
+