aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-306/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-306/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-306/jeanluc2020/perl/ch-1.pl65
-rwxr-xr-xchallenge-306/jeanluc2020/perl/ch-2.pl116
4 files changed, 183 insertions, 0 deletions
diff --git a/challenge-306/jeanluc2020/blog-1.txt b/challenge-306/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..07a76c8dcd
--- /dev/null
+++ b/challenge-306/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-306-1.html
diff --git a/challenge-306/jeanluc2020/blog-2.txt b/challenge-306/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..5e234e59e6
--- /dev/null
+++ b/challenge-306/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-306-2.html
diff --git a/challenge-306/jeanluc2020/perl/ch-1.pl b/challenge-306/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..0676779cbf
--- /dev/null
+++ b/challenge-306/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-306/#TASK1
+#
+# Task 1: Odd Sum
+# ===============
+#
+# You are given an array of positive integers, @ints.
+#
+# Write a script to return the sum of all possible odd-length subarrays of the
+# given array. A subarray is a contiguous subsequence of the array.
+#
+## Example 1
+##
+## Input: @ints = (2, 5, 3, 6, 4)
+## Output: 77
+##
+## Odd length sub-arrays:
+## (2) => 2
+## (5) => 5
+## (3) => 3
+## (6) => 6
+## (4) => 4
+## (2, 5, 3) => 10
+## (5, 3, 6) => 14
+## (3, 6, 4) => 13
+## (2, 5, 3, 6, 4) => 20
+##
+## Sum => 2 + 5 + 3 + 6 + 4 + 10 + 14 + 13 + 20 => 77
+#
+## Example 2
+##
+## Input: @ints = (1, 3)
+## Output: 4
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We walk the array from the first element to the last and
+# add the sum of all subarrays starting at the current element
+# when the number of elements in that subarray is odd.
+
+use strict;
+use warnings;
+use List::Util qw(sum);
+
+odd_sum(2, 5, 3, 6, 4);
+odd_sum(1, 3);
+
+sub odd_sum {
+ my @ints = @_;
+ print "Input: (" . join(",", @ints) . ")\n";
+ my $sum = 0;
+ foreach my $i (0..$#ints) {
+ foreach my $j ($i..$#ints) {
+ my @tmp = @ints[$i..$j];
+ if(scalar(@tmp) % 2) {
+ $sum += sum(@tmp);
+ }
+ }
+ }
+ print "Output: $sum\n";
+}
diff --git a/challenge-306/jeanluc2020/perl/ch-2.pl b/challenge-306/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..49b768f2ed
--- /dev/null
+++ b/challenge-306/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,116 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-306/#TASK2
+#
+# Task 2: Last Element
+# ====================
+#
+# You are given a array of integers, @ints.
+#
+# Write a script to play a game where you pick two biggest integers in the
+# given array, say x and y. Then do the following:
+#
+# a) if x == y then remove both from the given array
+# b) if x != y then remove x and replace y with (y - x)
+#
+# At the end of the game, there is at most one element left.
+#
+# Return the last element if found otherwise return 0.
+#
+## Example 1
+##
+## Input: @ints = (3, 8, 5, 2, 9, 2)
+## Output: 1
+##
+## Step 1: pick 8 and 9 => (3, 5, 2, 1, 2)
+## Step 2: pick 3 and 5 => (2, 2, 1, 2)
+## Step 3: pick 2 and 1 => (1, 2, 2)
+## Step 4: pick 2 and 1 => (1, 2)
+## Step 5: pick 1 and 2 => (1)
+#
+## Example 2
+##
+## Input: @ints = (3, 2, 5)
+## Output: 0
+##
+## Step 1: pick 3 and 5 => (2, 2)
+## Step 2: pick 2 and 2 => ()
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# There is a bit of ambiguity in the description that is clarified
+# by the examples: if there are multiple elements of different value,
+# but also multiple elements that are as big as the maximum element,
+# then pick one of the biggest values and one of the second biggest,
+# NOT two of the biggest. So from (2, 2, 1, 2) pick 2 and 1, not
+# 2 and 2.
+# With that clarified, we can now implement the whole thing: Search
+# the two biggest values in the array, then handle them as layed out
+# in the rules.
+#
+
+use v5.36;
+
+last_element(3, 8, 5, 2, 9, 2);
+last_element(3, 2, 5);
+
+sub last_element(@ints) {
+ say "Input: (" . join(",", @ints) . ")";
+ while(@ints) {
+ if(1 == scalar(@ints)) {
+ say "Output: " . $ints[0];
+ return;
+ }
+ my ($max1, $max2) = @ints[0..1];
+ if($max1 < $max2) {
+ ($max1, $max2) = ($max2, $max1);
+ }
+ foreach my $i (2..$#ints) {
+ if($ints[$i] > $max1) {
+ ($max1, $max2) = ($ints[$i], $max1);
+ } elsif ($max1 == $max2) {
+ $max2 = $ints[$i];
+ } else {
+ if($ints[$i] > $max2 && $ints[$i] < $max1) {
+ $max2 = $ints[$i];
+ }
+ }
+ }
+ if($max1 == $max2) {
+ # remove two occurences of $max1
+ my @tmp = ();
+ my $removed = 0;
+ foreach my $elem (@ints) {
+ if($elem == $max1 && $removed < 2) {
+ $removed++;
+ next;
+ }
+ push @tmp, $elem;
+ }
+ @ints = @tmp;
+ } else {
+ # remove max2, replace max1 with max1-max2
+ my @tmp = ();
+ my $new = $max1 - $max2;
+ my $removed = 0;
+ my $replaced = 0;
+ foreach my $elem (@ints) {
+ if($elem == $max1 && ! $removed) {
+ $removed = 1;
+ next;
+ }
+ if($elem == $max2 && ! $replaced) {
+ push @tmp, $new;
+ $replaced = 1;
+ next;
+ }
+ push @tmp, $elem;
+ }
+ @ints = @tmp;
+ }
+ }
+ say "Output: 0";
+}