Perl Weekly Challenge 278: Reverse Word

These are some answers to the Week 278, Task 2, 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 July 21, 2024, at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

Task 2: Reverse Word

You are given a word, $word and a character, $char.

Write a script to replace the substring up to and including $char with its characters sorted alphabetically. If the $char doesn’t exist, then DON'T do anything.

Example 1

Input: $str = "challenge", $char = "e"
Ouput: "acehllnge"

Example 2

Input: $str = "programming", $char = "a"
Ouput: "agoprrmming"

Example 3

Input: $str = "champion", $char = "b"
Ouput: "champion"

This task is not at all about reversing words, but I kept the title provided in the task description for proper reference.

Reverse Word in Raku

First, we use the index built-in routine to find the position of the first occurrence of $char in the input string. Then, we split the substring up to char into an array of individual characters and sort this array alphabetically (in ascending order), and join the characters into a string to provide the $prefix. We use the substr routine to extract the suffix, and stitch together the prefix and the suffix.

sub shuffle-word ($word is copy, $char) {
    my $ind = index $word, $char;
    return $word unless $ind;
    my $prefix = join "", sort $word.comb[0..$ind];
    return $prefix ~ substr $word, $ind + 1;
}

my @tests = <challenge e>, <programming a>, <champion, b>; 
for @tests -> @test {
    printf "%-12s %-2s => ", @test;
    say shuffle-word @test[0], @test[1];
}

This program displays the following output:

$ raku ./reverse-word.raku
challenge    e  => acehllnge
programming  a  => agoprrmming
champion,    b  => champion,

Reverse Word in Perl

This is a port to Perl of the above Raku program. There are a few syntax differences, but the basic built-in functions, index and substr, have essentially the same syntax in Perl and in Raku (at least for our purposes).

use strict;
use warnings;
use feature 'say';

sub shuffle_word {
    my ($word, $char) = @_;
    my $ind = index $word, $char;
    return $word unless $ind;
    my @prefix_letters = (split //, $word)[0..$ind];
    my $prefix = join "", sort @prefix_letters;
    return $prefix . substr $word, $ind + 1;
}

my @tests = ( [ qw<challenge e> ], 
              [ qw<programming a> ], 
              [ qw<champion b> ] ); 
for my $test (@tests) {
    printf "%-12s %-2s => ", @$test;
    say shuffle_word @$test;
}

This program displays the following output:

$ perl ./reverse-word.pl
challenge    e  => acehllnge
programming  a  => agoprrmming
champion     b  => champion

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 July 28, 2024. 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.