DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Reversing my broken keys

Weekly Challenge 341

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

Task 1: Broken Keyboard

Task

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.

My solution

On my keyboard, the only broken key is the Caps Lock key. It's lasts a couple of hours before I dislike it and remove it :)

For this challenge, I convert all the input to lower case. I then set a variable called completed_words to 0. I loop through each word in the input_string variable. If it does not contain any of the letters in the broken_keys list (array in Perl), I increment the completed_words variable.

def broken_keyboard(input_string: str, broken_keys: list[str]) -> int:
    input_string = input_string.lower()
    broken_keys = [key.lower() for key in broken_keys]

    completed_words = 0
    for word in input_string.split():
        if not any(char in broken_keys for char in word):
            completed_words += 1

    return completed_words
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic.

sub main (@array) {
    my $input_string = lc shift @array;
    my @broken_keys = map { lc } @array;

    my $completed_words = 0;
    for my $word (split ' ', $input_string) {
        if (not grep { index($word, $_) != -1 } @broken_keys) {
            $completed_words++;
        }
    }

    say $completed_words;
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py 'Hello World' d
1

$ ./ch-1.py 'apple banana cherry' a e
0

$ ./ch-1.py 'Coding is fun'
3

$ ./ch-1.py 'The Weekly Challenge' a b
2

$ ./ch-1.py 'Perl and Python' p
1
Enter fullscreen mode Exit fullscreen mode

Task 2: Reverse Prefix

Task

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.

My solution

For this challenge, I start by finding the position of the first occurrence of the char character in the input_string variable, and store it in the pos variable. If it does not exist, I throw an error.

In Python, I can obtain the reverse of the string using -1 as the third parameter to the list comprehension value.

def reverse_prefix(input_string: str, char: str) -> str:
    pos = input_string.find(char)

    if pos == -1:
        raise ValueError(f"Character '{char}' not found in input string.")

    return input_string[pos::-1] + input_string[pos+1:]
Enter fullscreen mode Exit fullscreen mode

Perl provides the reverse function, which does what you would expect to. Along with substr function this can be used to reverse part of the string.

sub main (@array) {
    my ( $input_string, $char ) = @array;

    my $pos = index( $input_string, $char );
    if ( $pos == -1 ) {
        die "Character '$char' not found in input string.\n";
    }

    say reverse( substr( $input_string, 0, $pos + 1 ) )
      . substr( $input_string, $pos + 1 );
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py programming g
gorpramming

$ ./ch-2.py hello h
hello

$ ./ch-2.py abcdefghij h
hgfedcbaij

$ ./ch-2.py reverse s
srevere

$ ./ch-2.py perl r
repl
Enter fullscreen mode Exit fullscreen mode

Top comments (0)