diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-06-24 22:33:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 22:33:38 +0100 |
| commit | 0b31691a71d7f2193e7f3b7c04632486720094cd (patch) | |
| tree | a99ca93970f36bbe33993222a771bea70db63fd3 | |
| parent | 2157366c850f535310231ae9c9126bd28320d02d (diff) | |
| parent | 740859cfa0649d28ec498e2d695ee33ac38e39a5 (diff) | |
| download | perlweeklychallenge-club-0b31691a71d7f2193e7f3b7c04632486720094cd.tar.gz perlweeklychallenge-club-0b31691a71d7f2193e7f3b7c04632486720094cd.tar.bz2 perlweeklychallenge-club-0b31691a71d7f2193e7f3b7c04632486720094cd.zip | |
Merge pull request #10320 from choroba/ech275
Add solutions to 275: Broken Keys & Replace Digits by E. Choroba
| -rwxr-xr-x | challenge-275/e-choroba/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-275/e-choroba/perl/ch-2.pl | 33 |
2 files changed, 51 insertions, 0 deletions
diff --git a/challenge-275/e-choroba/perl/ch-1.pl b/challenge-275/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c4cc55dee4 --- /dev/null +++ b/challenge-275/e-choroba/perl/ch-1.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub broken_keys($sentence, @keys) { + my @words = split ' ', $sentence; + my $broken = join "", @keys; + $broken = "[$broken]"; + return grep ! /$broken/i, @words +} + +use Test::More tests => 4; + +is broken_keys('Perl Weekly Challenge','l', 'a'), 0, 'Example 1'; +is broken_keys('Perl and Raku','a'), 1, 'Example 2'; +is broken_keys('Well done Team PWC','l', 'o'), 2, 'Example 3'; +is broken_keys('The joys of polyglottism','T'), 2, 'Example 4'; diff --git a/challenge-275/e-choroba/perl/ch-2.pl b/challenge-275/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..f134b98889 --- /dev/null +++ b/challenge-275/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub replace_digits($str) { + my $result = ""; + my $letter; + for my $char (split //, $str) { + if ($char =~ /[0-9]/) { + return "" unless $letter; + + my $ord = $char + ord $letter; + return "" if $ord > ord 'z'; + + $result .= chr $ord; + } else { + $result .= $letter = $char; + } + } + return $result +} + +use Test::More tests => 4 + 3; + +is replace_digits('a1c1e1'), 'abcdef', 'Example 1'; +is replace_digits('a1b2c3d4'), 'abbdcfdh', 'Example 2'; +is replace_digits('b2b'), 'bdb', 'Example 3'; +is replace_digits('a16z'), 'abgz', 'Example 4'; + +is replace_digits('a00b'), 'aaab', 'Zero'; +is replace_digits('1a'), "", 'Start with a digit'; +is replace_digits('z01'), "", 'Overflow'; |
