Skip to content

Instantly share code, notes, and snippets.

View okaybenji's full-sized avatar

Benji Kay okaybenji

View GitHub Profile
@okaybenji
okaybenji / frog-riddle-c.js
Last active July 25, 2018 17:30
Frog Riddle exhibit C
// Returns a sex at random.
const getRandomSex = () => Math.random() > 0.5 ? 'm' : 'f';
// Returns a frog of random sex.
const getRandomFrog = () => ({sex: getRandomSex()});
// Returns random item from a list.
const randomArrayElement = array => array[Math.floor(Math.random() * array.length)];
/**
@okaybenji
okaybenji / frog-riddle-b.js
Last active July 25, 2018 17:30
Frog Riddle exhibit B
// Returns a frog of random sex.
const getRandomFrog = () => Math.random() > 0.5 ? 'm' : 'f';
// Returns a random pair of frogs.
// Never returns 'ff'.
const getRandomFrogPair = () => {
const pair = getRandomFrog() + getRandomFrog();
// If both frogs are female, toss this pair and get a new one.
if (pair === 'ff') {
return getRandomFrogPair();
@okaybenji
okaybenji / frog-riddle-a.js
Last active July 2, 2018 16:18
Frog Riddle exhibit A
// Returns a frog of random sex.
const getRandomFrog = () => Math.random() > 0.5 ? 'm' : 'f';
// Returns a random pair of frogs.
const getRandomFrogPair = () => getRandomFrog() + getRandomFrog();
// Get a million pairs.
const population = [...Array(1000000)].map(getRandomFrogPair);
// Calculate the distribution.
@okaybenji
okaybenji / kNN.js
Created August 11, 2017 22:01
ES6 implementation of k-Nearest Neighbor algorithm
/**
* Based on http://burakkanber.com/blog/machine-learning-in-js-k-nearest-neighbor-part-1/
*/
/**
* Data can have an arbitrary number of numeric properties, and must have a type (unless we're
* guessing what the type is). Currently, all properties are equally weighted.
*/
const data = [
{rooms: 1, area: 350, type: 'apartment'},
@okaybenji
okaybenji / fibonacci.pl
Created June 4, 2017 23:43
Fibonacci generator written in Prolog
fib(0, 1).
fib(B, C) :- fib(A, B), C is A + B.
% fib(0, X) -> X = 1.
% fib(1, X) -> X = 1, X = 2.
% fib(144, X) -> X = 233.