diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-03 20:04:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-03 20:04:34 +0100 |
| commit | e4d06675f412aa1cc14a0b3685cc5c5e23027224 (patch) | |
| tree | f6e7655ff42b03a6b413abe5aa9410f81748757d | |
| parent | 2fd16147c5070fc6fb671ea7c955c68aa41f0729 (diff) | |
| parent | 44fa7a0afffff7d028e0dc7523044ce9f40647fe (diff) | |
| download | perlweeklychallenge-club-e4d06675f412aa1cc14a0b3685cc5c5e23027224.tar.gz perlweeklychallenge-club-e4d06675f412aa1cc14a0b3685cc5c5e23027224.tar.bz2 perlweeklychallenge-club-e4d06675f412aa1cc14a0b3685cc5c5e23027224.zip | |
Merge pull request #12783 from wanderdoc/master
pwc 341 (wanderdoc)
| -rw-r--r-- | challenge-341/wanderdoc/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-341/wanderdoc/perl/ch-2.pl | 55 |
2 files changed, 117 insertions, 0 deletions
diff --git a/challenge-341/wanderdoc/perl/ch-1.pl b/challenge-341/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..f61d965de6 --- /dev/null +++ b/challenge-341/wanderdoc/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string containing English letters only and also you are given broken keys. +Write a script to return the total words in the given sentence can be typed completely. + +Example 1 + +Input: $str = 'Hello World', @keys = ('d') +Output: 1 + +With broken key 'd', we can only type the word 'Hello'. + + +Example 2 + +Input: $str = 'apple banana cherry', @keys = ('a', 'e') +Output: 0 + + +Example 3 + +Input: $str = 'Coding is fun', @keys = () +Output: 3 + +No keys broken. + + +Example 4 + +Input: $str = 'The Weekly Challenge', @keys = ('a','b') +Output: 2 + + +Example 5 + +Input: $str = 'Perl and Python', @keys = ('p') +Output: 1 + +=cut + + +use Test2::V0 -no_srand => 1; +is(can_print('Hello World', ['d']), 1, 'Example 1'); +is(can_print('apple banana cherry', ['a', 'e']), 0, 'Example 2'); +is(can_print('coding is fun', []), 3, 'Example 3'); +is(can_print('The Weekly Challenge', ['a','b']), 2, 'Example 4'); +is(can_print('Perl and Python', ['p']), 1, 'Example 5'); +done_testing(); + + + + +sub can_print +{ + my ($str, $keys_aref) = @_; + my $keys_str = join('|', @$keys_aref); + my $keys_re = length($keys_str) ? qr/$keys_str/i : qr/ /; + return scalar(grep {!/$keys_re/} split(/\s/, $str)); +} diff --git a/challenge-341/wanderdoc/perl/ch-2.pl b/challenge-341/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..245dfb5c22 --- /dev/null +++ b/challenge-341/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given a string, $str and a character in the given string, $char. +Write a script to reverse the prefix upto the first occurrence of the given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + +=cut + + + + +use Test2::V0 -no_srand => 1; + +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"); +done_testing(); + +sub reverse_prefix +{ + my ($str, $char) = @_; + my $idx = index($str, $char, 0); + return reverse(substr($str, 0, $idx + 1)) . substr($str, $idx + 1); +} |
