Skip to content

Instantly share code, notes, and snippets.

@onlurking
Last active March 6, 2018 17:44
Show Gist options
  • Save onlurking/0702891ff0d11fad57f07fa44747b24e to your computer and use it in GitHub Desktop.
Save onlurking/0702891ff0d11fad57f07fa44747b24e to your computer and use it in GitHub Desktop.
generate all possible strings under 20 lines of Python
from itertools import count, product
from string import digits, ascii_lowercase, ascii_uppercase
from sys import stdout
import argparse
combinations = ascii_lowercase
parser = argparse.ArgumentParser()
parser.add_argument("--digits", "-d", help="include numbers", action="store_true")
parser.add_argument("--upper", "-u", help="include uppercase letters", action="store_true")
args = parser.parse_args()
if args.upper: combinations += ascii_uppercase
if args.digits: combinations += str(digits)
num = (n for n in count(start=0, step=1))
for length in range(int(input("Max strings length: "))+1):
words = (''.join(l) for l in product(combinations, repeat=next(num)))
for word in words: stdout.write(word+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment