DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Group Tag

Weekly Challenge 369

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. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.

Challenge, My solutions

Task 1: Valid Tag

Submitted by: Mohammad Sajid Anwar

You are given a given a string caption for a video.

Write a script to generate tag for the given string caption in three steps as mentioned below:

  1. Format as camelCase Starting with a lower-case letter and capitalising the first letter of each subsequent word: Merge all words in the caption into a single string starting with a #.
  2. Sanitise the String: Strip out all characters that are not English letters (a-z or A-Z).
  3. Enforce Length: If the resulting string exceeds 100 characters, truncate it so it is exactly 100 characters long.

My solution

The last example - where Hour starts with a upper case letter - would suggest that the second step needs to be done before the first one. As I used TDD when writing these solutions this was picked up when running the tests.

Nothing overly tricky about the solution. I take input_string and perform the necessary operations on it.

def valid_tag(input_string: str) -> str:
    input_string = re.sub(r"[^\sa-zA-Z]", "", input_string)
    input_string = input_string.strip().lower()
    input_string = re.sub(r" +([a-z])", upper_case_letter, input_string)
    return "#" + input_string[:99]
Enter fullscreen mode Exit fullscreen mode

The Perl solution is similar.

sub main ($input_string) {
    $input_string =~ s/[^\sa-zA-Z]//g;
    $input_string =~ s/^\s+//;
    $input_string =~ s/\s+$//;
    $input_string = lc $input_string;
    $input_string =~ s/ +([a-z])/uc $1/eg;
    say "#" . substr($input_string, 0, 99);
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-1.py "Cooking with 5 ingredients!"
#cookingWithIngredients

$ ./ch-1.py the-last-of-the-mohicans
#thelastofthemohicans

$ ./ch-1.py "  extra spaces here"
#extraSpacesHere

$ ./ch-1.py "iPhone 15 Pro Max Review"
#iphoneProMaxReview

$ ./ch-1.py "Ultimate 24-Hour Challenge: Living in a Smart Home controlled entirely by Artificial Intelligence and Voice Commands in the year 2026!"
#ultimateHourChallengeLivingInASmartHomeControlledEntirelyByArtificialIntelligenceAndVoiceCommandsIn
Enter fullscreen mode Exit fullscreen mode

Task 2: Group Division

Task

You are given a string, group size and filler character.

Write a script to divide the string into groups of given size. In the last group if the string doesnโ€™t have enough characters remaining fill with the given filler character.

My solution

This is one task where the Python and Perl solutions are completely different. Python's more_itertools module has the grouper function, so I use this to separate the string into parts with the filler if required. No need to reinvent a perfectly round wheel :)

def group_division(input_string: str, size: int, filler: str) -> list[str]:
    result = []
    for g in grouper(input_string, size, fillvalue=filler):
        result.append("".join(g))
    return result
Enter fullscreen mode Exit fullscreen mode

Perl does not have a similar function, that I'm aware of. I use a traditional for loop to split the string into the different parts. After that I have a command that will add the filler to the last item in the @result array if required. The x operator is the replication operator in Perl.

sub main ( $input_string, $size, $filler ) {
    my @result = ();
    for ( my $i = 0 ; $i < length($input_string) ; $i += $size ) {
        push @result, substr( $input_string, $i, $size );
    }

    $result[-1] .= $filler x ( $size - length( $result[-1] ) );

    say "(" . join( ", ", @result ) . ")";
}
Enter fullscreen mode Exit fullscreen mode

Examples

$ ./ch-2.py RakuPerl 4 "#"
['Raku', 'Perl']

$ ./ch-2.py Python 5 0
['Pytho', 'n0000']

$ ./ch-2.py 12345 3 x
['123', '45x']

$ ./ch-2.py HelloWorld 3 _
['Hel', 'loW', 'orl', 'd__']

$ ./ch-2.py AI 5 "!"
['AI!!!']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)