Spellbound Echo
with Raku

by Arne Sommer

Spellbound Echo with Raku

[386] Published 28. February 2026.

This is my response to The Weekly Challenge #362.

#362.1 Echo Chamber You are given a string containing lowercase letters.

Write a script to transform the string based on the index position of each character (starting from 0). For each character at position i, repeat it i + 1 times.

Example 1:
Input: "abca"
Output: "abbcccaaaa"

Index 0: "a" -> repeated 1 time  -> "a"
Index 1: "b" -> repeated 2 times -> "bb"
Index 2: "c" -> repeated 3 times -> "ccc"
Index 3: "a" -> repeated 4 times -> "aaaa"
Example 2:
Input: "xyz"
Output: "xyyzzz"

Index 0: "x" -> "x"
Index 1: "y" -> "yy"
Index 2: "z" -> "zzz"
Example 3:
Input: "code"
Output: "coodddeeee"

Index 0: "c" -> "c"
Index 1: "o" -> "oo"
Index 2: "d" -> "ddd"
Index 3: "e" -> "eeee"
Example 4:
Input: "hello"
Output: "heelllllllooooo"

Index 0: "h" -> "h"
Index 1: "e" -> "ee"
Index 2: "l" -> "lll"
Index 3: "l" -> "llll"
Index 4: "o" -> "ooooo"
Example 5:
Input: "a"
Output: "a"

Index 0: "a" -> "a"
File: echo-chamber
#! /usr/bin/env raku

unit sub MAIN ($string where $string ~~ /^<[a..z]>+$/);

(^$string.chars).map({ $string.substr($_, 1) x ($_ + 1) }).join.say;

[$string.chars] The length of the string.

[^...] The upto operator, giving a sequence of integers from zero up to (but not including) the given value. This gives us the indices for all the characters in the string.

[map] Apply the argument on all the indices.

[$string.substr($_, 1)] Get the character at that index.

[x] Use the string repetition operator x to repeat the single character as required; i.e. one more time than the index.

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

[join] Join the strings together as a single string

[say] And print that string.

Running it:

$ ./echo-chamber abca
abbcccaaaa

$ ./echo-chamber xyz
xyyzzz

$ ./echo-chamber code
coodddeeee

$ ./echo-chamber hello
heelllllllooooo

$ ./echo-chamber a
a

Looking good.

No verbose mode for this one, as it is basically a one liner.

#362.2 Spellbound Sorting You are given an array of integers.

Write a script to return them in alphabetical order, in any language of your choosing. Default language is English.

Example 1:
Input: (6, 7, 8, 9, 10)
Output: (8, 9, 7, 6, 10)

eight, nine, seven, six, ten
Example 2:
Input: (-3, 0, 1000, 99)
Output: (-3, 99, 1000, 0)

minus three, ninety-nine, one thousand, zero
Example 3:
Input: (1, 2, 3, 4, 5)

Output: (5, 2, 4, 3, 1) for French language
cinq, deux, quatre, trois, un

Output: (5, 4, 1, 3, 2) for English language
five, four, one, three, two
Example 4:
Input: (0, -1, -2, -3, -4)
Output: (-4, -1, -3, -2, 0)

minus four, minus one, minus three, minus two, zero
Example 5:
Input: (100, 101, 102)
Output: (100, 101, 102)

one hundred, one hundred and one, one hundred and two

The «Lingua::EN::Numbers» module does this translation, and it would be silly not to use it. It must be installed, though. It can be argued that only core modules should be allowed in the challenges, but I will not do so.

File: spellbound-sorting
#! /usr/bin/env raku

use Lingua::EN::Numbers;

unit sub MAIN (*@integers where all(@integers) ~~ Int && @integers.elems > 0)); # [1]

@integers.sort({ cardinal($^a) cmp cardinal($^b) }).join(", ").say;             # [2]

[1] Integers only, and at least one of them.

[2] Apply the «cardinal» function from the «Lingua::EN::Numbers» module on each value while we do the value comparison inside the sort.

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

Running it:

$ ./spellbound-sorting 6 7 8 9 10
8, 9, 7, 6, 10

$ ./spellbound-sorting -- -3 0 1000 99
-3, 99, 1000, 0

$ ./spellbound-sorting 1 2 3 4 5
5, 4, 1, 3, 2

$ ./spellbound-sorting 0 -1 -2 -3 -4
-4, -1, -3, -2, 0

$ ./spellbound-sorting 100 101 102
100, 101, 102

Looking good.

Verbose mode requires a rewrite. This time the «cardinal» function is only called once for each argument, and the sort function looks up the value in a hash.

File: spellbound-sorting-verbose
#! /usr/bin/env raku

use Lingua::EN::Numbers;

unit sub MAIN (*@integers where all(@integers) ~~ Int && @integers.elems > 0,
               :v(:$verbose));

my %spelling = @integers.map({ $_ => cardinal($_) });

@integers.map({ say ": $_ => %spelling{$_}" }) if $verbose;

@integers.sort({ %spelling{$^a} cmp %spelling{$^b} }).join(", ").say;

Running it, with verbose mode:

$ ./spellbound-sorting-verbose -v 6 7 8 9 10
: 6 => six
: 7 => seven
: 8 => eight
: 9 => nine
: 10 => ten
8, 9, 7, 6, 10

$ ./spellbound-sorting-verbose -v -- -3 0 1000 99
: -3 => negative three
: 0 => zero
: 1000 => one thousand
: 99 => ninety-nine
-3, 99, 1000, 0

$ ./spellbound-sorting-verbose -v 1 2 3 4 5
: 1 => one
: 2 => two
: 3 => three
: 4 => four
: 5 => five
5, 4, 1, 3, 2

$ ./spellbound-sorting-verbose -v  0 -1 -2 -3 -4
: 0 => zero
: -1 => negative one
: -2 => negative two
: -3 => negative three
: -4 => negative four
-4, -1, -3, -2, 0

$ ./spellbound-sorting-verbose -v  100 101 102
: 100 => one hundred
: 101 => one hundred one
: 102 => one hundred two
100, 101, 102

The «Lingua::Number» module has support for several languages, but it is broken. At least on both of my pcs (running Rakudo v2026.01). A program using it is included in the zip file as «spellbound-sorting-multi».

And that's it.