From 10fc9dabcfe664fabf775ff00876adcddc7d77bc Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 10 Feb 2025 07:29:32 +0000 Subject: w308 - Task 1 & 2 --- challenge-308/perlboy1967/perl/ch1.pl | 46 +++++++++++++++++++++++++++++++++++ challenge-308/perlboy1967/perl/ch2.pl | 37 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 challenge-308/perlboy1967/perl/ch1.pl create mode 100755 challenge-308/perlboy1967/perl/ch2.pl 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 + +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 + +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; -- cgit