Full Circle
with Raku

by Arne Sommer

Full Circle with Raku

[384] Published 15. February 2026.

This is my response to The Weekly Challenge #360.

Mohammad has been spewing out weekly challenges for almost seven years now, and I have been doing them all since the second one. (I added the first one later on, to complete the collection.)

I had decided to keep it up for as long as Mohammad managed to come up with challenges, come rain or shine (but mostly indoor), and he is still at it. This is the 360th weekly challenge, which is rather impressive.

#360.1 Text Justifier You are given a string and a width.

Write a script to return the string that centers the text within that width using asterisks * as padding.

Example 1:
Input: $str = "Hi", $width = 5
Output: "*Hi**"

Text length = 2, Width = 5
Need 3 padding characters total
Left padding: 1 star, Right padding: 2 stars
Example 2:
Input: $str = "Code", $width = 10
Output: "***Code***"

Text length = 4, Width = 10
Need 6 padding characters total
Left padding: 3 stars, Right padding: 3 stars
Example 3:
Input: $str = "Hello", $width = 9
Output: "**Hello**"

Text length = 5, Width = 9
Need 4 padding characters total
Left padding: 2 stars, Right padding: 2 stars
Example 4:
Input: $str = "Perl", $width = 4
Output: "Perl"

No padding needed
Example 5:
Input: $str = "A", $width = 7
Output: "***A***"

Text length = 1, Width = 7
Need 6 padding characters total
Left padding: 3 stars, Right padding: 3 stars
Example 6:
Input: $str = "", $width = 5
Output: "*****"

Text length = 0, Width = 5
Entire output is padding
File: text-justifier
 #! /usr/bin/env raku

unit sub MAIN ($str,                                    # [1]
               Int $width where $width >= $str.chars);  # [2]

say ("*" x ($width - $str.chars) / 2)                   # [3]
  ~ $str                                                # [4]
  ~ ("*" x (($width - $str.chars) / 2) + 0.5);          # [5]

[1] A string. Note the missing length requirement, as the 6th example uses an empty string.

[2] Ensure that the width argument is big enough to consume (encompass, wrap, ...) the string.

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

[3] Print the prefix stars, using the string repetition operator x and half the total padding required.

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

[4] Print the string itself.

[5] Print the postfix stars. Note the + 0.5 so that an odd number of padding will have the odd one added here (at the end).

Running it:

$ ./text-justifier hi 5
*hi**

$ ./text-justifier Code 10
***Code***

$ ./text-justifier Hello 9
**Hello**

$ ./text-justifier Perl 4
Perl

$ ./text-justifier A 7
***A***

$ ./text-justifier "" 5
*****

Looking good.

This is basically a one line; [3], [4] and [5], so verbose mode did not fit in.

#360.2 Word Sorter You are give a sentence.

Write a script to order words in the given sentence alphabetically but keeps the words themselves unchanged.

Example 1:
Input: $str = "The quick brown fox"
Output: "brown fox quick The"
Example 2:
Input: $str = "Hello    World!   How   are you?"
Output: "are Hello How World! you?"
Example 3:
Input: $str = "Hello"
Output: "Hello"
Example 4:
Input: $str = "Hello, World! How are you?"
Output: "are Hello, How World! you?"
Example 5:
Input: $str = "I have 2 apples and 3 bananas!"
Output: "2 3 and apples bananas! have I"

File: word-sorter
#! /usr/bin/env raku

unit sub MAIN ($str where $str.chars > 0);  # [1]

say $str.words.sort({ .lc }).join(" ");     # [2]

[1] A string with at least one character, this time.

[2] Split the string into words, sort them by the lowercase versions of the strings with sort({ .lc }) and join them up again into a sentence (of sorts, pun intended) with join.

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

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

Running it:

$ ./word-sorter "The quick brown fox"
brown fox quick The

$ ./word-sorter "Hello    World!   How   are you?"
are Hello How World! you?

$ ./word-sorter Hello
Hello

$ ./word-sorter "Hello, World! How are you?"
are Hello, How World! you?

$ ./word-sorter "I have 2 apples and 3 bananas!"
2 3 and apples bananas! have I

Looking good.

No verbose for this one either.

And that's it.