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