Skip to content

Instantly share code, notes, and snippets.

@Krazete
Created November 30, 2017 01:27
Show Gist options
  • Save Krazete/a64f3950529d58c0e48d191e8f541d57 to your computer and use it in GitHub Desktop.
Save Krazete/a64f3950529d58c0e48d191e8f541d57 to your computer and use it in GitHub Desktop.
convert cs122a midterm CDF to PMF
var cdf = [1, 1, 1, 1, 1, 1, 4, 5, 11, 21, 34, 56, 84, 126, 181, 226, 261, 294, 305, 311, 312];
var pdf = cdf.slice(0, 1).concat(cdf.map((e, i) => e - cdf[i - 1]).slice(1));
var max = pdf.reduce((a, b) => Math.max(a, b));
var log = "";
for (let r = 0; r <= max; r++) {
for (let c = 0; c < pdf.length; c++) {
var y = max - pdf[c];
if (y < r) {
log += "|++++| ";
}
else {
if (y - 1 < r) {
var x = max - r;
log += " " + (x < 10 ? " " : "") + x + " ";
}
else {
log += " ";
}
}
}
log += "\n";
}
for (let c = 0; c < pdf.length; c++) {
log += "--------";
}
log += "\n";
for (let c = 0; c < pdf.length; c++) {
d = c * 4 + 8
log += d + (d < 10 ? " " : "") + " ";
}
console.log(log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment