aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-225/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-225/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-225/jeanluc2020/perl/ch-1.pl54
-rwxr-xr-xchallenge-225/jeanluc2020/perl/ch-2.pl93
4 files changed, 149 insertions, 0 deletions
diff --git a/challenge-225/jeanluc2020/blog-1.txt b/challenge-225/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..a5f935d5e7
--- /dev/null
+++ b/challenge-225/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-225-1.html
diff --git a/challenge-225/jeanluc2020/blog-2.txt b/challenge-225/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..fb76c73620
--- /dev/null
+++ b/challenge-225/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-225-2.html
diff --git a/challenge-225/jeanluc2020/perl/ch-1.pl b/challenge-225/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..6b26da959b
--- /dev/null
+++ b/challenge-225/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-225/#TASK1
+#
+# Task 1: Max Words
+# =================
+#
+# You are given a list of sentences, @list.
+#
+## A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
+#
+#
+# Write a script to find out the maximum number of words that appear in a single sentence.
+#
+## Example 1
+##
+## Input: @list = ("Perl and Raku belong to the same family.",
+## "I love Perl.",
+## "The Perl and Raku Conference.")
+## Output: 8
+#
+## Example 2
+##
+## Input: @list = ("The Weekly Challenge.",
+## "Python is the most popular guest language.",
+## "Team PWC has over 300 members.")
+## Output: 7
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Split all sentences into their single words and count. Keep
+# the maximum.
+
+use strict;
+use warnings;
+
+max_words("Perl and Raku belong to the same family.", "I love Perl.", "The Perl and Raku Conference.");
+max_words("The Weekly Challenge.", "Python is the most popular guest language.", "Team PWC has over 300 members.");
+
+sub max_words {
+ my @list = @_;
+ print "Input: \@list = (\"" . join("\",\n \"",@list) . "\")\n";
+ my $max = 0;
+ foreach my $sentence (@list) {
+ my @words = split / /, $sentence;
+ my $count = scalar(@words);
+ $max = $count if $count > $max;
+ }
+ print "Output: $max\n";
+}
+
diff --git a/challenge-225/jeanluc2020/perl/ch-2.pl b/challenge-225/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..ce01c087cc
--- /dev/null
+++ b/challenge-225/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-225/#TASK2
+#
+# Task 2: Left Right Sum Diff
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to return left right sum diff array as shown below:
+#
+# @ints = (a, b, c, d, e)
+#
+# @left = (0, a, (a+b), (a+b+c))
+# @right = ((c+d+e), (d+e), e, 0)
+# @left_right_sum_diff = ( | 0 - (c+d+e) |,
+# | a - (d+e) |,
+# | (a+b) - e |,
+# | (a+b+c) - 0 | )
+#
+##
+## WARNING: this explaination isn't entirely correct. As per
+## https://leetcode.com/problems/left-and-right-sum-differences/
+## it would make more sense to have an additional element
+## (a+b+c+d) at the end of @left and ((b+c+d+e) at the beginning
+## of @right, which also the examples demonstrate:
+##
+#
+## Example 1:
+##
+## Input: @ints = (10, 4, 8, 3)
+## Output: (15, 1, 11, 22)
+##
+## @left = (0, 10, 14, 22)
+## @right = (15, 11, 3, 0)
+##
+## @left_right_sum_diff = ( |0-15|, |10-11|, |14-3|, |22-0|)
+## = (15, 1, 11, 22)
+#
+## Example 2:
+##
+## Input: @ints = (1)
+## Output: (0)
+##
+## @left = (0)
+## @right = (0)
+##
+## @left_right_sum_diff = ( |0-0| ) = (0)
+#
+## Example 3:
+##
+## Input: @ints = (1, 2, 3, 4, 5)
+## Output: (14, 11, 6, 1, 19)
+##
+## @left = (0, 1, 3, 6, 10)
+## @right = (14, 12, 9, 5, 0)
+##
+## @left_right_sum_diff = ( |0-14|, |1-12|, |3-9|, |6-5|, |10-0|)
+## = (14, 11, 6, 1, 10)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, calculate @left and @right, then calculate the
+# @left_right_sum_diff
+
+use strict;
+use warnings;
+
+left_right_sum_diff(10, 4, 8, 3);
+left_right_sum_diff(1);
+left_right_sum_diff(1, 2, 3, 4, 5);
+
+sub left_right_sum_diff {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ my ($current_left, $current_right) = (0, 0);
+ my @left = ();
+ my @right = ();
+ foreach my $index (0..$#ints) {
+ push @left, $current_left;
+ $current_left += $ints[$index];
+ unshift @right, $current_right;
+ $current_right += $ints[ $#ints - $index ];
+ }
+ my @left_right_sum_diff = ();
+ foreach my $index (0..$#ints) {
+ $left_right_sum_diff[$index] = abs($left[$index] - $right[$index]);
+ }
+ print "Output: (" . join(", ", @left_right_sum_diff) . ")\n";
+}