Last active
October 28, 2015 14:34
-
-
Save jkz/20f027a114701d13547e to your computer and use it in GitHub Desktop.
TOTAL_NUMBER_OF_UNIQUE_ENGLISH_LOST_MY_NAME_BOOKS
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
/* eslint semi: [2, "never"] */ | |
var STORY_COUNT = 45 // [34, 48, 48, 31, 45, 36, 43, 38, 40, 34] | |
var HELPERS = range(0, 3) | |
var LENGTHS = range(3, 12) | |
var ALPHABET = 26 // Needs per country | |
var HELPER_COUNT = 3 | |
var DIVERSITY = 6 // OR: [6, 6, 2, 2, 2, 2, 2, 2, 2, 2] | |
function range(start, end) { | |
var arr = [] | |
while (start <= end) { | |
arr.push(start++) | |
} | |
return arr | |
} | |
function sum(arr) { | |
return arr.reduce(function (x, y) { | |
return x + y | |
}) | |
} | |
function product(arr) { | |
return arr.reduce(function (x, y) { | |
return x * y | |
}) | |
} | |
function factorial(n) { | |
var result = n | |
if (n < 1) { | |
return 1 | |
} | |
while (--n) { | |
result = n * result | |
} | |
return result | |
} | |
function C(sets) { | |
var c = Math.round(factorial(sum(sets)) / product(sets.map(factorial))) | |
console.log('C(' + sets.join(', ') + ')', c) | |
return c | |
} | |
function P(elements, sample) { | |
var p = Math.round(factorial(elements) / factorial(elements - sample)) | |
console.log('P(' + elements + ', ' + sample + ')', p) | |
return p | |
} | |
function totalNumberOfUniqueBooks(options) { | |
return options.diversity * sum(options.bookLengths.map(function (s) { | |
return sum(options.helperLengths.map(function (h) { | |
return ( | |
P(options.totalStories, s - h) | |
* P(options.totalHelpers, h) | |
* Math.pow(options.alphabet, h) | |
* C([s - h, h]) | |
) | |
})) | |
})) | |
} | |
function defaultOptions(options) { | |
options = options || {} | |
options.totalHelpers = options.totalHelpers || 0 | |
options.totalStories = options.totalStories || 1 | |
options.diversity = options.diversity || 1 | |
options.helperLengths = options.helperLengths || range(0, options.totalHelpers) | |
options.bookLengths = options.bookLengths || range(options.totalStories, options.totalStories) | |
options.alphabet = options.alphabet || options.totalStories | |
return options | |
} | |
function assert(expected, options, msg) { | |
console.log() | |
console.log('========' + (msg || '') + '========') | |
options = defaultOptions(options) | |
console.log(options) | |
var actual = totalNumberOfUniqueBooks(options) | |
console.log() | |
console.log(actual, actual == expected ? '==' : '!=', expected) | |
if (actual != expected) { | |
throw "Fail" | |
} | |
} | |
assert(1) | |
assert(2, { | |
bookLengths: [2], | |
totalStories: 2 | |
}) | |
assert(2, { | |
totalHelpers: [1], | |
helperLengths: [1], | |
alphabet: 2 | |
}) | |
assert(4, { | |
totalStories: 2, | |
bookLengths: range(1, 2) | |
}) | |
assert(100, { | |
totalStories: 10, | |
bookLengths: range(1, 2) | |
}) | |
assert(200, { | |
totalStories: 10, | |
bookLengths: [2], | |
helperLengths: [1], | |
totalHelpers: 1, | |
alphabet: 10, | |
}) | |
assert(290, { | |
totalStories: 10, | |
bookLengths: [2], | |
helperLengths: [0, 1], | |
totalHelpers: 1, | |
alphabet: 10, | |
}) | |
assert(400, { | |
totalStories: 10, | |
bookLengths: [2], | |
helperLengths: [1], | |
totalHelpers: 2, | |
alphabet: 10, | |
}) | |
assert(490, { | |
totalStories: 10, | |
bookLengths: [2], | |
helperLengths: [0, 1], | |
totalHelpers: 2, | |
alphabet: 10, | |
}) | |
assert(1, { | |
totalHelpers: 3, | |
totalStories: 45, | |
diversity: 6, | |
bookLengths: range(3, 12), | |
helperLengths: range(0, 3), | |
alphabet: 26 | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment