Last active
November 16, 2017 15:05
-
-
Save jougene/d13f46225e8effd86e5504bbc5cc8610 to your computer and use it in GitHub Desktop.
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
const splitByPairs = (x) => { | |
const pairsCount = x.length - 1; | |
return new Array(pairsCount).fill().map((index, val) => x.substr(val, 2)); | |
}; | |
const digitsCount = (digit, number) => number.split('').filter(num => num == digit).length; | |
const dec2bin = (dec) => { return (dec >>> 0).toString(2) }; | |
const range = (l, r) => new Array(r - 1).fill().map((_,k) => k + l); | |
const fillByZeroes = (x, to) => { | |
const numLength = (x).toString().length; | |
if (numLength >= to) { | |
return x; | |
} | |
const zeroLacks = to - numLength; | |
return '0'.repeat(zeroLacks).concat((x).toString()); | |
}; | |
const withoutTwoZeros = (x, y) => { | |
const pow = x + y; | |
if(x == 1 && y == 1) { | |
return 2; | |
} | |
const zeroOnesColl = range(1, Math.pow(2, pow)) | |
.map(x => dec2bin(x)) | |
.map(x => fillByZeroes(x, pow)); | |
//return zeroOnesColl; | |
const filtered = zeroOnesColl.filter(el => { | |
return digitsCount(0, el) == x && digitsCount(1, el) == y; | |
}); | |
return filtered | |
.map(x => splitByPairs((x).toString())) | |
.filter(pairs => pairs.indexOf('00') == -1) | |
.length | |
}; | |
console.log(withoutTwoZeros(2, 2)); // 3 | |
console.log(withoutTwoZeros(1, 1)); // 2 | |
console.log(withoutTwoZeros(1, 3)); // 4 | |
console.log(withoutTwoZeros(2, 4)); // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment