RogerBW's Blog

The Weekly Challenge 214: A Rank Collection 30 April 2023

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.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1