This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stringifyTable(arr) { | |
var longest = 0; | |
return arr.map(function(innerArr) { | |
innerArr = trimArray(innerArr).map(val => '' + val); | |
longest = Math.max(longest, Math.max(innerArr.map(val => val.length))); | |
return innerArr; | |
}).map(function(innerArr) { | |
return innerArr.map(function(val) { | |
return padLeft('' + val, longest, ' '); | |
}).join(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayMin(array) { | |
return Math.min.apply(null, array); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function arrayMax(array) { | |
return Math.max.apply(null, array); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trimArray(arr) { | |
for (var start = 0; start < arr.length && !(start in arr); start++); | |
for (var end = arr.length; end > start && !(end - 1 in arr); end--); | |
return arr.slice(start, end); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// get all words of a certain length in alphabet | |
function permute(alphabet, wordLength) { | |
if (wordLength === 0) return ['']; | |
var words = []; | |
var shorterWords = permute(alphabet, wordLength - 1); | |
for (var i = 0; i < alphabet.length; i++) { | |
for (var j = 0; j < shorterWords.length; j++) { | |
words.push(alphabet[i] + shorterWords[j]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCircleArea(diameter) { | |
return diameter * diameter * Math.pi / 4; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// probability that the event with the passed probability will NOT occur | |
function complement(p) { | |
return 1 - p; | |
} | |
// probability that a and b will happen when neither outcome | |
// is affected by the other (accepts 1 or more arguments) | |
function intersectionOfIndependentEvents(a, b) { | |
var ret = a; | |
for (var i = 1; i < arguments.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uniques(iterable) { | |
return Array.from(new Set(iterable)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countUnique(iterable) { | |
return new Set(iterable).size; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countingSort(arr, min, max) { | |
var i, z = 0, count = []; | |
for (i = min; i <= max; i++) { | |
count[i] = 0; | |
} | |
for (i=0; i < arr.length; i++) { | |
count[arr[i]]++; | |
} | |
for (i = min; i <= max; i++) { | |
while (count[i]-- > 0) { |