The Bear's Den

Enter at your own risk

Capital Cleared

Task 1: Clear Digits

Submitted by: Mohammad Sajid Anwar


You are given a string containing only lower case English letters and digits.

Write a script to remove all digits by removing the first digit and the closest non-digit character to its left.

Example 1

Input: $str = "cab12"
Output: "c"

Round 1: remove "1" then "b" => "ca2"
Round 2: remove "2" then "a" => "c"

Example 2

Input: $str = "xy99"
Output: ""

Round 1: remove "9" then "y" => "x9"
Round 2: remove "9" then "x" => ""

Example 3

Input: $str = "pa1erl"
Output: "perl"

Solution

Repeatedly remove one digit and its preceeding non-digit.

Any character having the Unicode property “Number”, such as the superscript ¹ (U+00B9) is regarded as a digit, e.g. a¹b shall be transformed to b.

As it is not specified how to handle leading digits, it is assumed these shall be removed.

use strict;
use warnings;
use experimental 'signatures';

sub clear_digits ($str) {
    1 while $str =~ s/\P{N}?\p{N}//;
    $str;
}

See the full solution to task 1.

Task 2: Title Capital

Submitted by: Mohammad Sajid Anwar


You are given a string made up of one or more words separated by a single space.

Write a script to capitalise the given title. If the word length is 1 or 2 then convert the word to lowercase otherwise make the first character uppercase and remaining lowercase.

Example 1

Input: $str = "PERL IS gREAT"
Output: "Perl is Great"

Example 2

Input: $str = "THE weekly challenge"
Output: "The Weekly Challenge"

Example 3

Input: $str = "YoU ARE A stAR"
Output: "You Are a Star"

Solution

Convert the whole given Unicode string to lower case and title-case all words having at least three characters.

There is a subtle difference between upper case and title case for digraphs. Consider the (lower case) Bosnian letter lj (U+01C9) that has LJ (U+01C7) as upper case and Lj (U+01C8) as title case. This implementation will convert the words’ initial letters to title case.

use strict;
use warnings;

sub title_capital {
    lc(shift) =~ s/\w{3,}/\u$&/gr;
}

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.