Python Forum

Full Version: Generating all words of maximun length.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I am working in a python challenge wich requires to decipher SHA-1 hashes wich correspond to password of a maximun length of 5 characters. The passwords can be only formed with characters between a and z. Is there a simpler way to generate all these words? I know that I can do this with five anidated loops but I would like to do this with regular expresions but I dont know how. Does anyone know how to do it?

Thanks!
I don't know what you mean. Regular expressions are about matching patterns in strings, not generating strings.
I know that is one of the possible uses of the regular expressions (probably the main use) but I dont know if also it can be used to generate strings. But my question is more general. Is there a easy code to do the task that I said? If the words had a maximun length of 30 characters, Would I have to use 30 anidated loops? I think that must exist a more elegant way to do this task.
If you want to generate all passwords of length 5, you could use itertools.product. Repeat for other lengths.

from itertools import product, islice
import string

# all passwords of lowercase ascii of length 5
pw_iter = map(lambda x: "".join(x), product(string.ascii_lowercase, repeat=5))

# Show first 5 passwords
print(list(islice(pw_iter, 5)))

# Count the remaining passwords
count = len(tuple(pw_iter))
print(f"and {count:,} more passwords.")
Output:
['aaaaa', 'aaaab', 'aaaac', 'aaaad', 'aaaae'] and 11,881,371 more passwords.
(Jul-12-2020, 05:48 AM)bowlofred Wrote: [ -> ]If you want to generate all passwords of length 5, you could use itertools.product. Repeat for other lengths.

from itertools import product, islice
import string

# all passwords of lowercase ascii of length 5
pw_iter = map(lambda x: "".join(x), product(string.ascii_lowercase, repeat=5))

# Show first 5 passwords
print(list(islice(pw_iter, 5)))

# Count the remaining passwords
count = len(tuple(pw_iter))
print(f"and {count:,} more passwords.")
Output:
['aaaaa', 'aaaab', 'aaaac', 'aaaad', 'aaaae'] and 11,881,371 more passwords.

Thank you men! That is I was searching.