Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Distribute list of bits into given amount of slots

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda