aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-02-10 20:17:36 +0000
committerGitHub <noreply@github.com>2025-02-10 20:17:36 +0000
commite783380ee96e5ab55795035651aa85f5b13a18b7 (patch)
treed4fe5d341ef4380a13d795bf5900c8853e394c97
parentfd267e873db4e0567c63c81a0bdbc36340d66c96 (diff)
parent2df42f21a8df42b165bef0bc76fe8d8d394337b0 (diff)
downloadperlweeklychallenge-club-e783380ee96e5ab55795035651aa85f5b13a18b7.tar.gz
perlweeklychallenge-club-e783380ee96e5ab55795035651aa85f5b13a18b7.tar.bz2
perlweeklychallenge-club-e783380ee96e5ab55795035651aa85f5b13a18b7.zip
Merge pull request #11558 from pme/challenge-308
challenge-308
-rwxr-xr-xchallenge-308/peter-meszaros/perl/ch-1.pl56
-rwxr-xr-xchallenge-308/peter-meszaros/perl/ch-2.pl54
-rwxr-xr-xchallenge-308/peter-meszaros/tcl/ch-1.tcl62
-rwxr-xr-xchallenge-308/peter-meszaros/tcl/ch-2.tcl54
4 files changed, 226 insertions, 0 deletions
diff --git a/challenge-308/peter-meszaros/perl/ch-1.pl b/challenge-308/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..4fb0bd0714
--- /dev/null
+++ b/challenge-308/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Count Common
+
+Submitted by: Mohammad Sajid Anwar
+
+You are given two array of strings, @str1 and @str2.
+
+Write a script to return the count of common strings in both arrays.
+
+=head2 Example 1
+
+ Input: @str1 = ("perl", "weekly", "challenge")
+ @str2 = ("raku", "weekly", "challenge")
+ Output: 2
+
+=head2 Example 2
+
+ Input: @str1 = ("perl", "raku", "python")
+ @str2 = ("python", "java")
+ Output: 1
+
+=head2 Example 3
+
+ Input: @str1 = ("guest", "contribution")
+ @str2 = ("fun", "weekly", "challenge")
+ Output: 0
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[["perl", "weekly", "challenge"], ["raku", "weekly", "challenge"]], 2, "Example 1"],
+ [[["perl", "raku", "python"], ["python", "java"]], 1, "Example 2"],
+ [[["guest", "contribution"], ["fun", "weekly", "challenge"]], 0, "Example 3"],
+];
+
+sub count_common
+{
+ my $str1 = $_[0]->[0];
+ my $str2 = $_[0]->[1];
+
+ my %h1 = map {$_ => 1} @$str1;
+ return grep { $h1{$_} } @$str2;
+}
+
+for (@$cases) {
+ is(count_common($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-308/peter-meszaros/perl/ch-2.pl b/challenge-308/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..9e6ae45d81
--- /dev/null
+++ b/challenge-308/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: Decode XOR
+
+Submitted by: Mohammad Sajid Anwar
+
+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].
+
+=head2 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
+
+=head2 Example 2
+
+ Input: @encoded = (6, 2, 7, 3), $initial = 4
+ Output: (4, 2, 0, 7, 4)
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[1, 2, 3], 1], [1, 0, 2, 1], 'Example 1'],
+ [[[6, 2, 7, 3], 4], [4, 2, 0, 7, 4], 'Example 2'],
+];
+
+sub decode_xor
+{
+ my $encoded = $_[0]->[0];
+ my $initial = $_[0]->[1];
+
+ my @orig = ($initial);
+ push @orig, $encoded->[$_] ^ $orig[-1] for 0 .. $#$encoded;
+ return \@orig;
+}
+
+for (@$cases) {
+ is(decode_xor($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-308/peter-meszaros/tcl/ch-1.tcl b/challenge-308/peter-meszaros/tcl/ch-1.tcl
new file mode 100755
index 0000000000..e97b23ad3e
--- /dev/null
+++ b/challenge-308/peter-meszaros/tcl/ch-1.tcl
@@ -0,0 +1,62 @@
+#!/usr/bin/env tclsh
+#
+# Task 1: Count Common
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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
+#
+
+package require tcltest
+
+set cases {
+ {{{perl weekly challenge} {raku weekly challenge}} 2 "Example 1"}
+ {{{perl raku python} {python java}} 1 "Example 2"}
+ {{{guest contribution} {fun weekly challenge}} 0 "Example 3"}
+}
+
+proc count_common {p} {
+ set str1 [lindex $p 0]
+ set str2 [lindex $p 1]
+
+ set l [concat {*}[lmap x $str1 {list $x 1}]]
+ set h1 [dict create {*}$l]
+
+ set cnt 0
+ foreach s $str2 {
+ if {[dict exists $h1 $s]} {
+ incr cnt
+ }
+ }
+ return $cnt
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ count_common [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+
diff --git a/challenge-308/peter-meszaros/tcl/ch-2.tcl b/challenge-308/peter-meszaros/tcl/ch-2.tcl
new file mode 100755
index 0000000000..923b2070eb
--- /dev/null
+++ b/challenge-308/peter-meszaros/tcl/ch-2.tcl
@@ -0,0 +1,54 @@
+#!/usr/bin/env tclsh
+#
+# Task 2: Decode XOR
+#
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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)
+#
+
+package require tcltest
+
+set cases {
+ {{{1 2 3} 1} {1 0 2 1} "Example 1"}
+ {{{6 2 7 3} 4} {4 2 0 7 4} "Example 2"}
+}
+
+proc decode_xor {p} {
+ set encoded [lindex $p 0]
+ set initial [lindex $p 1]
+
+ set orig [list $initial]
+ foreach i $encoded {
+ lappend orig [expr {$i ^ [lindex $orig end]}]
+ }
+ return $orig
+}
+
+tcltest::configure -verbose {pass}
+foreach case $cases {
+ tcltest::test [lindex $case 2] {} {
+ decode_xor [lindex $case 0]
+ } [lindex $case 1]
+}
+
+exit 0
+