0

I want to generate all possible combinations from a-zA-Z0-9 and with my max string length.

So, for example, if I set the max length to 25, then my desired output is

a
...
z
aa
...
a1
...
zzzzzzzzzzzzzzzzzzzzzzzzz

So, generate all possible combinations and print each one to console I'm new in python, so I have no idea how to realize this...

blackbrandt
  • 2,010
  • 1
  • 15
  • 32
Kuroneko
  • 57
  • 5
  • 1
    What have you tried so far? I would suggest looking at [itertools](https://docs.python.org/3.5/library/itertools.html#itertools.combinations_with_replacement) library, specifically `combinations_with_replacement` – blackbrandt Aug 02 '21 at 14:33
  • Does this answer your question? [How to create a brute-force password cracker for alphabetical and alphanumerical passwords?](https://stackoverflow.com/questions/40269605/how-to-create-a-brute-force-password-cracker-for-alphabetical-and-alphanumerical) – user4157124 Jul 08 '22 at 00:54

2 Answers2

1

It's going to take close to an eternity to run with e.g. max_length=25 (the number of possible combinations is astronomical), but I think this should do what you want (eventually):

from itertools import combinations_with_replacement

characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

max_length = 4

for r in range(1, max_length+1):
    for combo in combinations_with_replacement(characters, r=r):
        print(''.join(combo))                             
DubeyJI
  • 3
  • 3
akensert
  • 284
  • 1
  • 2
  • 7
1

Same concept as akensert's answer, but a bit more readable:

from itertools import combinations_with_replacement
from string import ascii_letters

max_length = 4

for r in range(1, max_length+1):
    for combo in combinations_with_replacement(ascii_letters, r=r):
        print(''.join(combination))   

As a fair warning, the number of combinations is absolutely massive. Just the number of 25 character combinations is:

Sample Size: 26+26+10 = 62

All possible combinations with replacement: 62^n, so 62^25=6.25x10^44. Even if you got the cycle time down to 1 nanosecond, you'd still be looking at 10^35 seconds which is 10^27 years. And that's just for the largest set, ignoring all IO, and assuming the computer never fails.

You may want to consider rethinking your approach if you want your program to finish running before the end of the universe.

blackbrandt
  • 2,010
  • 1
  • 15
  • 32