Published
Edited
Dec 8, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
binary.map(input => { return input.split('').map(l => l.split(' ')) })
Insert cell
Insert cell
totals = {
let totals = []
for (let i = 0; i < binary[0].length; i++) {

let temp = []
for (let j = 0; j < binary.length; j++) {
temp.push(binary[j][i])
}
let counts = {}
for (var k = 0; k < binary.length; k++) {
counts[temp[k]] = 1 + (counts[temp[k]] || 0);
}
totals.push(counts)
}
return totals
}
Insert cell
gamma = totals.map((x) => {return x["0"] > x["1"]? "0" : "1" }).join("")
Insert cell
epsilon = totals.map((x) => {return x["0"] > x["1"]? "1" : "0" }).join("")

// epsilon = (2 ** data[0].length) - gamma - 1
// epsilon = 2^12-1-gamma
Insert cell
digit = (binary) => {
return parseInt(binary, 2)
}
Insert cell
digit(epsilon)*digit(gamma)
Insert cell
Insert cell
mostCommon = (numbers, i) => {
const counts = { 1: 0, 0: 0 };
for (const n of numbers) counts[n[i]]++;
return counts[0] > counts[1] ? 0 : 1;
}
Insert cell
function filter(input, criteria, i = 0) {
const match = criteria(input, i);
const remaining = input.filter((n) => n[i] == match);
return remaining.length === 1
? remaining[0]
: filter(remaining, criteria, i + 1);
}
Insert cell
o2 = digit(filter(binary, mostCommon))
Insert cell
co2 = digit(filter(binary, (input, i) => 1 - mostCommon(input, i)))
Insert cell
o2 * co2
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more