Perl Weekly Challenge 384.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 384.
Task 1: Base N
Submitted by: Mohammad Sajid Anwar
You are given a number and a base integer.
Write a script to convert the given number in the given base
integer.
Example 1
Input: $num = 42, $base = 2
Output: 101010
Example 2
Input: $num = 15642094, $base = 16
Output: EEADEE
Example 3
Input: $num = 493, $base = 8
Output: 755

Example 4
Input: $num = 2228519, $base = 36
Output: 1BRJB
Base 36 uses numbers 0-9 and letters A-Z.
Example 5
Input: $num = 123456789, $base = 64
Output: 7MyqL
Base 64 (using 0-9, A-Z, a-z, and extra symbols like +
and /)
The succesive digits of Dn of a number N=∑Dn Bn in base B may be obtained by setting N0=N and then iteratively setting Dn=Nn mod B and Nn+1=Nn/B. The iteration stops when the division yields 0. Then we can convert the value of each to its corresponding symbol using an array. The result takes a two-liner.
Examples:
perl -E '
$"="";@d=(0..9, "A".."Z", "a".."z", "+", "/");for my($N,$B)(@ARGV){$n=$N;
my @o;unshift(@o,$d[$n%$B]),$n=floor $n/$B while($n);say"$N, $B -> @o"}
' 42 2 15642094 16 493 8 2228519 36 123456789 64
Results:
42, 2 -> 101010
15642094, 16 -> EEADEE
493, 8 -> 755
2228519, 36 -> 1BRJB
123456789, 64 -> 7MyqL
The full code is:
1 # Perl weekly challenge 384
2 # Task 1: Base N
3 #
4 # See https://wlmb.github.io/2026/07/27/PWC384/#task-1-base-n
5 use v5.40;
6 use feature qw(try);
7 $"="";
8 die <<~"FIN" unless @ARGV && @ARGV%2==0;
9 Usage: $0 N0 B0 N1 B1...
10 to write the number Ni in base Bn.
11 FIN
12 my @digit=(0..9, "A".."Z", "a".."z", "+", "/");
13 my $maxbase=@digit;
14 for my($N,$B)(@ARGV){
15 try{
16 die "Base should an integer: $B" unless $B==floor $B;
17 die "Base should be larger than 1: $B" unless $B>1;
18 die "Base too large; can only handle up to $maxbase: $B"
19 unless $B<=$maxbase;
20 die "I only manage integer numbers: $N" unless floor $N==$N;
21 my $sign=$N<0?"-":"";
22 my $rest=$N<0?-$N:$N;
23 my @output;
24 while($rest){
25 unshift(@output, $digit[$rest%$B]);
26 $rest=floor $rest/$B;
27 }
28 my $output="@output";
29 say"$N, $B -> $output"
30 }
31 catch($e){ warn $e; }
32 }
Example:
./ch-1.pl 42 2 15642094 16 493 8 2228519 36 123456789 64
Results:
42, 2 -> 101010
15642094, 16 -> EEADEE
493, 8 -> 755
2228519, 36 -> 1BRJB
123456789, 64 -> 7MyqL
Task 2: Special Binary Substrings
Submitted by: Mohammad Sajid Anwar
You are given a binary string.
Write a script to return all non-empty substrings (distinct)
that have the same number of 0’s and 1’s, and all the 0’s
and all the 1’s in these substrings are grouped
consecutively.
Example 1
Input: $binary = "0101"
Output: ("01", "10")
Example 2
Input: $binary = "000111"
Output: ("000111", "0011", "01")
Example 3
Input: $binary = "000011"
Output: ("0011", "01")
Example 4
Input: $binary = "10011100"
Output: ("10", "0011", "01", "1100")
Example 5
Input: $binary = "00000"
Output: ()
I can look for a pattern of n zeroes followed by n ones or viceverse for every possible length, from 1 upto half the string length. This takes a 1.5-liner.
perl -E '
for(@ARGV){$s=$_;$l=length($s)/2;say"$_ -> ",join" ",map{$s=~
/(0{$_}1{$_})/,$s=~/(1{$_}0{$_})/} 1..$l}
' 0101 000111 000011 10011100 00000
Results:
0101 -> 01 10
000111 -> 01 0011 000111
000011 -> 01 0011
10011100 -> 01 10 0011 1100
00000 ->
I guess this could be done more compactly with a smarter match, but I didn’t pursue it.
The full code is:
1 # Perl weekly challenge 384
2 # Task 2: Special Binary Substrings
3 #
4 # See https://wlmb.github.io/2026/07/27/PWC384/#task-2-special-binary-substrings
5 use v5.36;
6 die <<~"FIN" unless @ARGV;
7 Usage: $0 S0 S1...
8 to look for substrings of Sn of the form
9 00...11... or 11...00... with the same number of
10 consecutive ones and zeroes.
11 FIN
12 for(@ARGV){
13 my $string=$_;
14 my $length=length($string)/2;
15 say"$_ -> (",(
16 join " ",
17 map {
18 $string=~/(0{$_}1{$_})/,
19 $string=~/(1{$_}0{$_})/
20 } 1..$length
21 ), ")";
22 }
Example:
./ch-2.pl 0101 000111 000011 10011100 00000
Results:
0101 -> (01 10)
000111 -> (01 0011 000111)
000011 -> (01 0011)
10011100 -> (01 10 0011 1100)
00000 -> ()
/;