RogerBW's Blog

The Weekly Challenge 161: Words and More Words 21 April 2022

I’ve been doing the Weekly Challenges. The latest involved word selections and manipulations. (Note that this is open until 24 April 2022.)

Task 1:

An abecedarian word is a word whose letters are arranged in alphabetical order. For example, "knotty" is an abecedarian word, but "knots" is not. Output or return a list of all abecedarian words in the dictionary, sorted in decreasing order of length.

My polyglot approach means I need to find out how to read standard input (or a file) in each of the various languages. Javascript and PostScript can't manage line by line, but the rest can.

Then it's a matter of testing each word. Regular expressions would be the obvious way, but they're slow, even in Perl (here's a one-liner in bash, without the length sort).

grep ^$(echo {a..z}\*|sed -e 's/ //g')$

So instead I compare each pair of adjacent characters and break out of the loop if an earlier one has a higher numerical value than a later one. (There are no character sets in common use now that have gaps in the A-Z sequence…)

Perl:

my @b;

while (<>) {
  chomp;
  my @a=split '',$_;
  my $u=1;
  foreach my $i (0..$#a-1) {
    if ($a[$i] gt $a[$i+1]) {
      $u=0;
      last;
    }
  }
  if ($u) {
    push @b,$_;
  }
}

Then sort by length. Everything I use can take a custom comparison function for its built-in sort, except the Quicksort library I wrote for PostScript - so I'd better fix that.

print map {"$_\n"} sort {length($b) <=> length($a)} @b;

Python's pairwise only shows up in 3.10 (October 2021) so still isn't in Debian/stable.

I do not pry: a chin hit is not a dirty blow, bell or not.

Task 2:

A pangram is a sentence or phrase that uses every letter in the English alphabet at least once. For example, perhaps the most well known pangram is:

the quick brown fox jumps over the lazy dog

Using the provided dictionary, so that you don't need to include individual copy, generate at least one pangram.

Your pangram does not have to be a syntactically valid English sentence (doing so would require far more work, and a dictionary of nouns, verbs, adjectives, adverbs, and conjunctions). Also note that repeated letters, and even repeated words, are permitted.

I decided to be moderately efficient. Here's the Ruby:

Forward and reverse lookup tables:

f = {}
r = {}

Each word gets a hash value to indicate which letters it contains: if it has an a, we set bit 0, b, bit 1, etc. (We store only the shortest word for each hash value, so "erase" would be used rather than "erasers".)

while line = gets
  line = line.chomp
  b = 1
  v = 0
  "a".upto("z") do |l|
    if !line.index(l).nil? then
      v |= b
    end
    b <<= 1
  end
  if !r.has_key?(v) || r[v].length > line.length then
    f[line]=v
    r[v]=line
  end
end

The final output will have at least one of each letter – i.e. the combined hash value will be (1) × 26. It starts at zero.

w = []
lt = (1 << 26) - 1
lu = [0]
while (lu[-1] != lt) do
  wn = ""

If we've already got one or more words, we look through the list.

  if (w.length > 0) then
    mode = 0
    sc = []
    r.each_key do |wv|

For each candidate word, if it has no letters in common with what's already in the output, that's a first class candidate. (Delete the list of second-class candidates, if any, and don't add any more.)

      if (wv & lu[-1]) == 0 then
        if mode == 0 then
          mode = 1
          sc = []
        end
        sc.push(r[wv])

Otherwise, if it has at least one letter that we don't already have, it's a second class candidate.

      elsif mode == 0 && (wv | lu[-1]) != lu[-1] then
        sc.push(r[wv])
      end
    end

If we have any candidates, pick one at random – otherwise back out of the last word and try a different one.

    if sc.length == 0 then
      w.pop
      lu.pop
    else
      wn = sc[rand(sc.length)]
    end
  else

If we don't already have one or more words, just pick one at random.

    sc = f.keys
    wn = sc[rand(sc.length)]
  end

If we have a new word, then update both the list and the current hash value.

  if wn != "" then
    w.push(wn)
    lu.push(lu[-1] | f[wn])
  end
end

print(w.join(" ") + "\n")

Ideally then I'd go back over the list, to see if any of the earlier words could be removed without compromising the pangram, but I found myself feeling lazy. Other suggestions were to generate pangrams of only abecedarian words (just feed it the output from task 1 in place of a dictionary file), produce the shortest possible pangram or pangrams where each word would add just one new letter (which could be done with this code, but would need bit-counting), etc.

This was fairly easy to write in various languages, though I was bitten by Raku and its special bitwise operators, different from any other language. (All right, Lua uses "or" rather than "|", but if I use "|" in Lua it doesn't accept it without question and then do the wrong things with it. Apparently "|" on its own "creates an any Junction from its arguments" which seems to be a thing Raku enthusiasts want to do.)

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 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 essen 2024 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