Python Forum
Generating all words of maximun length.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generating all words of maximun length.
#1
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!
Reply
#2
I don't know what you mean. Regular expressions are about matching patterns in strings, not generating strings.
Reply
#3
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.
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Create a function to find words of certain length ag4g 2 4,011 Apr-21-2019, 06:20 PM
Last Post: BillMcEnaney
  Compare all words in input() to all words in file Trianne 1 2,716 Oct-05-2018, 06:27 PM
Last Post: ichabod801
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 5,112 Jul-21-2018, 02:24 PM
Last Post: dj99

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020