Convert Product
with Raku

by Arne Sommer

Convert Product with Raku

[417] Published 26. September 2026.

This is my response to The Weekly Challenge #392.

#392.1 Convert Palindrome You are given a string.

Write a script to convert the given string to palindrome by adding characters in front of it.

Example 1:
Input: $str = "pinnipeds"
Output: "sdepinnipeds"
Example 2:
Input: $str = "abcd"
Output: "dcbabcd"
Example 3:
Input: $str = "bananas"
Output: "sananabananas"
Example 4:
Input: $str = "dissident"
Output: "tnedissident"
Example 5:
Input: $str = "cailliachs"
Output: "shcailliachs"
File: convert-palindrome
#! /usr/bin/env raku

unit sub MAIN (Str $str, :v(:$verbose));

my $n   = $str.chars;
my $rev = $str.flip;
my $m   = $n;

$m-- while $m > 1 && $str.substr(0, $m) ne $rev.substr($n - $m);

if $verbose
{
  say ": Palindromic prefix: { $str.substr(0, $m) }";
  say ": Prepending: { $rev.substr(0, $n - $m) }";
}

say $rev.substr(0, $n - $m) ~ $str;

[3] The input string, without any constraints.

[5] Get the length of the string.

[6] Get the reverse string with flip.

See docs.raku.org/routine/flip for more information about flip.

[7] A copy.

[9] Count down (to a smaller string) as long as the substring is not palindromic.

[17] Add the missing part (of the reversed string) before the string itself.

Running it:

$ ./convert-palindrome pinnipeds
sdepinnipeds

$ ./convert-palindrome abcd
dcbabcd

$ ./convert-palindrome bananas
sananabananas

$ ./convert-palindrome dissident
tnedissident

$ ./convert-palindrome cailliachs
shcailliachs

Looking good.

With verbose mode:

$ ./convert-palindrome -v pinnipeds
: Palindromic prefix: pinnip
: Prepending: sde
sdepinnipeds

$ ./convert-palindrome -v abcd
: Palindromic prefix: a
: Prepending: dcb
dcbabcd

$ ./convert-palindrome -v bananas
: Palindromic prefix: b
: Prepending: sanana
sananabananas

$ ./convert-palindrome -v dissident
: Palindromic prefix: dissid
: Prepending: tne
tnedissident

$ ./convert-palindrome -v cailliachs
: Palindromic prefix: cailliac
: Prepending: sh
shcailliachs

#392.2 Words Length Product You are given an array of strings.

Write a script to return the maximum value of len($words[i]) * len($words[j]) where the two words do not share common letters. If no such two words exist, return 0.

Example 1:
Input: @words = ("a", "ab", "abc", "d", "de", "def")
Output: 9

Two words are "abc" and "def".
Example 2:
Input: @words = ("a", "aa", "aaa", "aaaa")
Output: 0

Since no two words can be chosen without sharing letters, the result is 0.
Example 3:
Input: @words = ("meet", "app", "code", "sky", "bold")
Output: 16

Two words are "meet" and "bold".
Example 4:
Input: @words = ("a", "ab", "abc", "abcd", "efghi")
Output: 20

Two words are "abcd" and "efghi".
Example 5:
Input: @words = ("xyz", "w", "abcdefg", "hij")
Output: 21

Two words are "abcdefg" and "hij".
File: words-length-product
#! /usr/bin/env raku

unit sub MAIN (*@words where all(@words) ~~ /<[a..z]>+/,
               :v(:$verbose));

my $max  = 0;

for 0 ..^ @words.elems -> $i
{
  for $i + 1 ..^ @words.elems -> $j
  {
    next if @words[$i].comb ∩ @words[$j].comb;

    my $product = @words[$i].chars * @words[$j].chars;

    if $product >= $max
    {
      $max = $product;

      say ": Pair: @words[$i] & @words[$j] -> $product [high]"
        if $verbose;
    }
    elsif $verbose
    {
      say ": Pair: @words[$i] & @words[$j] -> $product";
    }
  }
}

say $max;

[3] All the words must contain lowercase letters only, at least one. Note that no words is allowed, even if not very useful.

[6] The highest product will end up here.

[8] Iterate over the indices of the words in the array.

[10] Iterate over the indices of the words to the right of the one in [8].

[12] Split each word into individual characters (with comb) and take the intersection (with the intersection operator ∩, or the ascii version (&)). Skip combinations where the same character occurs in both.

See docs.raku.org/routine/(&), infix ∩ for more information about the intersection operator.

[14] Get the product of the length of the two words.

[16] Do we have a new maximum?

[18] If so, take note.

[30] Print the result.

Running it:

$ ./words-length-product a ab abc d de def
9

$ ./words-length-product a aa aaa aaaa
0

$ ./words-length-product meet app code sky bold
16

$ ./words-length-product a ab abc abcd efghi
20

$ ./words-length-product xyz w abcdefg hij
21

Looking good.

With verbose mode:

$ ./words-length-product -v a ab abc d de def
: Pair: abc and def
9

$ ./words-length-product -v a aa aaa aaaa
0

$ ./words-length-product -v meet app code sky bold
: Pair: meet and bold
16

$ ./words-length-product -v a ab abc abcd efghi
: Pair: abcd and efghi
20

$ ./words-length-product -v xyz w abcdefg hij
: Pair: abcdefg and hij
21

And that's it.