diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-29 12:36:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-29 12:36:43 +0100 |
| commit | b5361a652c4e9023491f1963d53c8bef393fb4a2 (patch) | |
| tree | f5b1a7912fe66bf9b119376f8d5f4035d498d245 | |
| parent | 7e86795bf3d5b70910b7c554157184ef2c2aa560 (diff) | |
| parent | 675c4080313e477512dba05df0da1e43cd773b9c (diff) | |
| download | perlweeklychallenge-club-b5361a652c4e9023491f1963d53c8bef393fb4a2.tar.gz perlweeklychallenge-club-b5361a652c4e9023491f1963d53c8bef393fb4a2.tar.bz2 perlweeklychallenge-club-b5361a652c4e9023491f1963d53c8bef393fb4a2.zip | |
Merge pull request #12751 from choroba/ech341
Add solutions to 341: Broken Keyboard & Reverse Prefix by E. Choroba
| -rwxr-xr-x | challenge-341/e-choroba/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-341/e-choroba/perl/ch-2.pl | 20 |
2 files changed, 40 insertions, 0 deletions
diff --git a/challenge-341/e-choroba/perl/ch-1.pl b/challenge-341/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..473871cfa8 --- /dev/null +++ b/challenge-341/e-choroba/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub broken_keyboard($str, @keys) { + my @words = split ' ', $str; + return scalar @words unless @keys; + + my $regex = join "", @keys; + return grep ! /[\Q$regex\E]/i, @words +} + +use Test::More tests => 5; + +is broken_keyboard('Hello World', 'd'), 1, 'Example 1'; +is broken_keyboard('apple banana cherry', 'a', 'e'), 0, 'Example 2'; +is broken_keyboard('Coding is fun'), 3, 'Example 3'; +is broken_keyboard('The Weekly Challenge', 'a','b'), 2, 'Example 4'; +is broken_keyboard('Perl and Python', 'p'), 1, 'Example 5'; diff --git a/challenge-341/e-choroba/perl/ch-2.pl b/challenge-341/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..063cbf7614 --- /dev/null +++ b/challenge-341/e-choroba/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub reverse_prefix($str, $char) { + my $i = 1 + index $str, $char; + return $str if 1 >= $i; + + substr $str, 0, $i, reverse substr $str, 0, $i; + return $str +} + +use Test::More tests => 5; + +is reverse_prefix('programming', 'g'), 'gorpramming', 'Example 1'; +is reverse_prefix('hello', 'h'), 'hello', 'Example 2'; +is reverse_prefix('abcdefghij', 'h'), 'hgfedcbaij', 'Example 3'; +is reverse_prefix('reverse', 's'), 'srevere', 'Example 4'; +is reverse_prefix('perl', 'r'), 'repl', 'Example 5'; |
