The Bear's Den

Enter at your own risk

Broken Prefix

Task 1: Broken Keyboard

Submitted by: Mohammad Sajid Anwar


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

Solution

Though this task is a duplicate of task 1 from week 275, it turns out that my former solution does not comply to this week’s examples. Specifically, my old solution cannot handle the case of no broken keys.

So here is a slightly modified version.

use strict;
use warnings;

sub broken_keys {
    my $typeable = qr{^[^\s@{+shift}]+$}ixx;

    grep /$typeable/, split /\s+/, shift;
}

See the full solution to task 1.

Task 2: Reverse Prefix

Submitted by: Mohammad Sajid Anwar


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"

Solution

Search for $char within $str and reverse the found prefix. $chr is not limited to a single character.

use strict;
use warnings;

sub reverse_prefix ($chr, $str) {
    $str =~ s/.*?$chr/reverse $&/er;
}

See the full solution to task 2.


If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.