site.base_url }}/">The Bear's Den -->

The Bear's Den

Enter at your own risk

Sum Even Bits

Task 1: Count Even Digits Number

Submitted by: Mohammad Sajid Anwar


You are given a array of positive integers, @ints.

Write a script to find out how many integers have even number of digits.

Example 1

Input: @ints = (10, 1, 111, 24, 1000)
Output: 3

There are 3 integers having even digits i.e. 10, 24 and 1000.

Example 2

Input: @ints = (111, 1, 11111)
Output: 0

Example 3

Input: @ints = (2, 8, 1024, 256)
Output: 1

Solution

Convert the number into base B representation and grep for those having an even number of digits.

use Math::Prime::Util 'todigits';

sub cedn {
    my $base = shift;
    scalar grep !(scalar(todigits($_, $base)) % 2), @_
}

See the full solution.

Task 2: Sum of Values

Submitted by: Mohammad Sajid Anwar


You are given an array of integers, @int and an integer $k.

Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set.

Example 1

Input: @ints = (2, 5, 9, 11, 3), $k = 1
Output: 17

Binary representation of index 0 = 0
Binary representation of index 1 = 1
Binary representation of index 2 = 10
Binary representation of index 3 = 11
Binary representation of index 4 = 100

So the indices 1, 2 and 4 have total one 1-bit sets.
Therefore the sum, $ints[1] + $ints[2] + $ints[4] = 17

Example 2

Input: @ints = (2, 5, 9, 11, 3), $k = 2
Output: 11

Example 3

Input: @ints = (2, 5, 9, 11, 3), $k = 0
Output: 2

Solution

unpack’s format '%32b*' provides a simple way to count the bits in a bit vector. Packing each of the numbers’ indices, counting its bits, greping for exactly $k bits, take the result as an array slice and sum over its elements.

use List::Util 'sum0';

sub k_bit_sum {
    my $k = shift;

    sum0 @_[grep unpack('%32b*', pack('l', $_)) == $k, 0 .. $#_];
}

See the full solution.


If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.