RogerBW's Blog

The Weekly Challenge 319: Minimum Words Count Even If They Are Common 04 May 2025

I’ve been doing the Weekly Challenges. The latest involved REPLACEME_DESC. (Note that this ends today.)

Task 1: Word Count

You are given a list of words containing alphabetic characters only.

Write a script to return the count of words either starting with a vowel or ending with a vowel.

In my Perl-first days I'd have used regular expressions, and in some languages that's still the best option, but my current polyglot mindset has helped me see the strengths of different languages. For example in Crystal I can use nested cases:

def wordcount(a)
  ct = 0
  a.each do |w|
    ct += case w[0]
          when  'a', 'e', 'i', 'o', 'u'
            1
          else
            case w[-1]
            when  'a', 'e', 'i', 'o', 'u'
              1
            else
              0
            end
          end
  end
  ct
end

while PostScript gets string searches:

/wordcount {
    0 dict begin
    [ exch
      {
          dup
          0 1 getinterval (aeiou) exch search {
              pop pop pop pop 1
          } {
              pop
              dup length 1 sub 1 getinterval (aeiou) exch search {
                  pop pop pop 1
              } {
                  pop 0
              } ifelse
          } ifelse
      } forall
    ] { add } reduce
    end
} bind def

and Perl sticks with regexps:

sub wordcount($a) {
  my $ct = 0;
  foreach my $w (@{$a}) {
    if ($w =~ /^[aeiou]/ || $w =~ /[aeiou]$/) {
      $ct++;
    }
  }
  $ct;
}

Task 2: Minimum Common

You are given two arrays of integers.

Write a script to return the minimum integer common to both arrays. If none found return -1.

I treat this as a set intersection. In Raku:

sub minimumcommon(@a, @b) {
    my %aa = Set.new(@a);
    my %bb = Set.new(@b);
    my %cc = %aa (&) %bb;
    if (%cc.elems > 0) {
        %cc.keys.min;
    } else {
        -1;
    }
}

And I'm introducing a new language to these things, at least for the moment: Typst, which is not really designed as a general-purpose programming language at all; it's a layout engine, in the manner of TeX or Troff. But I've been using it for a lot of document production recently, any sufficiently complicated document production system is in effect a programming language even if the designers try to hide it (which in this case they don't), and I thought that the best way to get familiar with its programming capabilities would be to add it to the set of languages I use here.

It doesn't have a set type, so I need a converter from array to "set" (as in Perl, Lua, PostScript and others, a hash/dict/map with every value locked to true does double duty for me). Set keys here have to be strings, but we do at least have a working map and filter.

#let arr2set(a) = {
  a.map(x => (str(x), true)).to-dict()
}

Then the actual code is pretty much the same as the above.

#let minimumcommon(a, b) = {
  let aa = arr2set(a)
  let bb = arr2set(b)
  let cc = aa.keys().filter(x => x in bb).map(x => int(x))
  if cc.len() == 0 {
    -1
  } else {
    calc.min(.. cc)
  }
}

Full code on github.


  1. Posted by John Poole at 10:54am on 05 May 2025

    "A,e,i,o,u" and sometimes "y".

    your sly

    How do you handle the above two words?

  2. Posted by RogerBW at 12:10pm on 05 May 2025

    I deal with this problem by ignoring it. :)

    This isn't "starts or ends with a vowel sound", or I'd have to exclude "have". So if I met this in the real world I'd ask what the goal of the test was, and then build an algorithm based on that.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

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