Perl Weekly Challenge 212: Jumping Letters

These are some answers to the Week 212 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on April 16, 2023 at 23:59). This blog post offers some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 1: Jumping Letters

You are given a word having alphabetic characters only, and a list of positive integers of the same length.

Write a script to print the new word generated after jumping forward each letter in the given word by the integer in the list. The given list would have exactly the number as the total alphabets in the given word.

Example 1

Input: $word = 'Perl' and @jump = (2,22,19,9)
Output: Raku

'P' jumps 2 place forward and becomes 'R'.
'e' jumps 22 place forward and becomes 'a'. (jump is cyclic, i.e. after 'z' you go back to 'a')
'r' jumps 19 place forward and becomes 'k'.
'l' jumps 9 place forward and becomes 'u'.

Example 2

Input: $word = 'Raku' and @jump = (24,4,7,17)
Output: 'Perl'

Jumping Letters in Raku

The ord routine translates a letter into its ASCII code (well, really, it’s a Unicode code point, but it is equivalent for our purpose here with values less than 128). The chr performs the inverse operation. So we can simply convert each letter of the input, add the relevant jump value and convert the result back to a letter. One little complication is that we need to subtract 26 from the code point if it gets beyond the upper case and lower case letter ranges after having added the jump value.

sub jump-letter ($letter, $val) {
    my $new_ascii = $letter.ord + $val;
    return ($new_ascii - 26).chr if $new_ascii > 'z'.ord;
    return ($new_ascii - 26).chr if $letter le 'Z'
        and $new_ascii > 'Z'.ord;
    return $new_ascii.chr;
}
my @test = "Perl", <2 22 19 9>;
for ("Perl", <2 22 19 9>), ("Raku", <24 4 7 17>) -> @test {
    printf "%-10s => ", "@test[0]";
    for @test[0].comb Z @test[1].Array -> $a {
        print jump-letter $a[0], $a[1];
    }
    say " ";
}

This script displays the following output:

$ raku ./jumping-letters.raku
Perl       => Raku
Raku       => Perl

Jumping Letters in Perl

This is a port to Perl of the Raku program above. Please refer to the previous section if you need some explanations.

use strict;
use warnings;
use feature "say";

sub jump_letter  {
    my ($letter, $val) = @_;
    my $new_ascii = ord($letter) + $val;
    return chr($new_ascii - 26) if $new_ascii > ord 'z';
    return chr($new_ascii - 26) if $letter le 'Z'
        and $new_ascii > ord 'Z';
    return chr $new_ascii;
}

for my $test (["Perl", [<2 22 19 9>]], ["Raku", [<24 4 7 17>]]) {
    printf "%-10s => ", "$test->[0]";
    my @letters = split //, $test->[0];
    for my $i (0..$#letters) {
        print jump_letter $letters[$i], $test->[1][$i];
    }
    say " ";
}

This script displays the following output:

$ perl  ./jumping-letters.pl
Perl       => Raku
Raku       => Perl

Task 2: Rearrange Groups

You are given a list of integers and group size greater than zero.

Write a script to split the list into equal groups of the given size where integers are in sequential order. If it can’t be done then print -1.

Example 1:

Input: @list = (1,2,3,5,1,2,7,6,3) and $size = 3
Output: (1,2,3), (1,2,3), (5,6,7)

Example 2:

Input: @list = (1,2,3) and $size = 2
Output: -1

Example 3:

Input: @list = (1,2,4,3,5,3) and $size = 2
Output: (1,2,3), (3,4,5)

Example 4:

Input: @list = (1,5,2,6,4,7) and $size = 3
Output: -1

First, I think that example 3 above is wrong. I believe that size should probably be 3 for the example to make sense.

Update: this error in the task specifications has now been fixed.

Second, even though I started working on this second task (and think I probably have a working solution in Raku), I have no time today to complete this task, and probably won’t have time for several days. I still wanted to make my solutions to task 1 available today. I’ll hopefully write a new blog post or update this one later on.

Update: I have now written a second blog post dated April 13, 2023, providing solutions to this task 2 of the challenge.

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on April 23, 2023. And, please, also spread the word about the Perl Weekly Challenge if you can.

Leave a comment

About laurent_r

user-pic I am the author of the "Think Perl 6" book (O'Reilly, 2017) and I blog about the Perl 5 and Raku programming languages.