1

I am new to python and has been trying to make this algorithm but couldn't figure it out. How could I make a list of every possible word made of a-Z, so that it goes like

a, b, c, d, e.... aa, ab, ac, ad... aaa, aab, aac...The purpose of doing this, is to encode every word into md5 hash and repeat this until finding a hash code that has a specific prefix.

So, how could I achieve this? Thank you in advance for your help!

2
  • every string combination starting from? the alphabet? what are the elements that you want to combine? Commented Mar 6, 2020 at 13:42
  • 1
    Complementing what @kederracsaid said, starting from what..? And, what's the limitation? You can go from "a" till "aaaaaaaaaaaabbbb......" infinity. Commented Mar 6, 2020 at 13:45

3 Answers 3

4

You can use itertools.product (since you want the cartesian product of all letters up to a given length) and a list comprehension to iterate over a range up to the desired length (4 in your example):

from string import ascii_lowercase
from itertools import product

n = 4
[''.join(i) for r in range(1,n) for i in product(ascii_lowercase, repeat=r)]

Which gives:

['a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g', 
 ...   
 'aa',
 'ab',
 'ac',
 'ad',
 'ae',
 'af',
 'ag',
 'ah',
 'ai',
 'aj',
  ...
 'zz',
 'aaa',
 'aab',
 'aac',
 'aad',
 'aae',
 'aaf',
 'aag',
  ...
Sign up to request clarification or add additional context in comments.

6 Comments

Where do they say "up to a given length"?
Well is there is no up to a given length, it's just a matter of setting n up to the alphabet length, right? @HeapOverflow
You mean because the last value is not included in the range @heap ? then just len(ascii_lowercase)+1? I can't see how this could be hard to adapt in any case
No, not off-by-one. You'd also be missing 'a' * 42 (granted, they won't get that far anyway unless they transfer their consciousness to an immortal quantum realm or something like that).
I think that can safely be considered an edge case not worth considering, bu I see your point :) @heap
|
2

you can use a generator expression and itertools.product:

from string import ascii_lowercase
from itertools import product, count

gen = (''.join(i) for r in count(1) for i in product(ascii_lowercase, repeat=r))

every time you want a combination you can use the built-in function next:

next(gen)

generators are memory friendly and if you want to generate a huge amount of strings that will be process somewhere else (in other function for ex) it will be more convenient to not keep all the combinations in memory

Comments

0

How about a recursive generator?

from string import ascii_lowercase
from itertools import islice

def words():
    yield ''
    for word in words():
        for char in ascii_lowercase:
            yield word + char

print(' '.join(islice(words(), 30)))
print(' '.join(islice(words(), 0, 10**8, 10**7)))

Output:

 a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab ac
 uvxwj aqswtt bmpvrd cimuon dejtlx eagsjh ewdrgr fsaqeb gnxpbl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.