Perl Weekly Challenge 362.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 362.

Task 1: Echo Chamber

Submitted by: Mohammad Sajid Anwar
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"

This task seems very straightforward. I split the input string, repeat each character an increasing number of times and output the result. This takes a half liner:

perl -E '
for(@ARGV){$c=1; say "$_ -> ", map{$_ x $c++}split ""}
' abca xyz code hello a

Results:

abca -> abbcccaaaa
xyz -> xyyzzz
code -> coodddeeee
hello -> heelllllllooooo
a -> a

The full code is:

 1  # Perl weekly challenge 362
 2  # Task 1:  Echo Chamber
 3  #
 4  # See https://wlmb.github.io/2026/02/23/PWC362/#task-1-echo-chamber
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 S0 S1...
 8      to repeat each character Cn of the string Sn=C0C1C2 n+1 times.
 9      FIN
10  for(@ARGV){
11      my $counter=1;
12      my $out = join "", map {$_ x $counter++} split "";
13      say "$_ -> $out";
14  }

Example:

./ch-1.pl abca xyz code hello a

Results:

abca -> abbcccaaaa
xyz -> xyyzzz
code -> coodddeeee
hello -> heelllllllooooo
a -> a

Task 2: Spellbound Sorting

Submitted by: Peter Campbell Smith
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 fun part of this task is converting numbers to text. I guess I’ll miss the fun and search for some CPAN packages. I found, for example, Lingua::EN::Numbers for numbers in English, Lingua::ES::Numeros for numbers in Spanish and Lingua::FR::Numbers for numbers in French. I make a sorter routine for each available language and I use a Schwartzian transform to sort the numbers alphabetically according to the chosen language. I could also use Unicode::Collate as some numbers might contain non-ascii characters, such as accented characters, but I guess it wouldn’t change the results for this task.

 1  # Perl weekly challenge 362
 2  # Task 2:  Spellbound Sorting
 3  #
 4  # See https://wlmb.github.io/2026/02/23/PWC362/#task-2-spellbound-sorting
 5  use v5.36;
 6  use feature qw(try);
 7  use List::UtilsBy qw(sort_by);
 8  use Scalar::Util qw(looks_like_number);
 9  die <<~"FIN" unless @ARGV;
10      Usage: $0 N0 N1...
11      to sort the numbers space separated list of numbers Nn
12      alphabetically. The language may be specified as the first
13      element in the list.
14      Current languages are English (default), Spanish or French.
15      FIN
16  for(@ARGV){
17      try {
18  	my ($Lang, @in)=split " ", $_;
19  	unshift(@in,$Lang), $Lang="English" if looks_like_number($Lang); # default
20  	my $sorter=undef;
21  	$sorter=\&english if $Lang=~/^English$/i;
22  	$sorter=\&spanish if $Lang=~/^Spanish$/i;
23  	$sorter=\&french  if $Lang=~/^French$/i;
24  	die "Unknown language: $Lang" unless defined $sorter;
25  	my @out=$sorter->(@in);
26  	say "@in in $Lang -> @out";
27      }
28      catch($e){warn $e;}
29  }
30  
31  sub english(@num){
32      use Lingua::EN::Numbers qw(num2en);
33      return map {$_->[0]} sort_by {$_->[1]} map{[$_, num2en($_)]}@num;
34  }
35  sub spanish(@num){
36      use Lingua::ES::Numeros;
37      my $trans = Lingua::ES::Numeros->new();
38      return map {$_->[0]} sort_by {$_->[1]} map{[$_, $trans->cardinal($_)]}@num;
39  }
40  sub french(@num){
41      use Lingua::FR::Numbers qw(number_to_fr);
42      return map {$_->[0]} sort_by {$_->[1]} map{[$_, number_to_fr($_)]}@num;
43  }

Examples:

./ch-2.pl "6 7 8 9 10" "-3 0 1000 99" "French 1 2 3 4 5" "English 1 2 3 4 5" \
	  "0 -1 -2 -3 -4" "100 101 102" "Spanish 1 2 3 4 5"

Results:

6 7 8 9 10 in English -> 8 9 7 6 10
-3 0 1000 99 in English -> -3 99 1000 0
1 2 3 4 5 in French -> 5 2 4 3 1
1 2 3 4 5 in English -> 5 4 1 3 2
0 -1 -2 -3 -4 in English -> -4 -1 -3 -2 0
100 101 102 in English -> 100 101 102
1 2 3 4 5 in Spanish -> 5 4 2 3 1

A compact version of this code without language default, without checking for errors and only for English and French would fit a two-liner.

Examples.

perl -MLingua::EN::Numbers=num2en -MLingua::FR::Numbers=number_to_fr -MList::UtilsBy=sort_by -E '
%s=(e=>\&num2en,f=>\&number_to_fr);%l=(e=>"English", "f"=>"French");for(@ARGV){my($l,@i)=
split" ";@o=map{$_->[0]} sort_by{$_->[1]} map{[$_, $s{$l}->($_)]}@i;say "@i in $l{$l} -> @o";}
' "e 6 7 8 9 10" "e -3 0 1000 99" "f 1 2 3 4 5" "e 1 2 3 4 5" \
	  "e 0 -1 -2 -3 -4" "e 100 101 102"

Results:

6 7 8 9 10 in English -> 8 9 7 6 10
-3 0 1000 99 in English -> -3 99 1000 0
1 2 3 4 5 in French -> 5 2 4 3 1
1 2 3 4 5 in English -> 5 4 1 3 2
0 -1 -2 -3 -4 in English -> -4 -1 -3 -2 0
100 101 102 in English -> 100 101 102
Written on February 23, 2026