Last active
August 29, 2015 14:25
-
-
Save afonsomatos/50e30cf0c9bc7d7ce68d 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
function getNextChar (char) { | |
if (char == 'z') return 'A'; | |
if (char == 'Z') return 'a'; | |
return String.fromCharCode(char.charCodeAt(0) + 1); | |
} | |
function getNextString (str) { | |
let last = str.slice(-1), | |
init = str.slice(0, str.length - 1); | |
if (last === 'Z') { | |
let trail = 1, i = init.length - 1; | |
for ( ; i > 0; --i) { | |
if (init[i] === 'Z') { | |
// Propagate the nexted char | |
++trail; | |
} else { | |
// We found a char that can be nexted | |
break; | |
} | |
} | |
str = init.slice(0, i) + | |
getNextChar(init[i]) + | |
'a'.repeat(trail); | |
return str; | |
} | |
return init + getNextChar(last); | |
} | |
function* sequenceString (str) { | |
let newStr = getNextString(str); | |
yield newStr; | |
yield* sequenceString(newStr); | |
} | |
var k = sequenceString('ZaZZ'); | |
for (var i = 0; i < 100; ++i) console.log(k.next()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment