Robbie Hatley's Solutions To The Weekly Challenge #217
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:
This week (2023-05-14 through 2023-05-20) is weekly challenge #217.
Task 1 is as follows:
"Given an n x n matrix of real numbers (where n >= 2), write a script to find 3rd smallest element in the sorted matrix."
This is pretty simple. To get the 3rd-smallest member of a matrix, just dump the rows into an array, sort it, and print element 2:
Robbie Hatley's Solution to The Weekly Challenge 217-1
Task 2 is as follows:
"You are given a list of positive integers. Write a script to concatenate the integers to form the highest possible value."
There are two basic ways I can see to approach this:
Firstly, one could make a special sort algorithm which sorts the numbers in such a way that, when they're concatenated, one gets the greatest possible numerical value. This, however, is tricky, in that the "order" between any two given elements may depend on elements other than those two elements. And it may require recursion. This would probably be the most efficient way, but I don't have 100 hours to devote to this project, and there is a simpler (though probably slower) way.
Secondly, one can use the "permutations" feature of CPAN module "Math::Combinatorics" to create a list of all possible concatenations, then use the "max" feature of CPAN module "List::Util" to grab the numerically-greated concatenation. This approach is probably slower than the one I describe in the paragraph above, but it's much more straightforward, so it's the approach I use here:
Robbie Hatley's Perl Solution to The Weekly Challenge 217-2
That's it for 217; see you on 218!
Comments
Post a Comment