Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / stringify-table.js
Last active June 26, 2017 09:17
JS function: stringify table (2D array)
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();
@Daniel-Hug
Daniel-Hug / array-min.js
Created June 17, 2017 02:21
JS function: get min (smallest) value from array
function arrayMin(array) {
return Math.min.apply(null, array);
}
@Daniel-Hug
Daniel-Hug / array-max.js
Created June 17, 2017 02:21
JS function: get max value from array
function arrayMax(array) {
return Math.max.apply(null, array);
}
@Daniel-Hug
Daniel-Hug / trim-array.js
Last active June 16, 2017 03:45
JS function: trim undefined values from ends of array
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);
}
@Daniel-Hug
Daniel-Hug / permute.js
Last active June 15, 2017 19:28
JS function: get all word permutations of a given length over a given alphabet
// 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]);
}
@Daniel-Hug
Daniel-Hug / circle-area.js
Created June 14, 2017 00:18
JS function: get area of circle from diameter
function getCircleArea(diameter) {
return diameter * diameter * Math.pi / 4;
}
@Daniel-Hug
Daniel-Hug / probabilities.js
Last active October 5, 2020 18:28
JS probability functions
// 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++) {
@Daniel-Hug
Daniel-Hug / uniques.js
Created June 10, 2017 01:16
JS function: get unique items from collection
function uniques(iterable) {
return Array.from(new Set(iterable));
}
@Daniel-Hug
Daniel-Hug / count-unique.js
Created June 10, 2017 01:14
JS function: count unique items in collection
function countUnique(iterable) {
return new Set(iterable).size;
}
@Daniel-Hug
Daniel-Hug / counting-sort.js
Last active November 21, 2017 00:55
JS functions: linear time counting sort for integers
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) {