DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge: Vowels and numbers

Weekly Challenge 374

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: Count Vowel

Task

You are given a string.

Write a script to return all possible vowel substrings in the given string. A vowel substring is a substring that only consists of vowels and has all five vowels present in it.

My solution

It seems many Team PWC users sent Mohammad "so many emails" when the examples did not match the expected output. As I follow TDD when completing the challenges, I also picked this up.

For this task, I have two loops to generate all possible substrings. The variable start goes from 0 to 5 less than the length of the string. This is because a valid answer needs to have at least five letters. The variable end goes from start + 5 to the length of the string.

I then check if the substr contains each vowel and does not contain any non-vowels. If it does, I add it to the solution list.

def count_vowel(input_string: str) -> list[str]:
    l = len(input_string)
    vowels = ["a", "e", "i", "o", "u"]
    solution = []

    for start in range(l - 4):
        for end in range(start+5, l+1):
            substr = input_string[start:end]
            if (
                all(v in substr for v in vowels) and
                not any(c not in vowels for c in substr)
            ):
                solution.append(substr)

    return solution
Enter fullscreen mode Exit fullscreen mode

The Perl logic uses a variable length instead of end to reflect how the substr function works.

use List::Util 'any';

sub main ($input_string) {
    my $l        = length($input_string);
    my @vowels   = (qw/a e i o u/);
    my @solution = ();

    foreach my $start ( 0 .. $l - 4 ) {
        foreach my $length ( 5 .. $l - $start ) {
            my $substr = substr( $input_string, $start, $length );

            if ( $substr !~ /^[aeiou]+$/ ) {
                next;
            }

            if ( any { index( $substr, $_ ) == -1 } @vowels ) {
                next;
            }

            push @solution, $substr;
        }
    }

    say "(" . join( ", ", map { qq{"$_"} } @solution ) . ")";
}
Enter fullscreen mode Exit fullscreen mode

Examples

The order in the output differs from the examples.

$ ./ch-1.py aeiou
("aeiou")

$ ./ch-1.py aaeeeiioouu
("aaeeeiioou", "aaeeeiioouu", "aeeeiioou", "aeeeiioouu")

$ ./ch-1.py aeiouuaxaeiou
("aeiou", "aeiouu", "aeiouua", "eiouua", "aeiou")

$ ./ch-1.py uaeiou
("uaeio", "uaeiou", "aeiou")

$ ./ch-1.py aeioaeioa
()
Enter fullscreen mode Exit fullscreen mode

Task 2: Largest Same-digits Number

Task

You are given a string containing 0-9 digits only.

Write a script to return the largest number with all digits the same in the given string.

My solution

I always look at the examples to fill in answers that aren't mentioned in the task description. One thing I wasn't sure if the numbers had to be consecutive or not. In this instance, the examples don't help answer that question. I made the decision that they didn't need to be.

My first thought was to sort by the frequency of the numbers and the number if it had the same frequency. But this would work with numbers like 100 (where 0 appears most often).

The solution I came up with was to count the frequency of each number using the Counter function. I then loop through the dict (hash in Perl) to find the highest number.

from collections import Counter
import re

def largest_number(input_string) -> int:
    if not re.search(r'^\d+$', input_string):
        raise ValueError("String should only contain numbers")

    largest_number = 0
    freq = Counter(input_string)

    for digit, count in freq.items():
        number = int(digit * count)
        if number > largest_number:
            largest_number = number

    return largest_number
Enter fullscreen mode Exit fullscreen mode

The Perl solution follows the same logic.

Examples

$ ./ch-2.py 6777133339
3333

$ ./ch-2.py 1200034
4

$ ./ch-2.py 44221155
55

$ ./ch-2.py 88888
88888

$ ./ch-2.py 11122233
222
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
simongreennet profile image
Simon Green

Looking at some other pull requests, it seems the consensus is that the numbers needed to be consecutive for the second task. I'll still keep my solution, but make a note that I made the decision that they didn't need to be.