Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda