From 675c4080313e477512dba05df0da1e43cd773b9c Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 29 Sep 2025 08:41:16 +0200 Subject: Add solutions to 341: Broken Keyboard & Reverse Prefix by E. Choroba --- challenge-341/e-choroba/perl/ch-1.pl | 20 ++++++++++++++++++++ challenge-341/e-choroba/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100755 challenge-341/e-choroba/perl/ch-1.pl create mode 100755 challenge-341/e-choroba/perl/ch-2.pl 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'; -- cgit