RogerBW's Blog

The Weekly Challenge 305: Binary Aliens 26 January 2025

I’ve been doing the Weekly Challenges. The latest involved an array mapping and a custom sort. (Note that this ends today.)

Task 1: Binary Prefix

You are given a binary array.

Write a script to return an array of booleans where the partial binary number up to that point is prime.

This is basically a slightly extended mapping. In Rust (which doesn't have built-in prime finding so I use my own code):

Integer square root. I don't really need this but it feels cleaner.

fn isqrt(s: u32) -> u32 {
    if s <= 1 {
        return s;
    }
    let mut x0 = s / 2;
    let mut x1 = (x0 + s / x0) / 2;
    while x1 < x0 {
        x0 = x1;
        x1 = (x0 + s / x0) / 2;
    }           
    return x0;
}

Primality tester by reasonably efficient trial division (2, 3, then 6n±1). Uses the isqrt function to establish an upper bound.

fn is_prime(n: u32) -> bool {
    if n == 1 {
        return false;
    }
    if n>2 && n%2==0 {
        return false;
    }
    if n>3 && n%3==0 {
        return false;
    }
    let lim = isqrt(n);
    let mut k6 = 0;
    loop {
        k6 += 6;
        for t in [k6 - 1,k6 + 1] {
            if t <= lim {
                if n % t == 0 {
                    return false;
                }
            } else {
                return true;
            }
        }
    }
}

The actual task is almost trivial: iterate over the input to generate the current value, test, output.

fn binaryprefix(a: Vec<u8>) -> Vec<bool> {
    let mut out = Vec::new();
    let mut n = 0;
    for x in a {
        n *= 2;
        if x == 1 {
            n += 1;
        }
        out.push(is_prime(n));
    }
    out
}

Task 2: Alien Dictionary

You are given a list of words and alien dictionary character order.

Write a script to sort lexicographically the given list of words based on the alien dictionary characters.

This basically needs a custom sort; my solutions to task 301 part 1 used something similar. I ended up using large integer encoding rather than a string mapping, because these are all fairly short strings and because it amused me; to do it "properly" I'd map each string with a lexicographic encoding. Anyway, Raku:

sub aliendictionary(@a, @dc) {

Get the maximum input string length.

  my $mxl = @a.map({$_.chars}).max;

Build the table of letter values (1 to 26; 0 is reserved for "no letter in this position").

  my %dh;
  for @dc.kv -> $i, $c {
    %dh{$c} = $i + 1;
  }
  my @b = @a;

Build the list of numeric encodings. Letter bu letter, add the letter's value (or 0 once we're off the end of the strhing).

  my %numerics;
  for @b -> $w {
    my $n = 0;
    my @cc = $w.comb;
    for 0 ..^ $mxl -> $i {
      $n *= 27;
      if ($i < $w.chars) {
        $n += %dh{@cc[$i]};
      }
    }
    %numerics{$w} = $n;
  }

Then sort using that encoding list.

  @b = @b.sort({ %numerics{$^a} <=> %numerics{$^b} });
  return @b;
}

Here's the "proper" version in Raku as a proof of concept. It builds the sort keys by sorting the given keys, then assigns them in order to a lexical map.

sub aliendictionary(@a, @dc) {
  my $mxl = @a.map({$_.chars}).max;
  my %dh;
  my @lx = @dc.sort;
  for @dc.kv -> $i, $c {
    %dh{$c} = @lx[$i];
  }
  my @b = @a;
  my %lex;
  for @b -> $w {
      %lex{$w} = $w.comb.map({%dh{$_}}).join("");
  }
  @b = @b.sort({ %lex{$^a} cmp %lex{$^b} });
  return @b;
}

Full code on github.

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