Perl Weekly Challenge 372.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 372.
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.

I can find the words by splitting on space. The number of gaps is one less than the number of words. The interword space is the number of spaces divided by the number of gaps. At the end I add the residual spaces, obtained using modular arithmetic. The code fits a two liner.
Examples:
perl -E '
for(@ARGV){$i=$_;@w=split" ";$l=0;$l+=length$1 while s/(\s+)//;($s,$r)=@w>1?
($l/(@w-1),$l%(@w-1)):(0,$l);$j=" "x$s;say "\"$i\" -> \"", join($j,@w)," "x$r,"\"";}
' " challenge " "coding is fun" "a b c d" \
" team pwc " " the weekly challenge "
Results:
" challenge " -> "challenge "
"coding is fun" -> "coding is fun"
"a b c d" -> "a b c d "
" team pwc " -> "team pwc"
" the weekly challenge " -> "the weekly challenge "
The full code is:
1 # Perl weekly challenge 372
2 # Task 1: Rearrange Spaces
3 #
4 # See https://wlmb.github.io/2026/05/04/PWC372/#task-1-rearrange-spaces
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to rearrange the spaces in string Sn leaving equal gaps
9 between words.
10 FIN
11 for(@ARGV){
12 my $input = $_;
13 my @words = split" ";
14 my $length = 0;
15 $length += length $1 while s/(\s+)//;
16 my ($spaces, $rest) = @words > 1
17 ? ($length / (@words-1), $length % (@words-1))
18 : (0,$length);
19 my $sep = " " x $spaces;
20 say "\"$input\" -> \"", join($sep, @words), " "x$rest, "\"";
21 }
Example:
./ch-1.pl " challenge " "coding is fun" "a b c d" \
" team pwc " " the weekly challenge "
Results:
" challenge " -> "challenge "
"coding is fun" -> "coding is fun"
"a b c d" -> "a b c d "
" team pwc " -> "team pwc"
" the weekly challenge " -> "the weekly challenge "
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".
For each character $_ in the input, I try to match against
/$_(.*)$_/. If succesful, I add the length of the matched
substring to a list, and if unsuccesful I add -1. The result
is the max (from List::Util) of the list. The result
fits a one-liner.
Examples:
perl -MList::Util=max -E '
for(@ARGV){$i=$_;say "$_ -> ", max map {$i=~m/$_(.*)$_/?length($1):-1} split "";}
' aaaaa abcdeba abbc abcaacbc laptop
Results:
aaaaa -> 3
abcdeba -> 5
abbc -> 0
abcaacbc -> 4
laptop -> 2
Output: 3 Output: 5 Output: 0 Output: 4 Output: 2
The full code is similar. I use uniq from List::Util to
avoid redundant work.
1 # Perl weekly challenge 372
2 # Task 2: Largest Substring
3 #
4 # See https://wlmb.github.io/2026/05/04/PWC372/#task-2-largest-substring
5 use v5.36;
6 use List::Util qw(max uniq);
7 die <<~"FIN" unless @ARGV;
8 Usage: $0 S0 S1...
9 to find the length of the largest substring of Sn
10 between equal characters
11 FIN
12 for(@ARGV){
13 my $input = $_;
14 say "$_ -> ",
15 max
16 map {$input =~ m/$_(.*)$_/?length($1):-1;}
17 uniq
18 split "";
19 }
Examples:
./ch-2.pl aaaaa abcdeba abbc abcaacbc laptop
Results:
aaaaa -> 3
abcdeba -> 5
abbc -> 0
abcaacbc -> 4
laptop -> 2
/;