# RogerBW's Blog

The Weekly Challenge 217: Sorted Max 21 May 2023

I’ve been doing the Weekly Challenges. The latest involved various sorts of sorting. (Note that this closes today.)

Task 1: Sorted Matrix

You are given a `n x n` matrix where `n >= 2`.

Write a script to find 3rd smallest element in the sorted matrix.

Examples indicate that no deduplication is needed, so there will always be a third-smallest element.

Clearly the approach is to flatten everything into a list, sort it, and extract the third element. Flattening is one of those relatively new features that languages do in very different ways (or, in some cases, not at all – I confess I've never particularly missed it in Perl). In Raku:

``````sub sortedmatrix(@matrix) {
my @n = @matrix[*;*];
return @n.sort({\$^a <=> \$^b})[2];
}
``````

JavaScript:

``````    var n = matrix.flat();
``````

Python:

``````  n = list(chain.from_iterable(matrix))
``````

I'm father fond of the variable-free* PostScript:

``````/sortedmatrix {
[ exch
{
} forall
] quicksort 2 get
} bind def
``````

* well, almost, my `quicksort` uses a lot of variables…

Task 2: Max Number

You are given a list of positive integers.

Write a script to concatenate the integers to form the highest possible value.

The naïve approach would be to try all possible orderings. Obviously that would get expensive very quickly.

In most situations one can simply stringify each number, reverse-sort in string order, and concatenate the results (because the highest numbers should always be in the leftmost positions in the output). This looks pretty easy, but there's a fiddly edge case (which is why example 5 exists): when two strings aren't of equal length, problems arise.

`56` should appear before `5` (565 > 556).

`55` can appear before or after `5`.

`54` should appear after `5` (554 > 545).

My solution is to extend each string by its final character until all strings are the same length - so the `5` above becomes `55` for string-ordering purposes.

Then I have the problem of wanting to sort the modified strings and use that order to reorder the original strings. I use something like a Schwartzian transform, except that in most languages I sort a list of indices rather than a composite variable.

Kotlin:

``````fun maxnumber(lst: List<Int>): Int {
``````

`po` is the input list, stringified.

``````    val po = lst.map {it.toString()}
``````

`pl` is the maximum length of any of those strings.

``````    val pl = po.map {it.length}.maxOrNull()!!
``````

`pm` will hold the extended strings.

``````    var pm = ArrayList<String>()
for (so in po) {
var sm = so
``````

If this one is shorter than the longest, extend it by its last character.

``````        if (so.length < pl) {
val c = so.lastOrNull()!!
for (_i in 1 .. pl - so.length) {
sm += c
}
}
}
``````

Generate the list of indices.

``````    var pi = ArrayList((0 .. pm.size - 1).toList())
``````

Sort it based on the contents of `pm`.

``````    pi.sortByDescending {pm[it]}
``````

Use it to read out `po` in the right order.

``````    var out = ""
for (st in pi) {
out += po[st]
}
``````

And convert back to integer.

``````    return out.toInt()
}
``````

In PostScript I ended up extending my sorting library to support multiple ways of indicating a custom sort order: you can now specify a comparator function, or a list of keys, or a function to generate keys.

Full code on github.