Task1
We rearrange the spaces per the given instructions:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub rearrange_spaces{
my $c = $_[0] =~ tr/ //;
my @words = split ' ',$_[0];
my $width = @words > 1 ? int($c / $#words) : 0;
my $rest = @words > 1 ? $c % $#words : $c;
join(' ' x $width,@words) . ' ' x $rest
}
is rearrange_spaces(' challenge '),'challenge ','Example 1';
is rearrange_spaces('coding is fun'),'coding is fun','Example 2';
is rearrange_spaces('a b c d'),'a b c d ','Example 3';
is rearrange_spaces(' team pwc '),
'team pwc','Example 4';
is rearrange_spaces(' the weekly challenge '),
'the weekly challenge ','Example 5';
Task2
We find the largest substring in-between two equal characters:
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 5;
sub largest_string{
my $l = 0;
$l < length $2 and $l = length $2 while $_[0] =~ /(.)(?=(.*)\1)/g;
$l
}
is largest_string('aaaaa'),3,'Example 1';
is largest_string('abcdeba'),5,'Example 2';
is largest_string('abbc'),0,'Example 3';
is largest_string('abcaacbc'),4,'Example 4';
is largest_string('laptop'),2,'Example 5';