aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-228/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-228/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-228/jeanluc2020/perl/ch-1.pl60
-rwxr-xr-xchallenge-228/jeanluc2020/perl/ch-2.pl74
4 files changed, 136 insertions, 0 deletions
diff --git a/challenge-228/jeanluc2020/blog-1.txt b/challenge-228/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..f6d3525941
--- /dev/null
+++ b/challenge-228/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-228-1.html
diff --git a/challenge-228/jeanluc2020/blog-2.txt b/challenge-228/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..0eee83364f
--- /dev/null
+++ b/challenge-228/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-228-2.html
diff --git a/challenge-228/jeanluc2020/perl/ch-1.pl b/challenge-228/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..dbaf939927
--- /dev/null
+++ b/challenge-228/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-228/#TASK1
+#
+# Task 1: Unique Sum
+# ==================
+#
+# You are given an array of integers.
+#
+# Write a script to find out the sum of unique elements in the given array.
+#
+## Example 1
+##
+## Input: @int = (2, 1, 3, 2)
+## Output: 4
+##
+## In the given array we have 2 unique elements (1, 3).
+#
+## Example 2
+##
+## Input: @int = (1, 1, 1, 1)
+## Output: 0
+##
+## In the given array no unique element found.
+#
+## Example 3
+##
+## Input: @int = (2, 1, 3, 4)
+## Output: 10
+##
+## In the given array every element is unique.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# For each Element in the array, count the number of occurences.
+# Sum up the numbers where this number is 1.
+
+use strict;
+use warnings;
+
+unique_sum(2, 1, 3, 2);
+unique_sum(1, 1, 1, 1);
+unique_sum(2, 1, 3, 4);
+
+sub unique_sum {
+ my @int = @_;
+ print "Input: (" . join(", ", @int) . ")\n";
+ my $count;
+ # count the number of occurences for each element in the array
+ map { $count->{$_}++; } @int;
+ my $sum = 0;
+ # sum up the ones that occured exacty once
+ foreach my $key (keys %$count) {
+ $sum += $key if $count->{$key} == 1;
+ }
+ print "Output: $sum\n";
+}
diff --git a/challenge-228/jeanluc2020/perl/ch-2.pl b/challenge-228/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..beb3f3b848
--- /dev/null
+++ b/challenge-228/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-228/#TASK2
+#
+# Task 2: Empty Array
+# ===================
+#
+# You are given an array of integers in which all elements are unique.
+#
+# Write a script to perform the following operations until the array is
+# empty and return the total count of operations.
+#
+## If the first element is the smallest then remove it otherwise move it to
+## the end.
+#
+## Example 1
+##
+## Input: @int = (3, 4, 2)
+## Ouput: 5
+##
+## Operation 1: move 3 to the end: (4, 2, 3)
+## Operation 2: move 4 to the end: (2, 3, 4)
+## Operation 3: remove element 2: (3, 4)
+## Operation 4: remove element 3: (4)
+## Operation 5: remove element 4: ()
+#
+## Example 2
+##
+## Input: @int = (1, 2, 3)
+## Ouput: 3
+##
+## Operation 1: remove element 1: (2, 3)
+## Operation 2: remove element 2: (3)
+## Operation 3: remove element 3: ()
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# As long as there are elements in the array, we take the first
+# element. If it is the smallest element, we're done. Otherwise
+# we add the element back at the end of the array. Just count
+# the steps we're doing.
+
+use strict;
+use warnings;
+
+empty_array(3, 4, 2);
+empty_array(1, 2, 3);
+
+sub empty_array {
+ my @int = @_;
+ print "Input: (" . join(", ", @int) . ")\n";
+ my $steps = 0;
+ while(@int) {
+ $steps++;
+ my $min = min(@int);
+ my $first = shift @int;
+ if($min != $first) {
+ push @int, $first;
+ }
+ }
+ print "Output: $steps\n";
+}
+
+sub min {
+ my @array = @_;
+ my $min = $array[0];
+ foreach my $elem (@array) {
+ $min = $elem if $elem < $min;
+ }
+ return $min;
+}