Python Forum
Generating all words of maximun length. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generating all words of maximun length. (/thread-28272.html)



Generating all words of maximun length. - MathCommander - Jul-12-2020

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!


RE: Generating all words of maximun length. - ndc85430 - Jul-12-2020

I don't know what you mean. Regular expressions are about matching patterns in strings, not generating strings.


RE: Generating all words of maximun length. - MathCommander - Jul-12-2020

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.


RE: Generating all words of maximun length. - bowlofred - Jul-12-2020

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.



RE: Generating all words of maximun length. - MathCommander - Jul-12-2020

(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.