Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

79
Views
array.sort() - how to get very uneven distribution?

I'm trying to remove random elements from a list of cartesian combinations of values,
so that it affects the items in a less even way.

So that for example some items do not exist anymore, or in very low quantities, while others keep their numbers.

but although I'm dropping 200+ items from the total of 625 items, no single items ever gets a distribution lower than around ~70.

I would have expected for some items completely disappear, or at least get a lot less distributed.

How do you achieve a more uneven distribution?

base = [
  ['a1', 'a2', 'a3', 'a4', 'a5'],
  ['b1', 'b2', 'b3', 'b4', 'b5'],
  ['c1', 'c2', 'c3', 'c4', 'c5'],
  ['d1', 'd2', 'd3', 'd4', 'd5'],
]

const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())))

const countDistributions = output => {
  const counts = {}
  output.forEach(item => {
    item.forEach(letter => {
      counts[letter] = (counts[letter] || 0) + 1
    })
  })
  return counts
}

const output = cartesian(...base)
console.log(countDistributions(output))

output.sort(() => Math.random() - 0.5)
const dropSize = output.length / 3
truncatedOutput = output.slice(0, output.length - dropSize)
console.log(countDistributions(truncatedOutput))

7 months ago ยท Juan Pablo Isaza
Answer question
Find remote jobs