RogerBW's Blog

The Weekly Challenge 216: Word Registration 14 May 2023

I’ve been doing the Weekly Challenges. The latest involved mucking about with words. (Note that this is open until 14 May 2023.)

Task 1: Registration Number

You are given a list of words and a random registration number.

Write a script to find all the words in the given list that has every letter in the given registration number.

This is a fiddly definition. As I read it: for each word, if every letter from the registration is found in the word, add it to the output. It doesn't matter whether the candidate word has other letters that aren't in the registration. In Ruby:

Helper function: given a word, lower-case it, and return a set of all the letters present in it.

def word2set(word)
  return {|i| i >= 'a' && i <= 'z'})

Main function:

def registrationnumber(words, reg)
  out = []
  words.each do |w|

Build the set of letters in the registration. (In some languages I built this once and cloned the set for each pass.)

    ss = word2set(reg)

Use the same helper function to get a list of candidate letters. For each of them,

    word2set(w).each do |char|

If the letter is in the set,

      if ss.include?(char) then

delete it from the set,


and if the set is empty

        if ss.length == 0 then

note the "matching" word and go on to the next one.

  return out

Task 2: Word Stickers

You are given a list of word stickers and a target word.

Write a script to find out how many word stickers is needed to make up the given target word.

Each "sticker" is a set of letters which can be used individually, and there's an unlimited supply of each one. To me the obvious data structure is a hash with keys as the letters and values as the count of available letters. This can of course be done with a standard hash, but where a specialised class was readily available I used it; in Raku there's the BagHash, in Python the Counter, and the latter inspired a similar crate for rust.

In Perl, though:

sub wordstickers($stickers, $word) {

Set up the hash of letters and counts in the target word. (Quite similar to building the set in task 1.)

  my %w;
  map {$w{$_}++} split '',lc($word);

Also make a copy of this to check whether the target can be made at all.

  my $t = dclone(\%w);
  my @stick;

For each sticker,

  foreach my $s (@{$stickers}) {

Set up its hash of letters and counts.

    my %f;
    map {$f{$_}++} split '',lc($s);

Also delete from t the letters present in the sticker.

    map {delete $t->{$_}} keys %f;

Push it onto the stickers list.

    push @stick, \%f;

If there are any letters left in t they were in the target but not on any sticker, so we can't make the target.

  if (scalar %{$t}) {
    return 0;

Set up a breadth-first search (because we want the result with lowest depth).

  my @stack = ([\%w, 0]);

For each entry on the stack:

  while (scalar @stack > 0) {
    my $st = shift @stack;

If there are no letters remaining to be found, return the result.

    if (scalar %{$st->[0]} == 0) {
      return $st->[1];
    } else {

New depth is one more than the old depth.

      my $n = $st->[1] + 1;

Try each sticker.

      foreach my $sti (@stick) {

Clone the list of remaining letters.

        my $sp = dclone($st->[0]);
        my $v = 0;

For each letter on the sticker,

        foreach my $l (keys %{$sti}) {

If it's also in the remaining letters,

          if (exists $sp->{$l}) {

This is an interesting sticker.

            $v = 1;

Find out how many of this letter will be left in the word after applying the sticker.

            my $p = $sp->{$l} - $sti->{$l};

If there will be any,

            if ($p > 0) {

store that value in the list of remaining letters,

              $sp->{$l} = $p;
            } else {

otherwise delete that letter from the list.

              delete $sp->{$l};

If it was an interesting sticker, i.e. it removed any letters at all, push it onto the stack for further evaluation.

        if ($v) {
          push @stack, [$sp, $n];

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.

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 crystal 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 2021 hugo 2022 hugo 2023 hugo 2024 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