From 803fe69759e355ade4977192b20866c188637f3e Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 29 Sep 2025 07:39:56 +0000 Subject: w341 - Task 1 & 2 (Perl) --- challenge-341/perlboy1967/perl/ch1.pl | 34 ++++++++++++++++++++++++++++++++++ challenge-341/perlboy1967/perl/ch2.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100755 challenge-341/perlboy1967/perl/ch1.pl create mode 100755 challenge-341/perlboy1967/perl/ch2.pl diff --git a/challenge-341/perlboy1967/perl/ch1.pl b/challenge-341/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..a59ba86df6 --- /dev/null +++ b/challenge-341/perlboy1967/perl/ch1.pl @@ -0,0 +1,34 @@ +#!/bin/perl + +=pod + +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Broken Keyboard +Submitted by: Mohammad Sajid Anwar + +You are given a string containing English letters only and also you are given broken keys. + +Write a script to return the total words in the given sentence can be typed completely. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +sub brokenKeyboard ($str,@keys) { + my @w = split /\s+/,$str; + return scalar @w if (@keys == 0); + my $re = '['.join('',@keys).']'; + scalar grep !/$re/i, @w; +} + +is(brokenKeyboard('Hello World',qw(d)),1,'Example 1'); +is(brokenKeyboard('apple banana cherry',qw(a e)),0,'Example 2'); +is(brokenKeyboard('Coding is fun',qw()),3,'Example 3'); +is(brokenKeyboard('The Weekly Challenge',qw(ab)),2,'Example 4'); +is(brokenKeyboard('Perl and Python',qw(p)),1,'Example 5'); + +done_testing; diff --git a/challenge-341/perlboy1967/perl/ch2.pl b/challenge-341/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..6869404cf3 --- /dev/null +++ b/challenge-341/perlboy1967/perl/ch2.pl @@ -0,0 +1,33 @@ +#!/bin/perl + +=pod + +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Reverse Prefix +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str and a character in the given string, $char. + +Write a script to reverse the prefix upto the first occurrence of the given +$char in the given string $str and return the new string. + +=cut + +use Test2::V0 qw(-no_srand); +use exact 'v5.32', -signatures; + +sub reversePrefix ($str,$char) { + $str = $char.reverse ($1).$2 if ($str =~ m#^(.*?)$char(.*)#); + return $str; +} + +is(reversePrefix('programming','g'),'gorpramming','Example 1'); +is(reversePrefix('hello','h'),'hello','Example 2'); +is(reversePrefix('abcdefghij','h'),'hgfedcbaij','Example 3'); +is(reversePrefix('reverse','s'),'srevere','Example 4'); +is(reversePrefix('perl','r'),'repl','Example 5'); + +done_testing; -- cgit From dcb9a699f65ea3d7663152c29bd64b8a6cf6ac0d Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 29 Sep 2025 10:44:54 +0000 Subject: Task 2 - Small performance change --- challenge-341/perlboy1967/perl/ch2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-341/perlboy1967/perl/ch2.pl b/challenge-341/perlboy1967/perl/ch2.pl index 6869404cf3..f3dbfcc126 100755 --- a/challenge-341/perlboy1967/perl/ch2.pl +++ b/challenge-341/perlboy1967/perl/ch2.pl @@ -20,7 +20,7 @@ use Test2::V0 qw(-no_srand); use exact 'v5.32', -signatures; sub reversePrefix ($str,$char) { - $str = $char.reverse ($1).$2 if ($str =~ m#^(.*?)$char(.*)#); + return reverse($1).$2 if ($str =~ m#^(.+?$char)(.*)#); return $str; } -- cgit