aboutsummaryrefslogtreecommitdiff
path: root/challenge-308
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-02-10 07:29:32 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-02-10 07:29:32 +0000
commit10fc9dabcfe664fabf775ff00876adcddc7d77bc (patch)
tree25e2fd8c5c87eb370164fa5fd6b089733ef62257 /challenge-308
parentaa4b8399bdb3da0a50173fcab689bfa80b9b54e1 (diff)
downloadperlweeklychallenge-club-10fc9dabcfe664fabf775ff00876adcddc7d77bc.tar.gz
perlweeklychallenge-club-10fc9dabcfe664fabf775ff00876adcddc7d77bc.tar.bz2
perlweeklychallenge-club-10fc9dabcfe664fabf775ff00876adcddc7d77bc.zip
w308 - Task 1 & 2
Diffstat (limited to 'challenge-308')
-rwxr-xr-xchallenge-308/perlboy1967/perl/ch1.pl46
-rwxr-xr-xchallenge-308/perlboy1967/perl/ch2.pl37
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-308/perlboy1967/perl/ch1.pl b/challenge-308/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..c9009fa9f2
--- /dev/null
+++ b/challenge-308/perlboy1967/perl/ch1.pl
@@ -0,0 +1,46 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 303
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-308#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+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.
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+
+use Test2::V0 qw(-no_srand);
+
+no warnings qw(experimental::signatures);
+
+sub countCommon :prototype(\@\@) ($ar1,$ar2) {
+ my (%f1,%f2,%f3);
+ map { $f1{$_} = 1 } @$ar1;
+ map { $f2{$_} = 1 } @$ar2;
+ map { $f3{$_}++ } keys %f1, keys %f2;
+ scalar grep { $f3{$_} == 2 } keys %f3;
+}
+
+is(countCommon(@{[qw(perl weekly challenge)]},
+ @{[qw(raku weekly challenge)]}),
+ 2,'Example 1');
+is(countCommon(@{[qw(perl raku python)]},
+ @{[qw(python java)]}),
+ 1,'Example 2');
+is(countCommon(@{[qw(guest contribution)]},
+ @{[qw(fun weekly challenge)]}),
+ 0,'Example 3');
+is(countCommon(@{[1,2,2,3]},@{[2,3,3,4]}),2,'Own test');
+
+done_testing;
diff --git a/challenge-308/perlboy1967/perl/ch2.pl b/challenge-308/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..11b87c4cc7
--- /dev/null
+++ b/challenge-308/perlboy1967/perl/ch2.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 303
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-308#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+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].
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+
+use Test2::V0 qw(-no_srand);
+
+no warnings qw(experimental::signatures);
+
+sub decodeXOR ($initial,@encoded) {
+ my @l = ($initial);
+ push(@l,$l[-1] ^ $_) for (@encoded);
+ return @l;
+}
+
+is([decodeXOR(1,1,2,3)],[1,0,2,1],'Example 1');
+is([decodeXOR(4,6,2,7,3)],[4,2,0,7,4],'Example 2');
+
+done_testing;