Perl Weekly Challenge 301.

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

Task 1: Largest Number

Submitted by: Mohammad Sajid Anwar
You are given a list of positive integers, @ints.

Write a script to arrange all the elements in the given list such
that they form the largest number and return it.

Example 1
Input: @ints = (20, 3)
Output: 320
Example 2
Input: @ints = (3, 30, 34, 5, 9)
Output: 9534330

I simply sort and join the numbers a and b by comparing ab with ba. This yields a half-liner.

Example 1:

perl -E '
say "@ARGV -> ", join "", sort {$b.$a <=> $a.$b} @ARGV;
' 20 3

Results:

20 3 -> 320

Example 2:

perl -E '
say "@ARGV -> ", join "", sort {$b.$a <=> $a.$b} @ARGV;
' 3 30 34 5 9

Results:

3 30 34 5 9 -> 9534330

The full code is almost identical.

 1  # Perl weekly challenge 301
 2  # Task 1:  Largest Number
 3  #
 4  # See https://wlmb.github.io/2024/12/23/PWC301/#task-1-largest-number
 5  use v5.36;
 6  die <<~"FIN" unless @ARGV;
 7      Usage: $0 I1 I2...
 8      to produce the largest number by joining
 9      the integers I1 I2... in the best order.
10      FIN
11  say "@ARGV -> ", join "", sort {$b.$a <=> $a.$b} @ARGV;

Examples:

./ch-1.pl 20 3
./ch-1.pl 3 30 34 5 9

Results:

20 3 -> 320
3 30 34 5 9 -> 9534330

Task 2: Hamming Distance

Submitted by: Mohammad Sajid Anwar
You are given an array of integers, @ints.

Write a script to return the sum of Hamming distances between
all the pairs of the integers in the given array of integers.


The Hamming distance between two integers is the number of places
in which their binary representations differ.

Example 1
Input: @ints = (4, 14, 2)
Output: 6

Binary representation:
4  => 0100
14 => 1110
2  => 0010

HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2)
    = 2 + 2 + 2 = 6.
Example 2
Input: @ints = (4, 14, 4)
Output: 4

I can generate all pairs, take the exclusive or of their elements to find out the bits that differ, convert the result to a string of 1’s and 0’s, split it and sum it. To generate the pairs I can use combinations from Algorithm::Combinatorics. I use sum from List::Utils. The result fits a one liner.

Example 1:

perl -MAlgorithm::Combinatorics=combinations -MList::Util=sum -E '
say "@ARGV -> ", sum map {sum split "", sprintf "%b", $_->[0]^$_->[1]} combinations(\@ARGV,2);
' 4 14 2

Results:

4 14 2 -> 6

Example 2:

perl -MAlgorithm::Combinatorics=combinations -MList::Util=sum -E '
say "@ARGV -> ", sum map {sum split "", sprintf "%b", $_->[0]^$_->[1]} combinations(\@ARGV,2);
' 4 14 4

Results:

4 14 4 -> 4

The full code is similar.

 1  # Perl weekly challenge 301
 2  # Task 2:  Hamming Distance
 3  #
 4  # See https://wlmb.github.io/2024/12/23/PWC301/#task-2-hamming-distance
 5  use v5.36;
 6  use Algorithm::Combinatorics qw(combinations);
 7  use List::Util qw(sum);
 8  die <<~"FIN" unless @ARGV;
 9      Usage: $0 N1 N2...
10      to find the sum of the  Hamming distances between all pairs of
11      numbers taken from the list N1 N2...
12      FIN
13  say "@ARGV -> ",               # read the following comments upwards
14      sum                        # sum them
15      map {                      # get Hamming distance for each pair
16  	sum                    # sum them
17  	    split "",          # separate binary digits
18  	    sprintf "%b",      # convert to binary
19  	    $_->[0]^$_->[1]    # exclusive or -> 1 when bits differ, 0 when they are equal
20  } combinations(\@ARGV,2);      # generate all pairs of numbers

Examples:

./ch-2.pl 4 14 2
./ch-2.pl 4 14 4

Results:

4 14 2 -> 6
4 14 4 -> 4

/;

Written on December 23, 2024