Skip to content

Instantly share code, notes, and snippets.

@yjzhang
Last active September 17, 2015 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yjzhang/d1212684b21d7b48a466 to your computer and use it in GitHub Desktop.
Save yjzhang/d1212684b21d7b48a466 to your computer and use it in GitHub Desktop.
Given a list of chars, generate all possible permutations of the chars where the chars can repeat. Equivalent (should be) to itertools.product(chars, repeat=num_chars)
def password_gen(num_chars, chars):
"""
This is a generator
that outputs all passwords of lowercase letters of length
num_chars.
Inputs:
num_chars- the length of the password
chars- a list of the characters that can be part of the password
"""
if num_chars == 0:
yield ''
else:
for password in password_gen(num_chars-1, chars):
for char in chars:
yield char + password
def password_gen_list(num_chars, chars):
if num_chars == 1:
return chars
else:
passwords = []
for password in password_gen(num_chars-1, chars):
for char in chars:
passwords.append(char + password)
return passwords
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment