RogerBW's Blog

The Weekly Challenge 336: In the Final Reckoning, We're All Equal 31 August 2025

I’ve been doing the Weekly Challenges. The latest involved list partitioning and a progressive score calculator. (Note that this ends today.)

Task 1: Equal Group

You are given an array of integers.

Write a script to return true if the given array can be divided into one or more groups: each group must be of the same size as the others, with at least two members, and with all members having the same value.

What I want to work with here is the count of each number; the values of the numbers themselves don't matter. In Perl:

sub equalgroup($a) {

Use a hash to build up the counts.

  my %s;
  map {$s{$_}++} @{$a};

Then take the counts themselves, remove any duplicates, and sort them. (I don't actually need to sort them, only to find the minimum, but in some languages it's easier to do that and the deduplication with a sort first.)

  my @v = sort {$::a <=> $::b} uniq values %s;

A naïve reading of the question would suggest that all these values should have to be the same, but I can have multiple groups with the same numbers in them; in other words, a group of 4 can be regarded as two groups of 2 (as in example 1). So I'm looking for a group size which is at least 2, and by which all my counts can be evenly divided.

The smallest group must be at least 2, or it won't work at all.

  my $l = $v[0];
  if ($l < 2) {
    return 0;
  }

Otherwise I'll check each possible group size, from 2 up to the size of the smallest group.

  foreach my $t (2 .. $l) {

If all the groups can be evenly divided by it, we have a solution.

    if (all {$_ % $t == 0} @v) {
      return 1;
    }
  }

If we tried all the possibilities and none of them worked, we don't.

  0;
}

Task 2: Final Score

You are given an array of scores by a team.

Write a script to find the total score of the given team. The score can be any integer, +, C or D. The + adds the sum of previous two scores. The score C invalidates the previous score. The score D will double the previous score.

This is a stack-based problem, so I will indulge myself and show the PostScript solution.

/finalscore {

Push a start-array marker below the input list.

    [ exch

Iterate over the input list.

      {

We don't have a switch-case type statement in PostScript, and I want a default, so it's nested ifs.

          /n exch def

It's a C: pop the last score. (deepeq is a library function I wrote because strings in PostScript are in many ways arrays of characters, and so two strings can have different pointer values (which eq would compare) but identical content.)

          n (C) deepeq {
              pop
          } {

It's a "D": copy the last score and double it.

              n (D) deepeq {
                  dup 2 mul
              } {

It's a "+": copy the last two scores and add them together.

                  n (+) deepeq {
                      2 copy add
                  } {

Otherwise, it's a number; convert it to an integer.

                      n cvi
                  } ifelse
              } ifelse
          } ifelse
      } forall

There's no explicit stack pushing for any of this, because if I leave a number sitting there it gets pushed automatically. Finally, close off the array, and adds its values. (reduce is another of my library functions.)

    ] { add } reduce
} bind def

Full code on github.


  1. Posted by RogerBW at 12:13pm on 01 September 2025

    Looking at other blogs:

    (Task 1)

    Arne Sommer and Packy Anderson point out that Raku's Bag does a similar job to the Counter crate for Rust, and I really ought to remember to use it.

    Arne and Thomas Köhler added a primality check for the candidate sizes, but frankly at this scale of problem it doesn't seem worth it.

    Several players used GCD to collapse the group sizes into the highest common factor. Wish I'd thought of that!

    (Task 2)

    Arne checks for the integer first rather than feeding any non-matching character into the string-to-int converter. That's probably neater.

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 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech bayern 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 crystal cthulhu eternal cycling dead of winter disaster 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 essen 2024 essen 2025 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 horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 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 openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review 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 typst 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