Task 1: Missing Letter
"Alphabetical Intrigue: Solving the Mystery Letter!"
Given a sequence of letters with a missing entry (?), identify the character that maintains either a constant or alternating step pattern.
The Strategy: Convert letters to their alphabetical positions (a=1, b=2, etc.). Iterate through all possible characters ('a'..'z') and substitute the question mark with each. Calculate the differences between consecutive elements. If the differences are all identical (constant) or follow a recurring pair pattern (alternating), the candidate character is the answer.
Perl Implementation
sub find_missing_letter {
my (@letters) = @_;
my @indices = map { $_ eq '?' ? undef : ord($_) - ord('a') + 1 } @letters;
for my $cand_char ('a' .. 'z') {
my $cand_idx = ord($cand_char) - ord('a') + 1;
my @current = map { defined $_ ? $_ : $cand_idx } @indices;
my @diffs = map { $current[$_+1] - $current[$_] } 0 .. 3;
return $cand_char if ($diffs[0] == $diffs[1] && $diffs[1] == $diffs[2] && $diffs[2] == $diffs[3])
|| ($diffs[0] == $diffs[2] && $diffs[1] == $diffs[3]);
}
return '?';
}
Python Implementation
def find_missing_letter(letters: list[str]) -> str:
for cand_char in (chr(c) for c in range(ord('a'), ord('z') + 1)):
current_letters = [cand_char if ch == '?' else ch for ch in letters]
indices = [ord(ch) - ord('a') + 1 for ch in current_letters]
diffs = [indices[i+1] - indices[i] for i in range(4)]
if all(d == diffs[0] for d in diffs) or (diffs[0] == diffs[2] and diffs[1] == diffs[3]):
return cand_char
return '?'