RogerBW's Blog

The Weekly Challenge 234: Triplets with Character 17 September 2023

I’ve been doing the Weekly Challenges. The latest involved messing about with lists. (Note that this ends today.)

Task 1: Common Characters

You are given an array of words made up of alphabetic characters only.

Write a script to return all alphabetic characters that show up in all words including duplicates.

This fell for me into three major steps. In Perl:

sub commoncharacters ($a) {

Step 1: make a list ac of hashes of character counts.

  my @ac;
  foreach my $w (@{$a}) {
    my %h;
    foreach my $c (split '', $w) {
      $h{$c}++;
    }
    push @ac, \%h;
  }

Step 2: make a single hash vc of minimum character counts (no key if the character is not present in all words, otherwise the lowest number of times it occurs).

  my %vc = %{$ac[0]};
  foreach my $aa (@ac) {
    foreach my $c (keys %vc) {
      if (exists $aa->{$c}) {
        $vc{$c} = min($vc{$c}, $aa->{$c});
      } else {
        delete $vc{$c};
      }
    }
  }

Step 3: for each character in the (arbitrarily chosen) first input word, output it if it's present in vc and decrement its count there, deleting the key once we've run out of that character to simplify the testing next time it comes up (e.g. the second "a" in "java").

  my @out;
  foreach my $c (split '', $a->[0]) {
    if (exists $vc{$c}) {
      push @out, $c;
      $vc{$c}--;
      if ($vc{$c} == 0) {
        delete $vc{$c};
      }
    }
  }
  return \@out;
}

In Rust I could use counter::Counter which has an overloaded & to produce a minimum-count result for step 2, so the whole thing becomes more compact.

fn commoncharacters(a: Vec<&str>) -> Vec<char> {
    let ac =
        a.iter().map(|w| w.chars().collect::<Counter<_>>()).collect::<Vec<_>>();
    let mut vc = ac[0].clone();
    for tc in ac.iter().skip(1) {
        vc = vc & tc.clone();
    }
    let mut out = Vec::new();
    for c in a[0].chars() {
        if vc.contains_key(&c) {
            out.push(c);
            vc.subtract(c.to_string().chars());
        }
    }
    out
}

Task 2: Unequal Triplets

You are given an array of positive integers.

Write a script to find the number of triplets (i, j, k) that satisfies num[i] != num[j], num[j] != num[k] and num[k] != num[i].

I decided to have some fun with this one. The naïve way would be to generate all valid combinations of (i, j, k) and check for inequality. But there's a better way. In Python:

def unequaltriplets(a):

Make a hash of numbers and their counts.

  utc = defaultdict(lambda: 0)
  for n in a:
    utc[n] += 1

Make a list of distinct numbers, and bail out if there will be no triplets.

  kl = list(utc.keys())
  if len(kl) < 3:
    return 0

Start the count.

  out = 0

For each triplet of distinct numbers:

  for c in combinations(kl, 3):

Increment the count by the product of the number of times each number occurs, which is the total number of triplets that can be formed. So if I had an input of [1, 1, 2, 2, 3] my hash of counts would be {1 => 2, 2 => 2, 3 => 1} and I'd increment by 2×2×1 = 4.

    out += utc[c[0]] * utc[c[1]] * utc[c[2]]
  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