Created
May 24, 2017 03:51
-
-
Save cosmicbuffalo/c9d30bd2a69d08bef24eae3c6b7d9da0 to your computer and use it in GitHub Desktop.
A recursive algorithm to get all the possibilities of a binary string after filling in placeholders
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
function binaryCombos(str){ | |
var placeholder = "_"; | |
var result = []; | |
if (!str.includes(placeholder)){ | |
result.push(str); | |
return result; | |
} | |
for(var x = 0; x < str.length; x += 1){ | |
if (str[x] == placeholder){ | |
result = result.concat(binaryCombos(str.slice(0,x) + "0" + str.slice(x+1))); | |
result = result.concat(binaryCombos(str.slice(0,x) + "1" + str.slice(x+1))); | |
break; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment