aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-249/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-249/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-249/jeanluc2020/perl/ch-1.pl79
-rwxr-xr-xchallenge-249/jeanluc2020/perl/ch-2.pl62
-rwxr-xr-xchallenge-249/jeanluc2020/python/ch-1.py72
-rwxr-xr-xchallenge-249/jeanluc2020/python/ch-2.py59
6 files changed, 274 insertions, 0 deletions
diff --git a/challenge-249/jeanluc2020/blog-1.txt b/challenge-249/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..7d29922b0c
--- /dev/null
+++ b/challenge-249/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-249-1.html
diff --git a/challenge-249/jeanluc2020/blog-2.txt b/challenge-249/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..1606e86db7
--- /dev/null
+++ b/challenge-249/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-249-2.html
diff --git a/challenge-249/jeanluc2020/perl/ch-1.pl b/challenge-249/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..fe6d982d7a
--- /dev/null
+++ b/challenge-249/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/#TASK1
+#
+# Task 1: Shortest Distance
+# =========================
+#
+# You are given an array of integers with even number of elements.
+#
+# Write a script to divide the given array into equal pairs such that:
+#
+# a) Each element belongs to exactly one pair.
+# b) The elements present in a pair are equal.
+#
+#
+## Example 1
+##
+## Input: @ints = (3, 2, 3, 2, 2, 2)
+## Output: (2, 2), (3, 3), (2, 2)
+##
+## There are 6 elements in @ints.
+## They should be divided into 6 / 2 = 3 pairs.
+## @ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the
+## conditions.
+#
+## Example 2
+##
+## Input: @ints = (1, 2, 3, 4)
+## Output: ()
+##
+## There is no way to divide @ints 2 pairs such that the pairs satisfy every
+## condition.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We use the elements of the array as keys for a hash in which we
+# count the amount of elements of this value in the array.
+# If in the end, all values in the hash are even, we can spit out the
+# correct number of arrays, otherwise we can only return an empty array.
+
+use strict;
+use warnings;
+
+shortest_distance(3, 2, 3, 2, 2, 2);
+shortest_distance(1, 2, 3, 4);
+
+sub shortest_distance {
+ my @ints = @_;
+ print "Input: (" . join(", ", @ints) . ")\n";
+ my $map;
+ map { $map->{$_}++ } @ints;
+ my @result = ();
+ foreach my $key (keys %$map) {
+ if($map->{$key} % 2) {
+ print "Output: ()\n";
+ return;
+ }
+ my $i = int( $map->{$key} / 2 );
+ foreach my $elem (1..$i) {
+ push @result, [ $key, $key ];
+ }
+ }
+ my $first = 1;
+ print "Output: ";
+ foreach my $elem (@result) {
+ if($first) {
+ $first = 0;
+ } else {
+ print ", ";
+ }
+ print "(" . join(", ", @$elem) . ")";
+ }
+ print "\n";
+}
+
+
diff --git a/challenge-249/jeanluc2020/perl/ch-2.pl b/challenge-249/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..7407eaacbe
--- /dev/null
+++ b/challenge-249/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/#TASK2
+#
+# Task 2: DI String Match
+# =======================
+#
+# You are given a string s, consisting of only the characters "D" and "I".
+#
+# Find a permutation of the integers [0 .. length(s)] such that for each
+# character s[i] in the string:
+#
+# s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+# s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+#
+## Example 1
+##
+## Input: $str = "IDID"
+## Output: (0, 4, 1, 3, 2)
+#
+## Example 2
+##
+## Input: $str = "III"
+## Output: (0, 1, 2, 3)
+#
+## Example 3
+##
+## Input: $str = "DDI"
+## Output: (3, 2, 0, 1)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just count from 0 to length(s) for each "I" and from
+# length(s) to 0 for each "D", and in the end add the last
+# element we didn't use up yet.
+
+use strict;
+use warnings;
+
+DI_string_match("IDID");
+DI_string_match("III");
+DI_string_match("DDI");
+
+sub DI_string_match {
+ my $str = shift;
+ print "Input: '$str'\n";
+ my $upper = length($str);
+ my $lower = 0;
+ my @result = ();
+ foreach my $char (split//,$str) {
+ if($char eq "I") {
+ push @result, $lower++;
+ } else {
+ push @result, $upper--;
+ }
+ }
+ push @result, $lower;
+ print "Output: (", join(", ", @result), ")\n";
+}
diff --git a/challenge-249/jeanluc2020/python/ch-1.py b/challenge-249/jeanluc2020/python/ch-1.py
new file mode 100755
index 0000000000..d187c0ca7d
--- /dev/null
+++ b/challenge-249/jeanluc2020/python/ch-1.py
@@ -0,0 +1,72 @@
+#!/usr/bin/env python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/#TASK1
+#
+# Task 1: Shortest Distance
+# =========================
+#
+# You are given an array of integers with even number of elements.
+#
+# Write a script to divide the given array into equal pairs such that:
+#
+# a) Each element belongs to exactly one pair.
+# b) The elements present in a pair are equal.
+#
+#
+## Example 1
+##
+## Input: @ints = (3, 2, 3, 2, 2, 2)
+## Output: (2, 2), (3, 3), (2, 2)
+##
+## There are 6 elements in @ints.
+## They should be divided into 6 / 2 = 3 pairs.
+## @ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the
+## conditions.
+#
+## Example 2
+##
+## Input: @ints = (1, 2, 3, 4)
+## Output: ()
+##
+## There is no way to divide @ints 2 pairs such that the pairs satisfy every
+## condition.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We use the elements of the array as keys for a hash in which we
+# count the amount of elements of this value in the array.
+# If in the end, all values in the hash are even, we can spit out the
+# correct number of arrays, otherwise we can only return an empty array.
+
+def shortest_distance(ints: list) -> list:
+ print("Input: (", ", ".join([str(x) for x in ints]), ")")
+ result = []
+ map = {}
+ for i in ints:
+ if i in map:
+ map[i] += 1
+ else:
+ map[i] = 1
+ for key in map.keys():
+ if map[key] % 2 != 0:
+ print("Output: ()")
+ return ()
+ i = int( map[key] / 2 )
+ for j in range(1,i+1):
+ result.append( [key, key] )
+ first = True
+ print("Output: ", end="")
+ for elem in result:
+ if first:
+ first = False
+ else:
+ print(", ", end="")
+ print(", ".join([str(x) for x in elem]), end="")
+ print("")
+
+shortest_distance([3, 2, 3, 2, 2, 2])
+shortest_distance([1, 2, 3, 4])
+
diff --git a/challenge-249/jeanluc2020/python/ch-2.py b/challenge-249/jeanluc2020/python/ch-2.py
new file mode 100755
index 0000000000..504e70a26d
--- /dev/null
+++ b/challenge-249/jeanluc2020/python/ch-2.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/#TASK2
+#
+# Task 2: DI String Match
+# =======================
+#
+# You are given a string s, consisting of only the characters "D" and "I".
+#
+# Find a permutation of the integers [0 .. length(s)] such that for each
+# character s[i] in the string:
+#
+# s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+# s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+#
+## Example 1
+##
+## Input: $str = "IDID"
+## Output: (0, 4, 1, 3, 2)
+#
+## Example 2
+##
+## Input: $str = "III"
+## Output: (0, 1, 2, 3)
+#
+## Example 3
+##
+## Input: $str = "DDI"
+## Output: (3, 2, 0, 1)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We just count from 0 to length(s) for each "I" and from
+# length(s) to 0 for each "D", and in the end add the last
+# element we didn't use up yet.
+
+def DI_string_match(string: str) -> list:
+ print("Input: '", string, "'", sep="")
+ upper = len(string)
+ lower = 0
+ result = []
+ for char in list(string):
+ if char == "I":
+ result.append(lower)
+ lower += 1
+ else:
+ result.append(upper)
+ upper -= 1
+ result.append(lower)
+ print("Output: (", ", ".join([str(x) for x in result]), ")")
+ return result
+
+DI_string_match("IDID");
+DI_string_match("III");
+DI_string_match("DDI");
+