The Weekly Challenge 371

Alphabetical Patterns & Subset Sums!

Original Challenge Link

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 '?'

Task 2: Subset Equilibrium

"Balanced Bounds: Finding Subsets in Equilibrium!"

Find all subsets of an array where the sum of the elements equals the sum of their 1-based indices.

The Strategy: Use a bitmask to generate all ^n - 1$ non-empty subsets of the input array. For each subset, calculate the sum of its values and the sum of the 1-based indices of its members. If the sums are equal, add the subset to the result list.
Perl Implementation
sub find_equilibrium_subsets {
    my (@ints) = @_;
    my $n = scalar @ints;
    my @results;

    for my $mask (1 .. (1 << $n) - 1) {
        my ($sum_vals, $sum_inds, @subset) = (0, 0);
        for my $i (0 .. $n - 1) {
            if ($mask & (1 << $i)) {
                $sum_vals += $ints[$i];
                $sum_inds += ($i + 1);
                push @subset, $ints[$i];
            }
        }
        push @results, [ @subset ] if $sum_vals == $sum_inds;
    }
    return \@results;
}
Python Implementation
def find_equilibrium_subsets(ints: list[int]) -> list[list[int]]:
    n = len(ints)
    results = []
    for mask in range(1, 1 << n):
        sum_vals = sum_inds = 0
        subset = []
        for i in range(n):
            if mask & (1 << i):
                sum_vals += ints[i]
                sum_inds += (i + 1)
                subset.append(ints[i])
        if sum_vals == sum_inds:
            results.append(subset)
    return results