RogerBW's Blog

The Weekly Challenge 192: Equal Flips For All 27 November 2022

I’ve been doing the Weekly Challenges. The latest involved a binary negation and list division. (Note that this is open until 27 November 2022.)

Task 1: Binary Flip

You are given a positive integer, $n.

Write a script to find the binary flip.

This turns out to be: take the binary representation (with no leading zeroes), then exchange all 0s and 1s to get an inverse value.

This is clearly intended to be a problem about binary representations. But we don't need to do that. Instead, I find the value of the leftmost set bit in the input by counting repeated shift rights until there's nothing left – e.g. 5 is processed as 101, 10, 1, 0, so three shifts. (I.e. log base 2.) Then shift 1 left that many times (getting 8). Subtract 1: 7, which gives me what in binary would be a series of "1" of the same length as the original number. Then subtract the input from that (gosh, an actual legitimate "take away the number you first thought of").

This sequence is in the OEIS and the graph makes this approach particularly obvious.

Some languages have access to CPU functions to count leading zeroes and thus get a binary magnitude that way, but I did this the same way throughout. JavaScript:

function binaryflip(n) {
    let m = n;
    let r = 0;
    while (m > 0) {
        m >>= 1;
        r += 1;
    }
    return (1 << r) - 1 - n;
}

Task 2: Equal Distribution

You are given a list of integers greater than or equal to zero, @list.

Write a script to distribute the number so that each members are same. If you succeed then print the total moves otherwise print -1.

"Distribute" turns out to mean "subtract one from a list value, adding it to another adjacent list value". So this has to be done iteratively [0 3 0][1 2 0][1 1 1] = 2 steps , adding and subtracting 1 each time. If a number is higher than both its neighbours, the process is not immediately obvious, but this one seemed to work.

Raku:

sub equaldistribution(@list) {

Check that an even distribution is possible.

    my $s = @list.sum;
    if ($s % @list.elems != 0) {
        return -1;
    }

Get a target value for each element.

    my $m = $s div @list.elems;
    my $out = 0;
    my @w = @list;
    while (True) {
        for (0..@w.elems-2) -> $i {

Going through the list, if this value is higher than it needs to be, move any surplus into the next element.

            if (@w[$i] > $m) {
                my $v = @w[$i] - $m;
                @w[$i+1] += $v;
                $out += $v;
                @w[$i] = $m;

If it's lower, take as much as possible from the next element (without letting it go below zero.)

            } elsif (@w[$i] < $m) {
                my $v = min($m - @w[$i], @w[$i+1]);
                @w[$i+1] -= $v;
                $out += $v;
                @w[$i] += $v;
            }
        }

If the job's done, exit, otherwise loop again.

        if (@w.all == $m) {
            last;
        }
    }
    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