Last active
September 17, 2015 01:53
-
-
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)
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
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