The Weekly Challenge 366

Prefix Patrol & Time Validation!

Original Challenge Link

Task 1: Count Prefixes

"How Many Start With This?"

This week's first task involves counting how many strings in a list start with a given prefix. Given a prefix and a list of strings, we need to return the count of strings that begin with that prefix.

The Strategy: We iterate through each string in the list and check if it starts with the given prefix. We use string matching to count occurrences. The solution handles edge cases like empty strings and case sensitivity.
Perl Implementation
sub count_prefixes {
    my ($prefix, @strings) = @_;
    
    die "Error: prefix must be defined\n" unless defined $prefix;
    die "Error: strings must be defined\n" unless @strings;
    
    my $count = 0;
    for my $str (@strings) {
        $count++ if $str =~ /^\Q$prefix\E/;
    }
    
    return $count;
}
Python Implementation
def count_prefixes(prefix: str, strings: list) -> int:
    """
    Count how many strings start with the given prefix.
    
    Args:
        prefix: The prefix to search for.
        strings: List of strings to check.
        
    Returns:
        Number of strings that start with the prefix.
        
    Raises:
        ValueError: If prefix is None or strings is empty.
    """
    if prefix is None:
        raise ValueError("prefix must not be None")
    if not strings:
        raise ValueError("strings must not be empty")
    
    count = 0
    for s in strings:
        if s.startswith(prefix):
            count += 1
    
    return count

Task 2: Valid Valid Times

"Time Police: Are These Times Even Real?"

The second task involves validating time strings in HH:MM format. Given a list of time strings, we need to count how many are valid. A time is valid if the hour is between 00-23 and the minute is between 00-59.

The Strategy: We parse each time string, extract hours and minutes, and validate the ranges. The solution handles edge cases like invalid formats and out-of-range values. We use regular expressions for format validation and numeric comparisons for range validation.
Perl Implementation
sub valid_valid_times {
    my (@times) = @_;
    
    die "Error: times must be defined\n" unless @times;
    
    my $count = 0;
    for my $time (@times) {
        $count++ if _is_valid_time($time);
    }
    
    return $count;
}

sub _is_valid_time {
    my ($time) = @_;
    
    return 0 unless defined $time;
    
    # Check format: must be exactly HH:MM
    unless ($time =~ /^(\d{2}):(\d{2})$/) {
        return 0;
    }
    
    my ($hour, $minute) = ($1, $2);
    
    # Validate ranges
    return 0 if $hour > 23;
    return 0 if $minute > 59;
    
    return 1;
}
Python Implementation
def valid_valid_times(times: list) -> int:
    """
    Count how many time strings are valid.
    
    Args:
        times: List of time strings in HH:MM format.
        
    Returns:
        Number of valid time strings.
        
    Raises:
        ValueError: If times is None or empty.
    """
    if times is None:
        raise ValueError("times must not be None")
    if not times:
        raise ValueError("times must not be empty")
    
    count = 0
    for time_str in times:
        if _is_valid_time(time_str):
            count += 1
    
    return count

def _is_valid_time(time_str: str) -> bool:
    """
    Check if a time string is valid.
    
    A time is valid if:
        - Format is exactly HH:MM
        - Hour is between 00-23
        - Minute is between 00-59
    """
    if not time_str:
        return False
    
    # Check format with regex
    match = re.match(r'^(\d{2}):(\d{2})$', time_str)
    if not match:
        return False
    
    hour, minute = int(match.group(1)), int(match.group(2))
    
    # Validate ranges
    if hour > 23 or minute > 59:
        return False
    
    return True