aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-02-10 20:18:41 +0000
committerGitHub <noreply@github.com>2025-02-10 20:18:41 +0000
commite07087bddde35418e837ff415c75448272b90f73 (patch)
tree3fd6bdce57a5243692ddaff9f7ea01678a707dba
parentddeb3e1f986f901280cff456bda31803fc803a99 (diff)
parent32408e5920796a1b821719f015409e6b88fd302c (diff)
downloadperlweeklychallenge-club-e07087bddde35418e837ff415c75448272b90f73.tar.gz
perlweeklychallenge-club-e07087bddde35418e837ff415c75448272b90f73.tar.bz2
perlweeklychallenge-club-e07087bddde35418e837ff415c75448272b90f73.zip
Merge pull request #11561 from jeanluc2020/jeanluc2020-308
Add solution 308
-rw-r--r--challenge-308/jeanluc2020/blog-1.txt1
-rw-r--r--challenge-308/jeanluc2020/blog-2.txt1
-rwxr-xr-xchallenge-308/jeanluc2020/perl/ch-1.pl60
-rwxr-xr-xchallenge-308/jeanluc2020/perl/ch-2.pl60
4 files changed, 122 insertions, 0 deletions
diff --git a/challenge-308/jeanluc2020/blog-1.txt b/challenge-308/jeanluc2020/blog-1.txt
new file mode 100644
index 0000000000..b9d9d1f5aa
--- /dev/null
+++ b/challenge-308/jeanluc2020/blog-1.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-308-1.html
diff --git a/challenge-308/jeanluc2020/blog-2.txt b/challenge-308/jeanluc2020/blog-2.txt
new file mode 100644
index 0000000000..5887315a93
--- /dev/null
+++ b/challenge-308/jeanluc2020/blog-2.txt
@@ -0,0 +1 @@
+http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-308-2.html
diff --git a/challenge-308/jeanluc2020/perl/ch-1.pl b/challenge-308/jeanluc2020/perl/ch-1.pl
new file mode 100755
index 0000000000..12b229d2a2
--- /dev/null
+++ b/challenge-308/jeanluc2020/perl/ch-1.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-308/#TASK1
+#
+# Task 1: Count Common
+# ====================
+#
+# You are given two array of strings, @str1 and @str2.
+#
+# Write a script to return the count of common strings in both arrays.
+#
+## Example 1
+##
+## Input: @str1 = ("perl", "weekly", "challenge")
+## @str2 = ("raku", "weekly", "challenge")
+## Output: 2
+#
+## Example 2
+##
+## Input: @str1 = ("perl", "raku", "python")
+## @str2 = ("python", "java")
+## Output: 1
+#
+## Example 3
+##
+## Input: @str1 = ("guest", "contribution")
+## @str2 = ("fun", "weekly", "challenge")
+## Output: 0
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's make sure that both arrays don't have duplicate entries. Then
+# we can count all occurrences for all elements, which is either 1 or 2.
+# In the latter case, we can add 1 to our result.
+
+use v5.36;
+use List::Util qw(uniq);
+
+count_common( ["perl", "weekly", "challenge"], ["raku", "weekly", "challenge"]);
+count_common( ["perl", "raku", "python"], ["python", "java"] );
+count_common( ["guest", "contribution"], ["fun", "weekly", "challenge"] );
+
+sub count_common {
+ my ($str1, $str2) = @_;
+ print "Input: (" . join(", ", @$str1) . "), (" . join(", ", @$str2) . ")\n";
+ my @str1 = uniq(@$str1);
+ my @str2 = uniq(@$str2);
+ my $tmp;
+ my $result = 0;
+ foreach my $elem ( (@str1, @str2) ) {
+ $tmp->{$elem}++;
+ if($tmp->{$elem} > 1) {
+ $result++;
+ }
+ }
+ print "Output: $result\n";
+}
diff --git a/challenge-308/jeanluc2020/perl/ch-2.pl b/challenge-308/jeanluc2020/perl/ch-2.pl
new file mode 100755
index 0000000000..db82c2cebd
--- /dev/null
+++ b/challenge-308/jeanluc2020/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-308/#TASK2
+#
+# Task 2: Decode XOR
+# ==================
+#
+# You are given an encoded array and an initial integer.
+#
+# Write a script to find the original array that produced the given encoded
+# array. It was encoded such that encoded[i] = orig[i] XOR orig[i + 1].
+#
+## Example 1
+##
+## Input: @encoded = (1, 2, 3), $initial = 1
+## Output: (1, 0, 2, 1)
+##
+## Encoded array created like below, if the original array was (1, 0, 2, 1)
+## $encoded[0] = (1 xor 0) = 1
+## $encoded[1] = (0 xor 2) = 2
+## $encoded[2] = (2 xor 1) = 3
+#
+## Example 2
+##
+## Input: @encoded = (6, 2, 7, 3), $initial = 4
+## Output: (4, 2, 0, 7, 4)
+#
+############################################################
+##
+## discussion
+##
+############################################################
+#
+# Let's have a look at bitwise xor:
+# a xor b = c
+# 0 0 = 0
+# 1 0 = 1
+# 0 1 = 1
+# 1 1 = 0
+# We can see: a xor b = c <=> a = b xor c
+# In other words, we can calculate the next numer in the orig array by
+# calculating the last found element in the orig array and the corresponding
+# element in the encoded array. Since the first element of orig is given,
+# the solution is well-defined.
+
+use v5.36;
+
+decode_xor(1, (1, 2, 3));
+decode_xor(4, (6, 2, 7, 3));
+
+sub decode_xor {
+ my ($initial, @encoded) = @_;
+ print "Input: (" . join(", ", @encoded) . "), $initial\n";
+ my @result = ($initial);
+ foreach my $elem (@encoded) {
+ my $last = $result[$#result];
+ my $r = $last ^ $elem;
+ push @result, $r;
+ }
+ print "Output: (" . join(", ", @result) . ")\n";
+}