Skip to content

Instantly share code, notes, and snippets.

@reidblomquist
Created December 3, 2021 17:23
Show Gist options
  • Save reidblomquist/74a3a7f9cf20ba8baca33bad86c45313 to your computer and use it in GitHub Desktop.
Save reidblomquist/74a3a7f9cf20ba8baca33bad86c45313 to your computer and use it in GitHub Desktop.
Heard you kids still like 'em shloppy - here's day 3
const data = require('./data.json');
const getFrequency = (arr) => {
const dict = {};
arr.forEach(function(el) {
if (!dict[el]) dict[el] = 0;
dict[el]++;
});
return {
leastFrequent: `${Object.keys(dict).find(key => dict[key] === Math.min(...Object.values(dict)))}`,
mode: `${Object.keys(dict).find(key => dict[key] === Math.max(...Object.values(dict)))}`,
};
}
const results = data.data.reduce((acc, val, idx, arr) => {
val.split('').forEach((v, i) => {
const newSplitArr = [
...(acc.splitBitsArr[i] || []),
v
];
const { leastFrequent, mode } = getFrequency(newSplitArr);
acc.splitBitsArr[i] = newSplitArr;
acc.modeArr[i] = mode;
acc.leastFrequentArr[i] = leastFrequent;
acc.gammaBinary = acc.modeArr.join('');
acc.epsilonBinary = acc.leastFrequentArr.join('');
acc.gammaDecimal = parseInt(acc.gammaBinary, 2);
acc.epsilonDecimal = parseInt(acc.epsilonBinary, 2);
})
return acc;
}, {
epsilonBinary: "000000000000",
gammaBinary: "000000000000",
epsilonDecimal: 0,
gammaDecimal: 0,
modeArr: [],
leastFrequentArr: [],
splitBitsArr: [],
});
console.log(results.epsilonBinary);
console.log(results.epsilonDecimal);
console.log(results.gammaBinary);
console.log(results.gammaDecimal);
console.log(results.gammaDecimal * results.epsilonDecimal);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment