That’s Some Operator You Got There

Wherein we do what we must, and do not question why…

THE WEEKLY CHALLENGE – PERL & RAKU #163 Task 1


I’ve given him more mixed signals than a dyslexic Morse code operator.

— Rachel Cohn


Sum Bitwise Operator

Submitted by: Mohammad S Anwar

You are given list positive numbers, @n.

Write script to calculate the sum of bitwise & operator for all unique pairs.

Example 1
Input: @n = (1, 2, 3)
Output: 3

Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 =>  3.
Example 2
Input: @n = (2, 3, 4)
Output: 2

Since (2 & 3) + (2 & 4) + (3 & 4) => 2 + 0 + 0 =>  2.

opening commentary

Ahh, the old bitwise-AND n-choose-2! I was wondering when you would show your face again around here again!

I’m frankly a little surprised after what happened last time.

Things, if I recall correctly, did not go well for you? No they did not.

Well, you’ll need to watch your back is all I’ll say. Frank there, over in the corner, he’s been drinking all day — and the twins have been hollarin’ up a storm about God-knows what.

I don’t care anymore. I just want to let you know I don’t want no trouble, and don’t want to see no trouble, so that’s exactly what I’m going to do. You hear me right?

Good. I’m glad we had this talk.

Analysis

I feel I might need to do some research and try and figure out if this particular process has any practical use. Nothing immediately comes to mind, but that doesn’t mean I haven’t missed anything. I enjoy finding things I don’t know in general. Perhaps this is one of those things. Assuming there’s something to know.

The operation specified is akin to the cartesian-product of a set with itself, or the dot product in a way, with more parts, or perhaps, well… who am I fooling? I have no idea. What we are doing, though, is to collect all 2-combinations from the set of list elements and apply a bitwise AND operation to each pair, and then sum the resulting values. If there’s a name for this type of operation I don’t seem to know it.

METHOD

We can obtain a set of unique pairings between items is a set, excluding any identity pairings, by using two indexes in two loops, with the second index keyed one-advanced from the first. The interior of this structure will have both indexes available and we can examine the list at these positions and apply the bitwise operation. The result is added to an accumulator which is ultimately returned. Seems like a plan.

PERL 5 SOLUTION

We can obtain all combinations from the input list by using two index variables, from 0 to the items before the end of the list, and a second interior loop from one beyond the first variable to the last element. We can then use these indices to obtain pairs of values which we apply our operation to, adding the result to an accumulating sum.

use warnings;
use strict;
use utf8;
use feature ":5.26";
use feature qw(signatures);
no warnings 'experimental::signatures';

use constant {TEST => 1};

@ARGV == 0 
? say "please input a space-separated list of values at command line\n" 
: say and_sum( @ARGV );


sub and_sum (@list) {
    my $sum;
    for my $i (0..$#list-1) {
        for my $j ($i+1..$#list) {
            $sum += $list[$i] & $list[$j];
        }
    }
    return $sum;
}

Raku Solution

Raku is full of implicit iterators, and is designed around the idea of using them. As such between combinations, a map function and a list-reduction metaoperator we’ve eliminated all explicit loops from our code, leaving a single dense line.

We could even inline that too, but we’ll keep it as a subroutine for testing purposes.

In short: we take the list, create a list of combination pairs, then map each pair to a reduction using the bitwise operator +&. Then we sum the reduced values.

unit sub MAIN ( *@list ) ;

put and_sum( @list ) and exit if @list.elems > 0;

sub and_sum (*@list) {    
    @list.combinations(2).map({ [+&] |$_ }).sum
}


The Perl Weekly Challenge, that idyllic glade wherein we stumble upon the holes for these sweet descents, is now known as

The Weekly Challenge – Perl and Raku

It is the creation of the lovely Mohammad Sajid Anwar and a veritable swarm of contributors from all over the world, who gather, as might be expected, weekly online to solve puzzles. Everyone is encouraged to visit, learn and contribute at

https://theweeklychallenge.org

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s