RogerBW's Blog

The Weekly Challenge 233: Similar Frequency 10 September 2023

I’ve been doing the Weekly Challenges. The latest involved reducing lists in various ways. (Note that this ends today. And 232 was cancelled as the administrator was unwell)

Task 1: Similar Words

You are given an array of words made up of alphabets only.

Write a script to find the number of pairs of similar words. Two words are similar if they consist of the same characters.

It turns out from the examples that "they consist of the same characters" means "the set of characters in the words is the same, ignoring count and order" - so "abc" and "cabcb" are similar by this definition.

Which means I will solve this with a custom hasher.

sub similarwords($a) {
  my %ct;
  my $ac = ord('a');

For each word:

  foreach my $w (@{$a}) {

Make a set out of the letters (i.e. one key per letter, ignore the count).

    my %hs = map {$_ => 1} split '', $w;

Then set up a bitmask,

    my $mask = 0;

and encode based on ASCII value offset from a. So a produces 1, b 2, c 4, etc.

    foreach my $c (keys %hs) {
      $mask |= 1 << (ord($c) - $ac);
    }

Increment the counter for that mask.

    $ct{$mask}++;
  }

Now we just need to read off the mask values.

  my $pairs = 0;
  foreach my $cv (values %ct) {
    if ($cv > 1) {

No need to count the actual pairs; if there are 3 values, you can make 3 pairs out of them. 4 values, 6 pairs. 5 values, 10 pairs. Etc.

      $pairs += $cv * ($cv - 1) / 2;
    }
  }
  return $pairs;
}

Other languages are basically similar.

Task 2: Frequency Sort

You are given an array of integers.

Write a script to sort the given array in increasing order based on the frequency of the values. If multiple values have the same frequency then sort them in decreasing order.

I've generally been solving these in Rust first, then porting to other languages. But Rust's Counter crate has a handy feature which I didn't immediately find in the other languages…

fn frequencysort(a: Vec<i32>) -> Vec<i32> {
    let mut ct = a.into_iter().collect::<Counter<i32>>().most_common_ordered();

Well, there's half my work done, thanks very much! (Given how rich Raku's collection types are, I was surprised not to find anything like this in its Bag functions, but apparently not…)

    ct.reverse();
    let mut out = Vec::new();
    for (k, v) in ct.iter() {
        out.append(&mut vec![*k; *v]);
    }
    out
}

And there's the other half. But let's also look at the code for other languages, specifically Python.

def frequencysort(a):

Put the number counts into ct.

  ct = defaultdict(lambda: 0)
  for x in a:
    ct[x] += 1

Build a "reversed ct", where the keys are the counts and the values are lists of the mumbers with that count.

  rct = defaultdict(lambda: [])
  for k, v in ct.items():
    rct[v].append(k)

Then to build the output, and this is more or less as in the Rust code:

  out = []

Iterate low to high counts:

  for k in sorted(rct):

Then iterate high to low numbers with that count:

    for v in reversed(sorted(rct[k])):

Then push on the correct count of that number:

      for i in range(k):
        out.append(v)
  return out

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