Chunk Humping

by Jacob Bennett

HTML

<button onclick="pressButton()" class="foo">
Button
</button>

JavaScript

// same partitioning logic, unchanged
function randomGaussian() {
  let r = 0;
  const iteration = 6;
  for (let i = iteration; i > 0; i--) r += Math.random();
  return r / iteration - 0.5;
}

function subListWeightBuilder(partitionCount) {
  const gauss = [];
  const population = 250;
  const chunkWeights = {};
  if (partitionCount > population) return chunkWeights;
  for (let i = 0; i < population * 2; i++) gauss.push(randomGaussian());
  gauss.sort((a, b) => a - b);

  const partitionWidth = gauss.length / partitionCount;
  for (let idx = 0; idx < partitionCount; idx++) {
    const start = Math.round(idx * partitionWidth);
    const end = Math.round((idx + 1) * partitionWidth);
    let sum = 0;
    for (let j = start; j < end; j++) sum += 1 / (Math.abs(gauss[j]) + 0.66);
    chunkWeights[idx] = sum / Math.max(1, end - start);
  }
  let offset = 0;
  for (let key in chunkWeights) offset += chunkWeights[key];
  offset /= partitionCount;
  offset = (1 - offset) + Number.MIN_VALUE;
  for (let key in chunkWeights) chunkWeights[key] += offset;
  return chunkWeights;
}

function removeMeFromTheList(removeMe, theList) {
  for (let i = 0; i < theList.length; i++) {
    if (theList[i] === removeMe) { theList.splice(i, 1); return; }
  }
}

function makeListOfGaussianLists(chunkWeights, objects) {
  const listOfLists = [];
  const chunkWeightsSize = Object.keys(chunkWeights).length;
  if (chunkWeightsSize < 1 || objects.length < chunkWeightsSize) return null;
  const elementCount = objects.length / chunkWeightsSize;
  for (let i = 0; i < chunkWeightsSize; i++) {
    const modifiedElementCount = parseInt(chunkWeights[i] * elementCount) || 0;
    let innerList = [];
    for (let j = modifiedElementCount; j > 0; j--) {
      const obj = objects[0];
      if (obj == null) break;
      innerList.push(obj);
      removeMeFromTheList(obj, objects);
    }
    listOfLists.push(innerList);
  }
  if (objects.length > 0) {
    do...