The Weekly Challenge: 372

7 min read
← Back to Challenges

TABLE OF CONTENTS


  1. HEADLINES

  2. SPONSOR

  3. RECAP

  4. PERL REVIEW

  5. RAKU REVIEW

  6. CHART

  7. NEW MEMBERS

  8. GUESTS

  9. TASK #1: Rearrange Spaces

10. TASK #2: Largest Substring

HEADLINES


Welcome to the Week #372 of The Weekly Challenge.

Today is the first Monday of the month and time to declare the Champion of the Month. With great pride, I announce Matt Martini as the next champion. As of today, Matt, has contributed 39 solutions in Perl.

Below is my contributions to the Task #1 of Week #371.

Perl: source code


sub missing_character {
    my @seq  = @_;
    my @nums = map { $_ eq '?' ? undef : ord($_) - ord('a') + 1 } @seq;

    my ($x, $y);
    for (0..3) {
        if (defined $nums[$_] && defined $nums[$_ + 1]) {
            my $diff = $nums[$_ + 1] - $nums[$_];
            ($_ % 2 == 0)?($x = $diff):($y = $diff);
        }
    }

    if (!defined $x || !defined $y) {
        # Check if we can find the "other" one via a 2-step jump
        for my $i (0..2) {
            if (defined $nums[$i] && defined $nums[$i + 2]) {
                my $total = $nums[$i + 2] - $nums[$i];
                if (defined $x) {
                    $y = $total - $x;
                }
                elsif (defined $y) {
                    $x = $total - $y;
                }
                else {
                    $x = $y = $total / 2;
                }
            }
        }

        # If still missing one, the pattern is likely constant
        $x //= $y;
        $y //= $x;
    }

    # Find the '?' index and fill it
    my ($idx) = grep { !defined $nums[$_] } 0..4;
    if ($idx == 0) {
        $nums[0] = $nums[1] - $x;
    } else {
        my $step = ($idx - 1) % 2 == 0 ? $x : $y;
        $nums[$idx] = $nums[$idx - 1] + $step;
    }

    return chr($nums[$idx] + ord('a') - 1);
}

Raku: source code


sub missing-character(*@seq) {
    my @nums = @seq.map: { $_ eq '?' ?? Nil !! $_.ord - 'a'.ord + 1 };

    my ($x, $y);

    # 2. Identify steps
    for 0..3 -> $i {
        if @nums[$i].defined && @nums[$i + 1].defined {
            my $diff = @nums[$i + 1] - @nums[$i];
            ($i % 2 == 0) ?? ($x = $diff) !! ($y = $diff);
        }
    }

    # 3. Handle missing x or y logic
    if !$x.defined || !$y.defined {
        for 0..2 -> $i {
            if @nums[$i].defined && @nums[$i + 2].defined {
                my $total = @nums[$i + 2] - @nums[$i];
                if $x.defined {
                    $y = $total - $x;
                }
                elsif $y.defined {
                    $x = $total - $y;
                }
                else {
                    $x = $y = $total / 2;
                }
            }
        }
        $x //= $y;
        $y //= $x;
    }

    # 4. Find the '?' index and fill it
    my $idx = @nums.first(!*.defined, :k);

    if $idx == 0 {
        @nums[0] = @nums[1] - $x;
    } else {
        my $step = ($idx - 1) % 2 == 0 ?? $x !! $y;
        @nums[$idx] = @nums[$idx - 1] + $step;
    }

    return (@nums[$idx] + 'a'.ord - 1).chr;
}

Python: source code


def missing_character(seq):
    nums = [ord(char) - ord('a') + 1 if char != '?' else None for char in seq]

    x, y = None, None

    # 2. Identify the steps (x and y)
    for i in range(4):
        if nums[i] is not None and nums[i+1] is not None:
            diff = nums[i+1] - nums[i]
            if i % 2 == 0:
                x = diff
            else:
                y = diff

    # 3. Handle missing x or y using a 2-step jump logic
    if x is None or y is None:
        for i in range(3):
            if nums[i] is not None and nums[i+2] is not None:
                total = nums[i+2] - nums[i]
                if x is not None:
                    y = total - x
                elif y is not None:
                    x = total - y
                else:
                    x = y = total // 2

        # If still missing one, the pattern is likely constant
        if x is None: x = y
        if y is None: y = x

    # 4. Find the '?' index and fill it
    idx = nums.index(None)

    if idx == 0:
        nums[0] = nums[1] - x
    else:
        # Determine if we need the x step or y step based on the gap index
        step = x if (idx - 1) % 2 == 0 else y
        nums[idx] = nums[idx - 1] + step

    return chr(int(nums[idx]) + ord('a') - 1)

