RogerBW's Blog

The Weekly Challenge 209: Special Merge 26 March 2023

I’ve been doing the Weekly Challenges. The latest involved decoding a sequence and merging lists. (Note that this closes for public solutions today.)

Task 1: Special Bit Characters

You are given an array of binary bits that ends with 0.

Valid sequences in the bit string are:

[0] -decodes-to-> "a"
[1, 0] -> "b"
[1, 1] -> "c"

Write a script to print 1 if the last character is an “a” otherwise print 0.

One might take this backwards until one reached an unambiguous start character, but it seemed easier to start from the beginning. Python:

def specialbitcharacters(a):

Set up a latch showing where we are in the decoding process.

  s = 0
  for v in a:

If the last start bit was a 1, we don't care what this bit is, but it's part of that character.

    if s == 1:
      s = 2

Otherwise this is a start bit, so set the latch to its value.

    else:
      s = v

At the end, the latch is zero if and only if the last bit was both zero and a start bit.

  return s == 0

Task 2: Merge Account

You are given an array of accounts i.e. name with list of email addresses.

Write a script to merge the accounts where possible. The accounts can only be merged if they have at least one email address in common.

This suddenly lacked fun for me, so I only did it in Perl, Raku and PostScript. Each entry in the list consists of a list of which the first entry is an account name (not necessarily unique) and the rest are addresses. Perl:

sub mergeaccount($a) {
  my @aname;
  my $id = 0;
  my %r;

Looking through the list, build up a mapping of account ID (row number) to name, and each email address to a list of account IDs.

  foreach my $acc (@{$a}) {
    push @aname, $acc->[0];
    foreach my $i (1..$#{$acc}) {
      push @{$r{$acc->[$i]}}, $id;
    }
    $id++;
  }

Now a mapping of accounts to other accounts. If an address occurs in account IDs x and y, map y to x.

  my %m;
  foreach my $idlist (values %r) {
    if (scalar @{$idlist} > 1) {
      my $root = $idlist->[0];
      while (exists $m{$root}) {
        $root = $m{$root};
      }
      foreach my $i (1..$#{$idlist}) {
        $m{$idlist->[$i]} = $root;
      }
    }
  }

Actually merge the accounts. For each ID, find out which final account its addresses should go into, and put them there. Also save the first account name as a prefix.

  my %staging;
  my %prefix;
  foreach my $id (0..$#{$a}) {
    my $ii = $id;
    while (exists $m{$ii}) {
      $ii = $m{$ii};
    }
    my $acc = $a->[$id];
    if (!exists $prefix{$ii}) {
      $prefix{$ii} = $acc->[0];
    }
    foreach my $addr (map {$acc->[$_]} 1..$#{$acc}) {
      $staging{$ii}{$addr} = 1;
    }
  }

Finally assemble the results (hash keys to array entries)

  my @out;
  foreach my $k (keys %staging) {
    push @out,[$prefix{$k}, sort keys %{$staging{$k}}];
  }

and sort them (order unspecified, but account name and then first address seems to match the examples).

  @out = sort {$::a->[0] cmp $::b->[0] ||
                 $::a->[1] cmp $::b->[1]} @out;
  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 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 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