aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-284/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-284/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-284/jeanluc2020/perl/ch-1.pl59
-rwxr-xr-xchallenge-284/jeanluc2020/perl/ch-2.pl71
4 files changed, 132 insertions, 0 deletions
diff --git a/challenge-284/jeanluc2020/blog-1.txt b/challenge-284/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..7bf87cc718
--- /dev/null
+++ b/challenge-284/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-284-1.html
diff --git a/challenge-284/jeanluc2020/blog-2.txt b/challenge-284/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..8b200dfb54
--- /dev/null
+++ b/challenge-284/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-284-2.html
diff --git a/challenge-284/jeanluc2020/perl/ch-1.pl b/challenge-284/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..4205b57f3e
--- /dev/null
+++ b/challenge-284/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-284/#TASK1
+#
+# Task 1: Lucky Integer
+# =====================
+#
+# You are given an array of integers, @ints.
+#
+# Write a script to find the lucky integer if found otherwise return -1. If
+# there are more than one then return the largest.
+#
+### A lucky integer is an integer that has a frequency in the array equal to
+### its value.
+#
+## Example 1
+##
+## Input: @ints = (2, 2, 3, 4)
+## Output: 2
+#
+## Example 2
+##
+## Input: @ints = (1, 2, 2, 3, 3, 3)
+## Output: 3
+#
+## Example 3
+##
+## Input: @ints = (1, 1, 1, 3)
+## Output: -1
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Count the frequencies for all elements in the array. Then
+# check the frequencies versus the value sorted in descending
+# order and stop once we find a match. If there was no match,
+# return -1.
+
+use strict;
+use warnings;
+
+lucky_integer(2, 2, 3, 4);
+lucky_integer(1, 2, 2, 3, 3, 3);
+lucky_integer(1, 1, 1, 3);
+
+sub lucky_integer {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ my $frequencies = {};
+ foreach my $i (@ints) {
+ $frequencies->{$i}++;
+ }
+ foreach my $f (sort {$b<=>$a} keys %{$frequencies}) {
+ return print "Output: $f\n" if $frequencies->{$f} == $f;
+ }
+ print "Output: -1\n";
+}
diff --git a/challenge-284/jeanluc2020/perl/ch-2.pl b/challenge-284/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..b04a87b4aa
--- /dev/null
+++ b/challenge-284/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-284/#TASK2
+#
+# Task 2: Relative Sort
+# =====================
+#
+# You are given two list of integers, @list1 and @list2. The elements in the
+# @list2 are distinct and also in the @list1.
+#
+# Write a script to sort the elements in the @list1 such that the relative
+# order of items in @list1 is same as in the @list2. Elements that is missing
+# in @list2 should be placed at the end of @list1 in ascending order.
+#
+## Example 1
+##
+## Input: @list1 = (2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5)
+## @list2 = (2, 1, 4, 3, 5, 6)
+## Ouput: (2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9)
+#
+## Example 2
+##
+## Input: @list1 = (3, 3, 4, 6, 2, 4, 2, 1, 3)
+## @list2 = (1, 3, 2)
+## Ouput: (1, 3, 3, 3, 2, 2, 4, 4, 6)
+#
+## Example 3
+##
+## Input: @list1 = (3, 0, 5, 0, 2, 1, 4, 1, 1)
+## @list2 = (1, 0, 3, 2)
+## Ouput: (1, 1, 1, 0, 0, 3, 2, 4, 5)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# First, we turn the second list into a hash with the integers
+# as keys and their position inside the array as the values.
+# Then we run a clever compare function:
+# - if there is an entry for both numbers to be compared, we just
+# compare their position in the original list2.
+# - if only one of the two numbers has an entry in the hash, sort
+# this number first
+# - if both numbers are missing in list2, then sort by their value
+
+use strict;
+use warnings;
+
+relative_sort( [2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5], [2, 1, 4, 3, 5, 6] );
+relative_sort( [3, 3, 4, 6, 2, 4, 2, 1, 3], [1, 3, 2] );
+relative_sort( [3, 0, 5, 0, 2, 1, 4, 1, 1], [1, 0, 3, 2] );
+
+sub relative_sort {
+ my ($list1, $list2) = @_;
+ print "Input: (" . join(", ", @$list1) . "),\n (" . join(", ", @$list2) . ")\n";
+ my $int_to_index = {};
+ my $i = 0;
+ foreach my $elem (@$list2) {
+ $int_to_index->{$elem} = $i;
+ $i++;
+ }
+ my @sorted = sort {
+ ( defined($int_to_index->{$a}) && defined($int_to_index->{$b}) &&
+ $int_to_index->{$a} <=> $int_to_index->{$b} ) ||
+ ( defined($int_to_index->{$a}) && -1 ) ||
+ ( defined($int_to_index->{$b}) && 1 ) ||
+ $a <=> $b
+ } @$list1;
+ print "Output: (" . join(", ", @sorted) . ")\n";
+}