Thank you Team PWC, once again.

Happy Hacking!!



Last 5 weeks mainstream contribution stats. Thank you Team PWC for your support and encouragements.

  Week      Perl       Raku       Blog   
   367       53       22       20   
   368       47       21       17   
   369       51       23       27   
   370       50       18       18   
   371       45       21       18   


Last 5 weeks guest contribution stats. Thank you each and every guest contributors for your time and efforts.

  Week      Guests       Contributions       Languages   
   367       18       76       26   
   368       14       80       25   
   369       16       86       26   
   370       12       50       19   
   371       12       68       23   

TOP 10 Guest Languages


Do you see your favourite language in the Top #10? If not then why not contribute regularly and make it to the top.

 1. Python     (4360)
 2. Rust       (1147)
 3. C          (1020)
 3. Ruby       (899)
 5. Haskell    (893)
 6. Lua        (886)
 7. C++        (714)
 8. Go         (682)
 9. JavaScript (637)
10. Java       (532)

Blogs with Creative Title


1. Missing Equilibrium by Arne Sommer.

2. Subset Equilibrium (Just nod if you can hear me) by Bob Lied.

3. Missing Equilibria by Jorg Sommrey.

4. The Missing Equilibrium by Matthias Muth.

5. My subset is-a missing a letter… by Packy Anderson.

6. Solve the question and balance the subset by Peter Campbell Smith.

7. Missing Equilibrium by Roger Bell_West.

8. Question the bits by Simon Green.


GitHub Repository Stats


1. Commits: 49,407 (+123)

2. Pull Requests: 14,037(+33)

3. Contributors: 278

4. Fork: 349 (+1)

5. Stars: 212



With start of Week #355, we have a new sponsor Marc Perry until the end of year 2026. Having said we are looking for more sponsors so that we can go back to weekly winner. If anyone interested please get in touch with us at perlweeklychallenge@yahoo.com. Thanks for your support in advance. You can find more informations here.

RECAP


Quick recap of The Weekly Challenge - 371 by Mohammad Sajid Anwar.

PERL REVIEW


If you missed any past reviews then please check out the collection.

RAKU REVIEW


If you missed any past reviews then please check out the collection.

CHART


Please take a look at the charts showing interesting data.

I would like to THANK every member of the team for their valuable suggestions. Please do share your experience with us.

NEW MEMBERS


Please find out How to contribute?, if you have any doubts.

Please try the excellent tool EZPWC created by respected member Saif Ahmed of Team PWC.

GUESTS


Please check out the guest contributions for the Week #371.

Please find past solutions by respected guests. Please share your creative solutions in other languages.

Task 1: Rearrange Spaces

Submitted by: Mohammad Sajid Anwar

You are given a string text of words that are placed among number of spaces.

Write a script to rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximised. If you can’t distribute, place the extra spaces at the end. Finally return the string.

Example 1

Input: $str = "  challenge  "
Output: "challenge    "

We have 4 spaces and 1 word. So all spaces go to the end.

Example 2

Input: $str = "coding  is  fun"
Output: "coding  is  fun"

We have 4 spaces and 3 words (2 gaps). So 2 spaces per gap.

Example 3

Input: $str = "a b c  d"
Output: "a b c d "

We have 4 spaces and 4 words (3 gaps). So 1 space per gap and 1 remainder.

Example 4

Input: $str = "  team      pwc  "
Output: "team          pwc"

We have 10 spaces and 2 words (1 gap). So 10 spaces per gap.

Example 5

Input: $str = "   the  weekly  challenge  "
Output: "the    weekly    challenge "

We have 9 spaces and 3 words (2 gaps). So 4 spaces per gap and 1 remainder.

Task 2: Largest Substring

Submitted by: Mohammad Sajid Anwar

You are given a string.

Write a script to return the length of the largest substring between two equal characters excluding the two characters. Return -1 if there is no such substring.

Example 1

Input: $str = "aaaaa"
Output: 3

For character "a", we have substring "aaa".

Example 2

Input: $str = "abcdeba"
Output: 5

For character "a", we have substring "bcdeb".

Example 3

Input: $str = "abbc
Output: 0

For character "b", we have substring "".

Example 4

Input: $str = "abcaacbc"
Output: 4

For character "a", we have substring "bca".
For character "b", we have substring "caac".
For character "c", we have substring "aacb".

Example 5

Input: $str = "laptop"
Output: 2

For character "p", we have substring "to".

By submitting a response to the challenge you agree that your name or pseudonym, any photograph you supply and any other personal information contained in your submission may be published on this website and the associated mobile app. Last date to submit the solution 23:59 (UK Time) Sunday 10th May 2026.