diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-10 20:15:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-10 20:15:53 +0000 |
| commit | d4b0508b9e150eab1a2637be4940c88ebd46915c (patch) | |
| tree | c23e70e7ec8e61e3b73d50197a1d37d929192391 | |
| parent | 1eea7fd0443934132efbfa409195e80b722ef107 (diff) | |
| parent | 10fc9dabcfe664fabf775ff00876adcddc7d77bc (diff) | |
| download | perlweeklychallenge-club-d4b0508b9e150eab1a2637be4940c88ebd46915c.tar.gz perlweeklychallenge-club-d4b0508b9e150eab1a2637be4940c88ebd46915c.tar.bz2 perlweeklychallenge-club-d4b0508b9e150eab1a2637be4940c88ebd46915c.zip | |
Merge pull request #11554 from PerlBoy1967/branch-for-challenge-308
w308 - Task 1 & 2
| -rwxr-xr-x | challenge-308/perlboy1967/perl/ch1.pl | 46 | ||||
| -rwxr-xr-x | challenge-308/perlboy1967/perl/ch2.pl | 37 |
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; |
