Perl Weekly Challenge 379.

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

Task 1: Reverse String

Submitted by: Mohammad Sajid Anwar
You are given a string.

Write a script to reverse the given string without using
standard reverse function.

Example 1
Input: $str = ""
Output: ""

Example 2
Input: $str = "reverse the given string"
Output: "gnirts nevig eht esrever"

Example 3
Input: $str = "Perl is Awesome"
Output: "emosewA si lreP"

Example 4
Input: $str = "v1.0.0-Beta!"
Output: "!ateB-0.0.1v"

Example 5
Input: $str = "racecar"
Output: "racecar"


The idea is to implement the reverse function. A simple solution is to split the letters of the string, unshift them into an array, so that the last (right-most) character becomes the first character in the array. Then, I can join back the letters of the array. This takes a one-liner:

Examples:

perl -E '
for(@ARGV){my @x;  unshift @x,$_ for split "";say "\"$_\" -> \"", @x,"\""}
' "" "reverse the given string" "Perl is Awesome" "v1.0.0-Beta!" "racecar"

Results:

"" -> ""
"reverse the given string" -> "gnirts nevig eht esrever"
"Perl is Awesome" -> "emosewA si lreP"
"v1.0.0-Beta!" -> "!ateB-0.0.1v"
"racecar" -> "racecar"

The full code is:

 1  # Perl weekly challenge 379
 2  # Task 1:  Reverse String
 3  #
 4  # See https://wlmb.github.io/2026/06/22/PWC379/#task-1-reverse-string
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 S0 S1...
 8      to reverse the string Sn
 9      FIN
10  for(@ARGV){
11      my @reversed;
12      unshift @reversed, $_ for split "";
13      say "\"$_\" -> \"", @reversed,"\""
14  }
15  

Examples:

./ch-1.pl "" "reverse the given string" "Perl is Awesome" "v1.0.0-Beta!" "racecar"

Results:

"" -> ""
"reverse the given string" -> "gnirts nevig eht esrever"
"Perl is Awesome" -> "emosewA si lreP"
"v1.0.0-Beta!" -> "!ateB-0.0.1v"
"racecar" -> "racecar"

Task 2: Armstrong Number

Submitted by: Mohammad Sajid Anwar
You are given two integers, $base and $limit.

Write a script to find all Armstrong numbers in base $base
that are less than $limit.

If raising each of the digits of a nonnegative integer to
the power of the total number of digits, then taking the
sum, equals the original number, it is an Armstrong number.

Example 1
Input: $base = 10, $limit = 1000
Output: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407)

Example 2
Input: $base = 7, $limit = 1000
Output: (0, 1, 2, 3, 4, 5, 6, 10, 25, 32, 45, 133, 134,
         152, 250)

Example 3
Input: $base = 16, $limit = 1000
Output: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
         15, 342, 371, 520, 584, 645)

I can generate the digits in the given $base of succesive numbers up to $limit by incrementing by one each succesive digit until there is no overflow, and replacing overflowed digits by 0. Given the array of digits, its size is the power to which I have to raise each digit, sum the powers of each digit and compare to the current number to decide if it is and Armstrong number. The result fits a two-liner

Examples:

perl -MList::Util=sum -E '
for my($t,$l)(@ARGV){my@r;my@d=(-1);for(0..$l){for(0..@d){last unless ++$d[$_]
==$t;$d[$_]=0;}push@r,$_ if (sum map{$_**@d}@d)==$_}say "$t, $l -> @r";}
' 10 1000 7 1000 16 1000

Results:

10, 1000 -> 0 1 2 3 4 5 6 7 8 9 153 370 371 407
7, 1000 -> 0 1 2 3 4 5 6 10 25 32 45 133 134 152 250
16, 1000 -> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 342 371 520 584 645

The full code is:

 1  # Perl weekly challenge 379
 2  # Task 2:  Armstrong Number
 3  #
 4  # See https://wlmb.github.io/2026/06/22/PWC379/#task-2-armstrong-number
 5  use v5.36;
 6  use List::Util qw(sum);
 7  die <<~"FIN" unless @ARGV and @ARGV%2==0;
 8      Usage: $0 B0 L0 B1 L1...
 9      to list all Armstrong numbers up to Ln in base Bn
10      FIN
11  for my($base, $limit)(@ARGV){
12      my @result;
13      my @digits = (-1);
14      for(0..$limit){
15          for(0..@digits){ # increment by 1
16              last unless ++$digits[$_] == $base; # last unless overflow
17              $digits[$_] = 0;                    # reset overflowed digit
18          }
19          push @result, $_ if (sum map {$_**@digits} @digits) == $_;
20      }
21      say "base=$base, limit=$limit -> @result";
22  }

Example:

./ch-2.pl 10 1000 7 1000 16 1000

Results:

base=10, limit=1000 -> 0 1 2 3 4 5 6 7 8 9 153 370 371 407
base=7, limit=1000 -> 0 1 2 3 4 5 6 10 25 32 45 133 134 152 250
base=16, limit=1000 -> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 342 371 520 584 645

/;

Written on June 22, 2026