Last active
January 28, 2018 14:55
-
-
Save kevinsimper/c2ec0a5099932db9e230e236f6da405b to your computer and use it in GitHub Desktop.
How to generate a list of all combinations with Javascript Generators
This file contains hidden or 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 letters = 'abcdefghijklmnopqrstuvxyzæøå' | |
| const numbers = '0123456789' | |
| const list = (letters + numbers).split('') | |
| exports.generate = function* generate(length) { | |
| function* gen(length) { | |
| for(var i = 0; i < list.length; i++) { | |
| if(length > 1) { | |
| for (const val of gen(length - 1)) { | |
| yield list[i] + val | |
| } | |
| } else { | |
| yield list[i] | |
| } | |
| } | |
| } | |
| for(var i = 1; i <= length; i++) { | |
| for (const val of gen(i)) { | |
| yield val | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment