aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-03-18 21:19:18 +0000
committerGitHub <noreply@github.com>2023-03-18 21:19:18 +0000
commitb25a057c64870d936ec8d1891797b4cbc668fe5d (patch)
treeecf87b96a9c0609137d72c7c48c65d1234986516
parent71e907b5f05ddb891af9b2a3137bf1d0270c8adc (diff)
parent3d81eb4fc51a3963c8e032602f0434fbd00d5408 (diff)
downloadperlweeklychallenge-club-b25a057c64870d936ec8d1891797b4cbc668fe5d.tar.gz
perlweeklychallenge-club-b25a057c64870d936ec8d1891797b4cbc668fe5d.tar.bz2
perlweeklychallenge-club-b25a057c64870d936ec8d1891797b4cbc668fe5d.zip
Merge pull request #7731 from jeanluc2020/jeanluc-208
Add solution for week 208
-rw-r--r--challenge-208/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-208/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-208/jeanluc2020/perl/ch-1.pl107
-rwxr-xr-xchallenge-208/jeanluc2020/perl/ch-2.pl70
4 files changed, 179 insertions, 0 deletions
diff --git a/challenge-208/jeanluc2020/blog-1.txt b/challenge-208/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..fb31fe351f
--- /dev/null
+++ b/challenge-208/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-208-1.html
diff --git a/challenge-208/jeanluc2020/blog-2.txt b/challenge-208/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..020e78afd7
--- /dev/null
+++ b/challenge-208/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-208-2.html
diff --git a/challenge-208/jeanluc2020/perl/ch-1.pl b/challenge-208/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..6328e21a4b
--- /dev/null
+++ b/challenge-208/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,107 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-208/#TASK1
+#
+# Task 1: Minimum Index Sum
+# =========================
+#
+# You are given two arrays of strings.
+#
+# Write a script to find out all common strings in the given two arrays with minimum index sum. If no common strings found returns an empty list.
+#
+## Example 1
+##
+## Input: @list1 = ("Perl", "Raku", "Love")
+## @list2 = ("Raku", "Perl", "Hate")
+##
+## Output: ("Perl", "Raku")
+##
+## There are two common strings "Perl" and "Raku".
+## Index sum of "Perl": 0 + 1 = 1
+## Index sum of "Raku": 1 + 0 = 1
+#
+## Example 2
+##
+## Input: @list1 = ("A", "B", "C")
+## @list2 = ("D", "E", "F")
+##
+## Output: ()
+##
+## No common string found, so no result.
+#
+## Example 3
+##
+## Input: @list1 = ("A", "B", "C")
+## @list2 = ("C", "A", "B")
+##
+## Output: ("A")
+##
+## There are three common strings "A", "B" and "C".
+## Index sum of "A": 0 + 1 = 1
+## Index sum of "B": 1 + 2 = 3
+## Index sum of "C": 2 + 0 = 2
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# We have to find the index for every string in both arrays.
+# Then we find the index sum for each string.
+# Then we find the minimum value for the sums.
+# Then we output every string that has this minimum value as its sum.
+
+use strict;
+use warnings;
+use List::Util qw(min);
+
+index_sum( ["Perl", "Raku", "Love"], ["Raku", "Perl", "Hate"] );
+index_sum( ["A", "B", "C"], ["D", "E", "F"] );
+index_sum( ["A", "B", "C"], ["C", "A", "B"] );
+
+sub index_sum {
+ my ($l1, $l2) = @_;
+ my @list1 = @$l1;
+ my @list2 = @$l2;
+ my @result = ();
+ my $index_data = {};
+ print "Input: (" . join(", ", @list1) . "); (" . join(", ", @list2) . ")\n";
+ # find the index for every string in list1
+ foreach my $i (0..$#list1) {
+ my $value = $list1[$i];
+ $index_data->{$value}->{"list1_index"} = $i;
+ }
+ # find the index for every string in list2
+ foreach my $j (0..$#list2) {
+ my $value = $list2[$j];
+ $index_data->{$value}->{"list2_index"} = $j;
+ }
+ # for each found string, if it exists in both lists, calculate the sum
+ my @sums = ();
+ foreach my $v (keys %$index_data) {
+ if(defined($index_data->{$v}->{"list1_index"})
+ && defined($index_data->{$v}->{"list2_index"})) {
+ $index_data->{$v}->{"sum"} =
+ $index_data->{$v}->{"list1_index"}
+ + $index_data->{$v}->{"list2_index"};
+ push @sums, $index_data->{$v}->{"sum"};
+ }
+ }
+ # calculate the minimum sum
+ my $minimum = min(@sums);
+ # find all values that share the minimum index sum
+ foreach my $v (keys %$index_data) {
+ if(defined($index_data->{$v}->{"sum"})) {
+ if($index_data->{$v}->{"sum"} == $minimum) {
+ push @result, $v;
+ }
+ }
+ }
+ # in order to avoid a random sort order of the results we sort by
+ # their index in list1
+ print "Output: (" . join(", ",
+ sort {
+ $index_data->{ $a }->{"list1_index"} <=>
+ $index_data->{ $b }->{"list1_index"} }
+ @result) . ")\n";
+}
diff --git a/challenge-208/jeanluc2020/perl/ch-2.pl b/challenge-208/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..00eda8c66f
--- /dev/null
+++ b/challenge-208/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-208/#TASK2
+#
+# Task 2: Duplicate and Missing
+# =============================
+#
+# You are given an array of integers in sequence with one missing and one duplicate.
+#
+# Write a script to find the duplicate and missing integer in the given array. Return -1 if none found.
+#
+# For the sake of this task, let us assume the array contains no more than one duplicate and missing.
+#
+## Example 1:
+##
+## Input: @nums = (1,2,2,4)
+## Output: (2,3)
+##
+## Duplicate is 2 and Missing is 3.
+#
+## Example 2:
+##
+## Input: @nums = (1,2,3,4)
+## Output: -1
+##
+## No duplicate and missing found.
+#
+## Example 3:
+##
+## Input: @nums = (1,2,3,3)
+## Output: (3,4)
+##
+## Duplicate is 3 and Missing is 4.
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Walk the array, keep track of the previous element, and
+# check where we're at with the current one.
+
+use strict;
+use warnings;
+
+duplicate_and_missing(1,2,2,4);
+duplicate_and_missing(1,2,3,4);
+duplicate_and_missing(1,2,3,3);
+
+sub duplicate_and_missing {
+ my @nums = @_;
+ my $duplicate;
+ my $missing;
+ print "Input: (" . join(",", @nums) . ")\n";
+ my $last_element = shift @nums;
+ foreach my $element (@nums) {
+ if($element == $last_element) {
+ $duplicate = $element;
+ $missing = $element+1;
+ } elsif ($element > $last_element+1) {
+ $missing = $last_element+1;
+ }
+ $last_element = $element;
+ }
+ if(defined($duplicate) && defined($missing)) {
+ print "Output: ($duplicate, $missing)\n";
+ } else {
+ print "Output: -1\n";
+ }
+}