Home > front end >  Distribute list of bits into given amount of slots
Distribute list of bits into given amount of slots

Time:03-26

Given a bitfield like the following (numbers can only be 1 or 0):

const bits = [1, 1, 0, 1, 0, 0, 0, 1]

I want to produce a list of N average values where N is an arbitrary number.

In our example above, given N is 4, the result would be [1, 0.5, 0, 0.5]. (group the array 2 by 2 and then calculate the average number of each group).

If, instead, N was 2, the result would be [0.75, 0.25].

I have been using the following code so far:

const average = (...args) => args.reduce((a, b) => a   b) / args.length;

const averageNumbers = Ramda.splitEvery(Math.floor(bits.length / N), bits).map(nums => average(nums))

The problem is that the above doesn't work in case my bits array is composed of 696 values and I need 150 average numbers, because I end up having 174 numbers instead of 150.

What am I doing wrong?

CodePudding user response:

As mentioned in the comments, you end up with 174 numbers instead of 150 because floor(696 / 150) divides the bit field into 174 chunks of 4 bits.

To get an approximate value for the averages, you can first "expand" the bit field to a length that is a multiple of N before taking the averages.

// Factor is the lowest number to expand bits by for its length to be a multiple of N
const factor = lcm(bits.length, N) / bits.length;

// Expand bits by the factor
const expandedBits = bits.map(bit => Array(factor).fill(bit)).flat(1);

const averages = splitEvery(Math.floor(expandedBits.length / N), expandedBits).map(nums => average(...nums));

// averages.length should now always be N
  • Related