RabbitFarm
2025-04-27
Group Position Reversals
The examples used here are from the weekly challenge problem statement and demonstrate the working solution.
Part 1: Group Position
You are given a string of lowercase letters. Write a script to find the position of all groups in the given string. Three or more consecutive letters form a group. Return “” if none found.
Here’s our one subroutine, this problem requires very little code.
-
sub groupings{
my($s) =
@_;
my
@groups;
my
@group;
my($current, $previous);
my
@letters = split //, $s;
$previous = shift
@letters;
group = ($previous);
do {
$current = $_;
if($previous eq $current){
push
@group, $current;
}
if($previous ne $current){
if(
@group >= 3){
push
@groups, [
@group];
}
group = ($current);
}
$previous = $current;
} for
@letters;
if(
@group >= 3){
push
@groups, [
@group];
}
my
@r = map {q/"/␣.␣join(q//,␣
@{$_}) . q/"/␣}␣
@groups;
return join(q/, /,
@r) || q/""/;
}
◇
-
Fragment referenced in 2.
Putting it all together...
The rest of the code just runs some basic tests.
-
MAIN:{
say groupings q/abccccd/;
say groupings q/aaabcddddeefff/;
say groupings q/abcdd/;
}
◇
-
Fragment referenced in 2.
Sample Run
$ perl perl/ch-1.pl "cccc" "aaa", "dddd", "fff" ""
Part 2: Reverse Equals
You are given two arrays of integers, each containing the same elements as the other. Write a script to return true if one array can be made to equal the other by reversing exactly one contiguous subarray.
Here’s the process we’re going to follow.
- scan both arrays and check where and how often they differ
- if they differ in zero places return true!
- if they differ in one or more places check to see if the reversal makes the two arrays equal
Now let’s check and see how many differences were found.
-
return 1 if
@{$indices_different} == 0;
$indices_different = [sort {$a <=> $b}
@{$indices_different}];
my $last_i = $indices_different->[
@{$indices_different} - 1];
my $length = 1 + $last_i - $indices_different->[0];
my
@u_ = reverse
@{$u}[$indices_different->[0] .. $last_i];
my
@v_ = reverse
@{$v}[$indices_different->[0] .. $last_i];
splice
@{$u}, $indices_different->[0], $length,
@u_;
splice
@{$v}, $indices_different->[0], $length,
@v_;
return 1 if join(q/,/,
@{$u}) eq join(q/,/,
@{$t});
return 1 if join(q/,/,
@{$v}) eq join(q/,/,
@{$s});
return 0;
◇
The rest of the code combines the previous steps and drives some tests.
-
MAIN:{
say reverse_equals [3, 2, 1, 4], [1, 2, 3, 4];
say reverse_equals [1, 3, 4], [4, 1, 3];
say reverse_equals [2], [2];
}
◇
-
Fragment referenced in 7.
Sample Run
$ perl perl/ch-2.pl 1 0 1
References
posted at: 19:21 by: Adam Russell | path: /perl | permanent link to this entry