I’ve been doing the Weekly
Challenges. The
latest
involved score ranking and sequence manipulation. (Note that this is
open until 30 April 2023.)
Task 1: Rank Score
You are given a list of scores (>=1).
Write a script to rank each score in descending order. First three
will get medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will
just get the ranking number.
Using the standard model of giving equal scores equal rank, then
advancing that number of ranks.
I extended this slightly to indicate a tie with "="; thus the sequence
[4, 4, 2]
maps to ['G=', 'G=', 'B']
. Drop out the relevant condition
check if you want to enforce strict compliance with the rules.
(And for the benefit of languages that have types, all output is in
string form.)
This goes in three stages (Raku):
sub rankscore(@a) {
Build a hash mapping actual score to the number of occurrences of that
score.
my %av;
for @a -> $s {
%av{$s}++;
}
my $rank = 1;
my %tab;
For each score in descending order, allocate it a rank string. Advance
the running total of rank by the number of occurrences.
for (sort {$^b <=> $^a}, %av.keys) -> $k {
my $siz = %av{$k};
my $r;
if ($rank <= 3) {
$r = ['G', 'S', 'B'][$rank - 1];
} else {
$r = '' ~ $rank;
}
if ($siz > 1) {
$r ~= "=";
}
%tab{$k} = $r;
$rank += $siz;
}
Finally, return a list of the original scores mapped to their rank strings.
return [map {%tab{$_}}, @a];
}
Task 2: Collect Points
You are given a list of numbers.
You will perform a series of removal operations. For each operation,
you remove from the list N (one or more) equal and consecutive
numbers, and add to your score N × N.
Determine the maximum possible score.
This isn't as simple as just removing the longest continuous run,
because taking one run out from between two others with matching keys
causes them to merge together and score more. (I suspected this might
be the case, and submitted test case #4 to prove it.) Therefore I run
an exhaustive search, depth-first so that I can use standard arrays
rather than double-ended queues, over possible removal orders.
I found it interesting that some languages (Kotlin, Ruby, Python) have
a remove
operation that's essentially "delete the first matching
value from the array", rather than the value at a specific index.
If I'm searching through an array for something, I'll generally have
converted it into a hash already, though that may be a Perlism that I
should unlearn for other languages.
Attitudes towards the making of deep copies varied:
-
Perl, Python and Rust have routines built in as standard or in
readily-available libraries.
-
Raku has one built in as standard but actually getting it to work
required some combination of deepmap
and clone
(which itself
only produces a shallow copy) that I couldn't get to work, nor could
I find anyone else who had, so I gave up and wrote something that
understood the specific data structure.
-
JavaScript apparently has one in the latest versions but here it's
easiest to convert to JSON and back. Ruby similarly gets a
conversion to serialised format.
-
Looking for one for Kotlin, I repeatedly found pages that said that
even posing the question was a stupid thing to do and it was
intrinsically impossible to write a generic deep copier; instead I
should define a clone
method for the class I was building. (I'm
not building a class.) Wrote a custom one as with Raku.
-
I wrote generic ones for Lua and PostScript.
I'll use the Python code to show off the algorithm:
from copy import deepcopy
def collectpoints(a):
First, convert the list to a set of tuples of (m, n)
where m
is
the key and n
is the number of occurrences in that series. (Not
always tuples in programming language terms; for example the elements
of a tuple in Python are required to be invariant, so I used arrays.)
Therefore I'll be removing just one element from this modified list at
a time.
m = []
st = 0
while st < len(a):
k = a[st]
e = st
while e < len(a) and a[e] == k:
e += 1
m.append([k, e - st])
st = e
Set a maximum value and build a stack.
rv = 0
stack = [[m, 0]]
Standard depth-first search scaffolding.
while len(stack) > 0:
s = stack.pop()
If there's nothing left in the list, log the highest value achieved.
if len(s[0]) == 0:
rv = max(rv, s[1])
else:
For each entry in the list, remove one element (from a new deep copy)
and add its value to the score.
for i in range(len(s[0])):
ss = deepcopy(s)
ss[1] += ss[0][i][1] * ss[0][i][1]
del ss[0][i]
Then check for adjacent equal elements, which collapse into each other.
j = i
while True:
if j > 0 and j < len(ss[0]) and ss[0][j][0] == ss[0][j - 1][0]:
ss[0][j][1] += ss[0][j - 1][1]
del ss[0][j - 1]
j -= 1
else:
break
Put that new list on the stack, and continue.
stack.append(ss)
return rv
Full code on
github.
Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.