diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2024-06-24 07:55:57 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2024-06-24 07:55:57 +0000 |
| commit | bfd1832bbbda9d5b99459af6bf5a28e180316a99 (patch) | |
| tree | 5b4d316b26bff86a0b880f092634443b7d6c0c5b | |
| parent | e706d2548a3ce7ff5d3ae480224ee3ee6c5577de (diff) | |
| download | perlweeklychallenge-club-bfd1832bbbda9d5b99459af6bf5a28e180316a99.tar.gz perlweeklychallenge-club-bfd1832bbbda9d5b99459af6bf5a28e180316a99.tar.bz2 perlweeklychallenge-club-bfd1832bbbda9d5b99459af6bf5a28e180316a99.zip | |
w275 - Task 1 & 2
| -rwxr-xr-x | challenge-275/perlboy1967/perl/ch1.pl | 35 | ||||
| -rwxr-xr-x | challenge-275/perlboy1967/perl/ch2.pl | 41 |
2 files changed, 76 insertions, 0 deletions
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#^(?<c>[a-z])(?<d>\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; |
