I went to the Perl and Raku Conference in Toronto, ON, two weeks ago. I went because I really wanted to reconnect with the Perl community that I’d fallen out of touch with while I was working at a job where Perl was actively ridiculed.
While I was there, I was talking to one of the people giving talks, Bruce Gray. He suggested that one of the best ways to reconnect would be to do the weekly challenge.
I’d seen the challenge being talked about in emails I subscribed to, but I hadn’t given it much thought. But I wanted to reconnect, keep my Perl chops up to date, and generally start participating in the community again. So when I got the email for Challenge #226, I thought about it a bit. What I realized was that the challenge wasn’t just a way for people to showcase their Perl skills; it was a way for the community to showcase how easy Perl and Raku were to use. So I decided that was the approach I was going to take: not try to be clever, but try to show how easy this language I love is to solve problems.
Task 1: Shuffle String
Here’s the description provided in the challenge:
You are given a string and an array of indices of same length as string.
Write a script to return the string after re-arranging the indices in the correct order.
Example 1
Input: $string = 'lacelengh', @indices = (3,2,0,5,4,8,6,7,1)
Output: 'challenge'
Example 2
Input: $string = 'rulepark', @indices = (4,7,3,1,0,5,2,6)
Output: 'perlraku'
I won’t lie, it took me a little while to understand what it wanted me to do. Finally, I realized that the @indicies
array was showing me where in the output string the character from the input string should be moved to: the first character in the input string should be moved to the 3rd position in the output, the second character to the 2nd position, the third to the 0th position and so on. Once I grokked that requirement, the Perl implementation came easily:
#!/usr/bin/env perl
use v5.36;
sub shuffle_string {
my($string, $indices) = @_;
my @chars = split //, $string; # split input string into characters
my @result;
foreach my $index ( @$indices ) {
my $char = shift @chars; # get the next character
$result[$index] = $char; # put the character at that index in the result
}
say "Input: \$string = '$string', \@indices = (" . join(',', @$indices) . ")";
say "Output: '" . join(q{}, @result) . "'";
}
say "Task 1: Shuffle String";
say "\nExample 1";
shuffle_string('lacelengh', [3,2,0,5,4,8,6,7,1]);
say "\nExample 2";
shuffle_string('rulepark', [4,7,3,1,0,5,2,6]);
Note how Perl makes handling the parts of the problem easy: splitting a string into its component characters is easy, recombining them back into a string is easy, passing the data around is easy.
Now, I don’t have a lot of experience with Raku; but I want to get better at it, so that’s why I’m doing the challenges in Raku as well. Unfortunately, for the moment my Raku solutions will look a lot like my Perl solutions:
#!/usr/bin/env raku
sub shuffle_string ($string, @indices) {
my @chars = $string.split("", :skip-empty);
my @result;
for @indices -> $index {
my $char = shift @chars; # get the next character
@result[$index] = $char; # put the character at that index in the result
}
say "Input: \$string = '$string', \@indices = (" ~ @indices.join(',') ~ ")";
say "Output: '" ~ @result.join('') ~ "'";
}
say "Task 1: Shuffle String";
say "\nExample 1";
shuffle_string('lacelengh', (3,2,0,5,4,8,6,7,1));
say "\nExample 2";
shuffle_string('rulepark', (4,7,3,1,0,5,2,6));
Task 2: Zero Array
You are given an array of non-negative integers, @ints.
Write a script to return the minimum number of operations to make every element equal zero.
In each operation, you are required to pick a positive number less than or equal to the smallest element in the array, then subtract that from each positive element in the array.
Example 1:
Input: @ints = (1, 5, 0, 3, 5)
Output: 3
operation 1: pick 1 => (0, 4, 0, 2, 4)
operation 2: pick 2 => (0, 2, 0, 0, 2)
operation 3: pick 2 => (0, 0, 0, 0, 0)
Example 2:
Input: @ints = (0)
Output: 0
Example 3:
Input: @ints = (2, 1, 4, 0, 3)
Output: 4
operation 1: pick 1 => (1, 0, 3, 0, 2)
operation 2: pick 1 => (0, 0, 2, 0, 1)
operation 3: pick 1 => (0, 0, 1, 0, 0)
operation 4: pick 1 => (0, 0, 0, 0, 0)
This one I found a lot easier to understand for some reason.
#!/usr/bin/env perl
use v5.36;
use List::Util qw( min );
sub min_positive {
my @ints = grep { $_ > 0 } @_; # only consider positive numbers
return min @ints; # find smallest, undef if empty list
}
sub zero_array {
my @ints = @_;
say "Input: \@ints = (" . join(', ', @ints) . ")";
my @operations;
while ( my $min = min_positive(@ints) ) {
my $op_num = scalar(@operations) + 1;
foreach my $int ( @ints ) {
$int -= $min if $int > 0;
}
push @operations, "operation $op_num: pick $min => (" . join(', ', @ints) . ")";
}
say "Output: " . scalar(@operations);
if (@operations) {
say "";
say join "\n", @operations;
}
}
say "Task 2: Zero Array";
say "\nExample 1";
zero_array(1, 5, 0, 3, 5);
say "\nExample 2";
zero_array(0);
say "\nExample 3";
zero_array(2, 1, 4, 0, 3);
This one I’d like to pull apart a bit more. Picking “a positive number less than or equal to the smallest element in the array” sounded a lot like the min
function found in the List::Util
module, but that gives us the minimum value, not the minimum non-zero value, so I needed to filter the values equal to zero out of the array first. Initially, I did it like this:
min grep { $_ > 0 } @ints
but then I realized I needed to do that as part of the conditional to a loop, and I decided it would be a lot more readable if I pulled it out into it’s own function. Remember, I’m trying to express how easy things are in Perl, so I want to make my solutions completely readable and understandable to people who have never used Perl before.
I wanted the output to look exactly like the text in the examples, so I made the minimal extra effort to build an array of operations and put a bit of formatting into that so I could just dump the operations when I’d found out how many operations were necessary.
Again, my Raku solution looks like my Perl solution with a few syntax tweaks:
#!/usr/bin/env raku
sub min_positive (@ints) {
my @positive = @ints.grep({ $_ > 0 }); # only consider positive numbers
return unless @positive.elems; # return early if no positive nums
return @positive.reduce(&min); # find smallest
}
sub zero_array (@ints) {
say "Input: \@ints = (" ~ @ints.join(', ') ~ ")";
my @operations;
while ( my $min = min_positive(@ints) ) {
my $op_num = @operations.elems + 1;
for @ints <-> $int {
$int -= $min if $int > 0;
}
@operations.push("operation $op_num: pick $min => (" ~ @ints.join(', ') ~ ")");
}
say "Output: " ~ @operations.elems;
if (@operations) {
say "";
say @operations.join("\n");
}
}
say "Task 2: Zero Array";
say "\nExample 1";
zero_array([1, 5, 0, 3, 5]);
say "\nExample 2";
zero_array([0]);
say "\nExample 3";
zero_array([2, 1, 4, 0, 3]);
If you really want a good example of how Raku can be used to solve this problem, take a look at Bruce Gray’s solution.
And that’s it. I’ve already coded my solutions for Challenge #227, and I’ll be blogging about them soon.