diff options
| author | E. Choroba <choroba@matfyz.cz> | 2025-09-29 08:41:16 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2025-09-29 08:41:16 +0200 |
| commit | 675c4080313e477512dba05df0da1e43cd773b9c (patch) | |
| tree | 04d925a1933ebf2c196035673b1f6ae8107a3e14 | |
| parent | dd9dab4686fc230480c1ba07dc81492311c1d41f (diff) | |
| download | perlweeklychallenge-club-675c4080313e477512dba05df0da1e43cd773b9c.tar.gz perlweeklychallenge-club-675c4080313e477512dba05df0da1e43cd773b9c.tar.bz2 perlweeklychallenge-club-675c4080313e477512dba05df0da1e43cd773b9c.zip | |
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'; |
