Robbie Hatley's Solutions To The Weekly Challenge #251

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

This week (2024-01-07 through 2024-01-13) is weekly challenge #251. Its tasks are as follows:

Task 251-1: Concatenation Value
Submitted by: Mohammad S Anwar
You are given an array of integers, @ints. Write a script to
find the concatenation value of the given array. The
concatenation of two numbers is the number formed by
concatenating their numerals. For example, the concatenation
of 10, 21 is 1021. The concatenation value of @ints is initially
equal to 0. Perform this operation until @ints becomes empty:
If there exists more than one number in @ints, pick the first
element and last element in @ints respectively and add the value
of their concatenation to the concatenation value of @ints, then
delete the first and last element from @ints. If one element
exists, add its value to the concatenation value of @ints, then
delete it.

Example 1:
Input: @ints = (6, 12, 25, 1)
Output: 1286
1st operation: concatenation of 6 and 1 is 61
2nd operation: concaternation of 12 and 25 is 1225
Concatenation Value => 61 + 1225 => 1286

Example 2:
Input: @ints = (10, 7, 31, 5, 2, 2)
Output: 489
1st operation: concatenation of 10 and 2 is 102
2nd operation: concatenation of 7 and 2 is 72
3rd operation: concatenation of 31 and 5 is 315
Concatenation Value => 102 + 72 + 315 => 489

Example 3:
Input: @ints = (1, 2, 10)
Output: 112
1st operation: concatenation of 1 and 10 is 110
2nd operation: only element left is 2
Concatenation Value => 110 + 2 => 112

This is only going to work for arrays of positive integers, not a general "array of integers", as integers such as "-17" and "0" are going to mess-up concatenations, so I'll import "sub are_pos_ints" from my solution to 243-1. The "concatenation" of the first and last elements of an array @a of positive-integer strings can be done by "$v += (0 + ($a[0].$a[-1]))". The rest is simple.

Robbie Hatley's Perl Solution to The Weekly Challenge 251-1

Task 251-2: Lucky Numbers
Submitted by: Mohammad S Anwar
You are given a m x n matrix of distinct numbers. Write a script
to return the lucky number, if there is one, or -1 if not.
A lucky number is an element of the matrix such that it is
the minimum element in its row and maximum in its column.

Example 1:
Input: $matrix = [ [ 3,  7,  8],
                   [ 9, 11, 13],
                   [15, 16, 17] ];
Output: 15
15 is the only lucky number since it is the minimum in its row
and the maximum in its column.

Example 2:
Input: $matrix = [ [ 1, 10,  4,  2],
                   [ 9,  3,  8,  7],
                   [15, 16, 17, 12] ];
Output: 12

Example 3
Input: $matrix = [ [7 ,8],
                   [1 ,2] ];
Output: 7

First of all, "distinct numbers" is problematic because it would necessitate running equality comparison between real numbers, which is problematic in computer programming because most computers do not store exact representations of numbers such as 2/3 or π, much less 0.37e-5.16(3/7)πi. I'll import an "are_ints" sub from an earlier PWCC submission to make sure all array elements are integers.

Secondly, I have to wonder if a matrix of integers always has either 0 or 1 "magic numbers"? It seems to me that a matrix might have several. So I'll check for that by testing each row minimum to see if it's a column maximum.

Thirdly, I can't use -1 to indicate "no lucky number" because -1 might actually be a "lucky number" for a given matrix! So instead, if there are no "lucky numbers", I'll print "Lucky numbers: None.".

Robbie Hatley's Perl Solution to The Weekly Challenge 251-2

That's it for challenge 251; see you on challenge 252!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #221

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #262