Skip to content

Instantly share code, notes, and snippets.

@Syrup-tan
Created October 18, 2015 22:11
Show Gist options
  • Save Syrup-tan/e9c1614caf4135f0b77f to your computer and use it in GitHub Desktop.
Save Syrup-tan/e9c1614caf4135f0b77f to your computer and use it in GitHub Desktop.
ENTERPRISE QUALITY STRING REVERSER
w = reverse('SebaJun');
while (v = w.next().value) console.log(v);
function *reverse(s) {
yield s;
for (i = 0, j = 0; j++ < s.length; i = 0) while (s[i+1] && i + j < s.length) {
yield s = [
s.slice(0, i),
s.slice(i+1, i+2),
s.slice(i, i+1),
s.slice(i+2),
].join(''); i++;
}
return s;
}
/** output
* SebaJun
* eSbaJun
* ebSaJun
* ebaSJun
* ebaJSun
* ebaJuSn
* ebaJunS
* beaJunS
* baeJunS
* baJeunS
* baJuenS
* baJuneS
* abJuneS
* aJbuneS
* aJubneS
* aJunbeS
* JaunbeS
* JuanbeS
* JunabeS
* uJnabeS
* unJabeS
* nuJabeS
*
* nuJabeS
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment