Peter’s blog ✴ Week 371 ✴ 27 April 2026

THE WEEKLY CHALLENGE
Solve the question and balance the subset

The Perl Camel

Task 1

Missing letter

You are given a sequence of 5 lowercase letters, with one letter replaced by ‘?’.

Each letter maps to its position in the alphabet (‘a = 1’, ‘b = 2’, …, ‘z = 26’). The sequence follows a repeating pattern of step sizes between consecutive letters. The pattern is either a constant step (eg ‘+2, +2, +2, +2’) or a simple alternating pattern of two distinct steps (eg ‘+2, +3, +2, +3’).

Write a script to determine the letter that the '?' represents.

Examples


Example 1
Input: @seq = qw(a c ? g i)
Output: e
The pattern of the sequence is +2,+2,+2,+2.
1: a
3: c
5: e
7: g
9: i

Example 2
Input: @seq = qw(a d ? j m)
Output: g
The pattern of the sequence is +3,+3,+3,+3.
1: a
4: d
7: g
10: j
13: m

Example 3
Input: @seq = qw(a e ? m q)
Output: i
The pattern of the sequence is +4,+4,+4,+4.
1: a
5: e
9: i
13: m
17: q

Example 4
Input: @seq = qw(a c f ? k)
Output: h
The pattern of the sequence is +2,+3,+2,+3.
1: a
3: c
6: f
8: h
11: k

Example 5
Input: @seq = qw(b e g ? l)
Output: j
The pattern of the sequence is +3,+2,+3,+2.
2: b
5: e
7: g
10: j
12: l

Analysis

There are 5 positions (let's call them q0 to q4) of which one is initially a '?'. In the solved sequence we are told that if the pattern of the sequence is a, b, then:

  • q1 = q0 + a
  • q2 = q1 + b
  • q3 = q2 + a
  • q4 = q3 + b

As we know 4 out of the 5 q values we can determine a and b, because if the '?' is ...

  • q0, then b = q2 - q1 and a = q3 - q2
  • q1, then a = q3 - q2 and b = q4 - q3
  • q2, then a = q1 - q0 and b = q4 - q3
  • q3, then a = q1 - q0 and b = q2 - q1
  • q4, then a = q1 - q0 and a = q3 - q2

So, for example, if q2 is the '?', from the above we can see that
a = q1 - q0 and b = q4 - q3.

So now we know that if:

  • q0 is '?' then its letter is q1 - a
  • q1 is '?' then its letter is q0 + a
  • q2 is '?' then its letter is q1 + b
  • q3 is '?' then its letter is q2 + a
  • q4 is '?' then its letter is q3 + b

Perl Weekly’s review

from Perl Weekly issue 771

By using established neighboring letter pairs, the algorithm determines the steps $a and $b in an efficient manner by deducing which are the next alternately patterned letter combinations in relation to each of the five positions for question marks. The entire algorithm utilises a single conditional expression to account for all five possible question mark positions. Additionally, the use of the modulus operator and the mapping of the patterning logic from the analysis to the assignment of steps makes this implementation both very efficient and very easy to read.

Try it 

Try running the script with any input:



example: r s ? u v

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-04-27
use utf8;     # Week 371 - task 1 - Missing letter
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

missing_letter(qw(? m b n c));
missing_letter(qw(b ? c b d));
missing_letter(qw(a c ? g i));
missing_letter(qw(a c e ? i));
missing_letter(qw(m a n b ?));
missing_letter(qw(z y x w ?));
missing_letter(qw(o o ? o o));

sub missing_letter {
    
    my (@q, $a, $b, $j, $qm, $gap, $char);
    
    # initialise
    @q = @_;
    
    # find the pairs (see blog)
    for $j (0 .. 3) {
        if ($q[$j] =~ m|[a-z]| and $q[$j + 1] =~ m|[a-z]|) {
            $gap = ord($q[$j + 1]) - ord($q[$j]);
            if ($j & 1) { $b = $gap } else { $a = $gap };
            
        # and the '?'
        } else {
            $qm = $j;
        }
    }
    $qm = 4 if $q[4] eq '?';
    
    # deduce the missing char
    $char = $qm == 0 ? chr(ord($q[1]) - $a) : chr(ord($q[$qm - 1]) + ($qm & 1 ? $a : $b));

    say qq[\nInput:  ] . join(' ', @q); 
    say qq[Output: ? = $char; sequence = ] . sprintf('%+d, %+d; ', $a, $b);
}

13 lines of code

Output from script


Input:  ? m b n c
Output: ? = a; sequence = +12, -11; 

Input:  b ? c b d
Output: ? = a; sequence = -1, +2; 

Input:  a c ? g i
Output: ? = e; sequence = +2, +2; 

Input:  a c e ? i
Output: ? = g; sequence = +2, +2; 

Input:  m a n b ?
Output: ? = o; sequence = -12, +13; 

Input:  z y x w ?
Output: ? = v; sequence = -1, -1; 

Input:  o o ? o o
Output: ? = o; sequence = +0, +0; 

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain