By Shahed Nooshmand

Perl Weekly Challenge: week 58

Here goes:

Task #1

Compare two given version number strings v1 and v2 such that:

The version numbers are non-empty strings containing only digits, and the dot (“.”) and underscore (“_”) characters. (“_” denotes an alpha/development version, and has a lower precedence than a dot, “.”).

Raku has a type Version, and it even supports comparing via <=>, but v1.2_5 is the same as v1.2._.5 and so v1.2 > v1.2_5. It’s not hard to work around this, and I’ve come up with a gross solution on GitHub.

Task #2

Write a script to arrange people in a lineup according to how many taller people are in front of each person in line. You are given two arrays. @H is a list of unique heights, in any order. @T is a list of how many taller people are to be put in front of the corresponding person in @H. The output is the final ordering of people’s heights, or an error if there is no solution.

If we go from tallest to shortest, and each time insert the person who’s supposed to be behind t people at index t (starting with 0, of course) we have our answer.

This works because we know the new person is shorter than everyone already in the line, so obviously there are t people taller than the person whose postition is t.

The only time we don’t get an answer is when there aren’t enough people to place in front of someone; the language can actually take care of this for us.

So, assuming we know @H and @T, this is the solution:

#!/usr/bin/env raku

my @Q;
@Q.splice: .value, 0, .key for (@H Z=> @T).sort: -*.key;
print @Q;

Which gives the answer for that 1000-person list in about 1 second.

Now, let’s assume this is the input:

@H = 1;
@T = 1;

Absurdly, this input is asking for someone taller than Sultan Kösen, which causes splice to fail due to an out-of-range offset argument, and so the interpreter throws us an error.