The Weekly Challenge 364

Code Cracking Chaos & Goal Parsing Pandemonium!

Original Challenge Link

Task 1: Decrypt String

"Cracking the Code: From Digits to Letters!"

In Challenge 364, we explore a character mapping challenge. The input consists of digits and # characters, representing a specific encoding of lowercase English letters.

The Strategy: To avoid incorrect mapping, we must process the string from left to right or prioritize the longer ## patterns. This prevents a sequence like '10#' from being interpreted as '1' and '0'.

Using a regex-based approach allows us to cleanly handle both the two-digit-plus-hash cases and the single-digit cases in a single pass.

Perl Implementation
sub decrypt_string {
    my ($str) = @_;
    $str =~ s/(\d{2})#/chr(96 + $1)/eg;
    $str =~ s/(\d)/chr(96 + $1)/eg;
    return $str;
}
Python Implementation
def decrypt_string(s: str) -> str:
    s = re.sub(r"(\d{2})#", lambda m: chr(96 + int(m.group(1))), s)
    s = re.sub(r"(\d)", lambda m: chr(96 + int(m.group(1))), s)
    return s

Task 2: Goal Parser

"The Goal Parser: G for Goal, () for O, (al) for AL!"

The second task involves parsing a specialized string format with fixed rules: G stays G, () becomes o, and (al) becomes al.

This is a classic string transformation problem where global search-and-replace provides an efficient and readable solution.

Perl Implementation
sub goal_parser {
    my ($str) = @_;
    $str =~ s/\(\)/o/g;
    $str =~ s/\(al\)/al/g;
    return $str;
}
Python Implementation
def goal_parser(s: str) -> str:
    return s.replace("()", "o").replace("(al)", "al")