perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 371 - Task 1: Missing Letter

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-371/#TASK1
  3 #
  4 # Task 1: Missing Letter
  5 # ======================
  6 #
  7 # You are given a sequence of 5 lowercase letters, with one letter replaced by
  8 # ‘?’. Each letter maps to its position in the alphabet (‘a = 1’, ‘b = 2’, …,
  9 # ‘z = 26’). The sequence follows a repeating pattern of step sizes between
 10 # consecutive letters. The pattern is either a constant step (e.g., ‘+2, +2,
 11 # +2, +2’) or a simple alternating pattern of two distinct steps (e.g., ‘+2,
 12 # +3, +2, +3’).
 13 #
 14 ## Example 1
 15 ##
 16 ## Input: @seq = qw(a c ? g i)
 17 ## Output: e
 18 ##
 19 ## The pattern of the sequence is +2,+2,+2,+2.
 20 ## 1: a
 21 ## 3: c
 22 ## 5: e
 23 ## 7: g
 24 ## 9: i
 25 #
 26 ## Example 2
 27 ##
 28 ## Input: @seq = qw(a d ? j m)
 29 ## Output: g
 30 ##
 31 ## The pattern of the sequence is +3,+3,+3,+3.
 32 ## 1: a
 33 ## 4: d
 34 ## 7: g
 35 ## 10: j
 36 ## 13: m
 37 #
 38 ## Example 3
 39 ##
 40 ## Input: @seq = qw(a e ? m q)
 41 ## Output: i
 42 ##
 43 ## The pattern of the sequence is +4,+4,+4,+4.
 44 ## 1: a
 45 ## 5: e
 46 ## 9: i
 47 ## 13: m
 48 ## 17: q
 49 #
 50 ## Example 4
 51 ##
 52 ## Input: @seq = qw(a c f ? k)
 53 ## Output: h
 54 ##
 55 ## The pattern of the sequence is +2,+3,+2,+3.
 56 ## 1: a
 57 ## 3: c
 58 ## 6: f
 59 ## 8: h
 60 ## 11: k
 61 #
 62 ## Example 5
 63 ##
 64 ## Input: @seq = qw(b e g ? l)
 65 ## Output: j
 66 ##
 67 ## The pattern of the sequence is +3,+2,+3,+2.
 68 ## 2: b
 69 ## 5: e
 70 ## 7: g
 71 ## 10: j
 72 ## 12: l
 73 #
 74 ############################################################
 75 ##
 76 ## discussion
 77 ##
 78 ############################################################
 79 #
 80 # For each element in the list we check what the difference is to the
 81 # next element - until we have the value of both potentially different
 82 # step sizes (if there is only one step size, this will lead to both
 83 # step sizes to be the same). We then only need keep track of when the '?'
 84 # appeared. In the end, if it appeared in the first place in $seq, we
 85 # calculate it by walking backwards by $s1 from the second element in $seq.
 86 # Otherwise, we walk forward by $s1 or $s2 from the previous element,
 87 # dependent on whether the index of the unknown element is odd or even.
 88 #
 89 use v5.36;
 90 
 91 missing_letter( qw(a c ? g i) );
 92 missing_letter( qw(a d ? j m) );
 93 missing_letter( qw(a e ? m q) );
 94 missing_letter( qw(a c f ? k) );
 95 missing_letter( qw(b e g ? l) );
 96 
 97 sub missing_letter(@seq) {
 98     say "Input: (" . join(", ", @seq) . ")";
 99     my ($s1, $s2, $which_unknown);
100     foreach my $i (0..$#seq-1) {
101         if($seq[$i] eq '?') {
102             $which_unknown = $i;
103             next;
104         }
105         if($i % 2) {
106             next if $s2;
107             next if $seq[$i+1] eq '?';
108             $s2 = ord($seq[$i+1]) - ord($seq[$i]);
109         } else {
110             next if $s1;
111             next if $seq[$i+1] eq '?';
112             $s1 = ord($seq[$i+1]) - ord($seq[$i]);
113         }
114     }
115     if($which_unknown == 0) {
116         return say "Output: " . chr(ord($seq[1]) - $s1);
117     }
118     if($which_unknown % 2) {
119         say "Output: " . chr(ord($seq[$which_unknown-1]) + $s1);
120     } else {
121         say "Output: " . chr(ord($seq[$which_unknown-1]) + $s2);
122     }
123 }