RogerBW's Blog

The Weekly Challenge 193: Odd Binary 04 December 2022

I’ve been doing the Weekly Challenges. The latest involved a binary expansion and string analysis. (Note that this closes today.)

Task 1: Binary String

You are given an integer, $n > 0.

Write a script to find all possible binary numbers of size $n.

So that's everything from zero to one less than 1 << n. Easy enough.


sub binarystring($n) {
  my @o;
  foreach my $a (0..(1 << $n)-1) {
    push @o,sprintf('%0'.$n.'b', $a);
  return \@o;

…I mean, I could have used a recursive incremental approach to build up the string character by character, but this just seemed cleaner. Lots of variation in how you get a binary number string, of course.

Task 2: Odd String

You are given a list of strings of same length, @s.

Write a script to find the odd string in the given list. Use positional value of alphabet starting with 0, i.e. a = 0, b = 1, ... z = 25.

Find the difference array for each string as shown in the example. Then pick the odd one out.

In other words, "abc" and "fgh" would both become [1, 1], but "fec" would be [-1, -2]

I end up doing this just one character at a time and bailing out when we reach a singleton.


fun oddstring(ss: List<String>): String {

We know each string is the same length, so the outer iteration is by character index.

    for (i in[0].length-2) {

Set up the table of differences.

        var tab = mutableMapOf<Int, ArrayList<String>>()

For each string,

        for (s in ss) {

work out the difference in code between character i and the next,

            val v = s[i+1].code - s[i].code

and store the string in the hash indexed by the difference.

            var ll = tab.getOrDefault(v, ArrayList<String>())
            tab.put(v, ll)

Then for each hash value (list of strings),

        for (j in tab.values) {

if there's only one string in that list (for this difference value),

            if (j.size == 1) {

it's the answer, so return it.

                return j[0]

Otherwise go on to the next character. If we never got a match, return an empty string.

    return ""

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.

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