Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Pearls in the Box by Sim0000
"use strict";
function boxProbability(marbles, step) {
r = marbles.length
w = marbles.split("").reduce((prev, curr) => prev + (curr == "w"), 0)
p = (0.5 + Math.pow(1 - 2 / r, step - 1) * (w / r - 0.5))
return Math.round(p * 100) / 100
}
// Thanks Juge_Ti :-)
var assert = require('assert');
if (!global.is_checking) {
assert.equal(boxProbability('bbw', 3), 0.48, "First");
assert.equal(boxProbability('wwb', 3), 0.52, "Second");
assert.equal(boxProbability('www', 3), 0.56, "Third");
assert.equal(boxProbability('bbbb', 1), 0, "Fifth");
assert.equal(boxProbability('wwbb', 4), 0.5, "Sixth");
assert.equal(boxProbability('bwbwbwb', 5), 0.48, "Seventh");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
July 8, 2016