The Weekly Challenge 001

It All Started Here: Replace and FizzBuzz!

Original Challenge Link | My Solutions

Task 1: Replace e with E

"The Great Letter Swap: When e Goes Uppercase!"

Write a script to replace the character 'e' with 'E' in the string 'Perl Weekly Challenge'. Also print the number of times the character 'e' is found in the string.

The Strategy: Simply count the occurrences of 'e' using regex, then perform a global substitution replacing 'e' with 'E'. Return both the count and modified string.
Perl Implementation
sub replace_e {
    my $what = shift;
    my $count = () = $what =~ /e/g;
    $what =~ s/e/E/g;
    return [$count, $what];
}
Python Implementation
def replace_e(what: str) -> tuple[int, str]:
    count = what.count("e")
    modified_what = what.replace("e", "E")
    return count, modified_what

Task 2: FizzBuzz one-liner

"The Classic Game: When Numbers Get Fizzed and Buzzed!"

Write a one-liner to solve the FizzBuzz problem and print the numbers 1 through 20. However, any number divisible by 3 should be replaced by the word 'fizz' and any divisible by 5 by the word 'buzz'. Those numbers that are both divisible by 3 and 5 become 'fizzbuzz'.

The Strategy: Iterate from 1 to 20, check divisibility by 3 and 5, build the output string accordingly.
Perl Implementation
sub fizz_buzz {
    my @output;
    for (1..20) {
        my $line = '';
        $line = 'fizz' if $_ % 3 == 0;
        $line .= 'buzz' if $_ % 5 == 0;
        $line = $_ unless $line;
        push @output, $line;
    }
    return \@output;
}
Python Implementation
def fizz_buzz():
    result = []
    for i in range(1, 21):
        if i % 15 == 0:
            result.append("fizzbuzz")
        elif i % 3 == 0:
            result.append("fizz")
        elif i % 5 == 0:
            result.append("buzz")
        else:
            result.append(str(i))
    return result