From bfd1832bbbda9d5b99459af6bf5a28e180316a99 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 24 Jun 2024 07:55:57 +0000 Subject: w275 - Task 1 & 2 --- challenge-275/perlboy1967/perl/ch1.pl | 35 ++++++++++++++++++++++++++++++ challenge-275/perlboy1967/perl/ch2.pl | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 challenge-275/perlboy1967/perl/ch1.pl create mode 100755 challenge-275/perlboy1967/perl/ch2.pl diff --git a/challenge-275/perlboy1967/perl/ch1.pl b/challenge-275/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..624b8fb931 --- /dev/null +++ b/challenge-275/perlboy1967/perl/ch1.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 275 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-275 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Broken Keys +Submitted by: Mohammad Sajid Anwar + +You are given a sentence, $sentence and list of broken keys @keys. + +Write a script to find out how many words can be typed fully. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub brokenKeys ($sentence,@keys) { + my $re = '['.join('',@keys).']'; + scalar grep !/$re/i, split /\s+/, $sentence; +} + +is(brokenKeys('Perl Weekly Challenge','l','a'),0); +is(brokenKeys('Perl and Raku','a'),1); +is(brokenKeys('Well done Team PWC','l','o'),2); +is(brokenKeys('The joys of polyglottism','T'),2); + +done_testing; diff --git a/challenge-275/perlboy1967/perl/ch2.pl b/challenge-275/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..ccd347c497 --- /dev/null +++ b/challenge-275/perlboy1967/perl/ch2.pl @@ -0,0 +1,41 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 275 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-275 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Replace Digits +Submitted by: Mohammad Sajid Anwar + +You are given an alphanumeric string, $str, where each character +is either a letter or a digit. + +Write a script to replace each digit in the given string with the +value of the previous letter plus (digit) places. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub replaceDigits ($str) { + my $r; + while ($str =~ s#^(?[a-z])(?\d+)##i) { + $r .= $+{c} . join('', map { chr(ord($+{c})+$_) } split(//,$+{d})); + } + return $r.$str; +} + +is(replaceDigits('a1c1e1'),'abcdef','Example 1'); +is(replaceDigits('a1b2c3d4'),'abbdcfdh','Example 2'); +is(replaceDigits('b2b'),'bdb','Example 3'); +is(replaceDigits('a16z'),'abgz','Example 4'); +is(replaceDigits('Z5'),'Z_','Own test'); + +done_testing; -- cgit