DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Clearly the Title

Weekly Challenge 330

Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. It's a great way for us all to practice some coding.

Challenge, My solutions

Sorry about the lack of blogs and Perl solutions over the past few weeks. I've been moving interstate (back home to Brisbane), and my poor little netbook (with 4 GB of RAM) doesn't like VS Code too much.

Task 1: Clear Digits

Task

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.

My solution

Naturally this is a task where regular expressions are going to be used. The Python and Perl solutions follow the same logic, but are implemented differently.

In Python, strings are immutable (i.e. they can't be changed). For this task, I start by setting the string solution to the input string. I then have an infinite loop.

For each loop, I remove the first pair of a lower case letter followed by a digit, and set the new_string variable with the result. If this is the same as the solution variable, no substitution was performed and I return the solution variable.

If the strings are different, I set solution to new_string, and the loop is called again.

def clear_digits(input_string) -> str:
    solution = input_string

    while True:
        new_string = re.sub(r'[a-z][0-9]', '', solution)
        if new_string == solution:
            return solution

        solution = new_string
Enter fullscreen mode Exit fullscreen mode

In Perl, strings are mutable, and thus the solution is a little easier. The substitution operator returns the number of changes made, and there is no need to have an intermediate new_string variable.

sub main ($input_string) {
    my $solution = $input_string;

    my $changes = 1;
    while ($changes) {
        $changes = ( $solution =~ s/[a-z][0-9]// );
    }

    say '"', $solution, '"';
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py cab12
"c"

$ ./ch-1.py xy99
""

$ ./ch-1.py pa1erl
"perl"
Enter fullscreen mode Exit fullscreen mode

Task 2: Title Capital

Task

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.

My solution

For this task, I create a list (array in Perl) called words which has the lower cased version of the input string split by spaces. In Python, I use list comprehension to call the capitalize function to capitalize the first letter in each word if it is longer than two characters. Finally, I join the words list with spaces to return the solution.

def title_capital(input_string: str) -> str:
    words = input_string.lower().split()

    new_words = [
        word.capitalize() if len(word) > 2 else word
        for word in words
    ]

    return ' '.join(new_words)
Enter fullscreen mode Exit fullscreen mode

The Perl solution uses the same logic, and uses the map function to mimic list comprehension. The ucfirst function will capitalize the first letter of each word.

sub main ($input_string) {
    my @words = split / /, lc $input_string;

    my @new_words = map { length($_) > 2 ? ucfirst($_) : $_ } @words;

    say '"', join( ' ', @new_words ), '"';
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py "PERL IS gREAT"
"Perl is Great"

$ ./ch-2.py "THE weekly challenge"
"The Weekly Challenge"

$ ./ch-2.py "YoU ARE A stAR"
"You Are a Star"
Enter fullscreen mode Exit fullscreen mode

Top comments (0)