Skip to content

Instantly share code, notes, and snippets.

@sters
Created March 31, 2014 02:49
Show Gist options
  • Select an option

  • Save sters/9884323 to your computer and use it in GitHub Desktop.

Select an option

Save sters/9884323 to your computer and use it in GitHub Desktop.
function inversefb(data) {
var _base = "1211213", base = "";
var _value = [3,5,6,9,10,12,15], value = [];
data = data.map(function(v){ return v == "Fizz" ? "1" : v == "Buzz" ? "2" : "3"}).join("");
while(data.length > base.length) {
base += _base;
value = value.concat(_value);
}
for(var i=0; i<2; i++) {
base += base;
value = value.concat(value);
}
var pos = base.indexOf(data);
if(pos == -1) {
return "";
}
if(data.length == 1) {
return value[pos].toString();
}
var tmpAns = [], lastAns = [];
var stopper = 0;
while(pos + data.length - 1 < value.length) {
if(stopper > 5) break;
var n = [ value[pos], value[pos + data.length - 1] ];
if(n[0] > n[1]) {
n[1] += Math.round(data.length / _base.length) * 15;
}
if(tmpAns.length == 0 || (tmpAns[1] - tmpAns[0] > n[1] - n[0])) {
tmpAns = n;
stopper = 0;
} else {
stopper++;
}
pos = base.indexOf(data, pos + 1);
}
return tmpAns.toString().replace(",", "..");
}
console.log([
inversefb(["Fizz", "Buzz", "Fizz", "Fizz", "Buzz", "Fizz", "FizzBuzz"]),
inversefb(["FizzBuzz", "Fizz", "Buzz", "Fizz"]),
inversefb(["FizzBuzz", "Fizz", "Buzz", "Fizz", "Fizz", "Buzz"]),
inversefb(["Buzz","Buzz"]), // Error
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment