[Tutor] All possible 16 character alphanumeric strings?

akleider at sonic.net akleider at sonic.net
Sun Sep 16 04:29:17 CEST 2012


>> Hello again python tutor list.
>> I have what I see as a somewhat complicated problem which I have no idea
>> where to begin. I'm hoping you fine folks can help me.
>>
>> I'm trying to generate a list of every possible 16 character string
>> containing only 2-7 and a-z lowercase. I've seen some examples using
>> regex
>> to define which characters I want to use but not a way to generate the
>> complete list of all possibilities. I'm not looking for a handout- just
>> a
>> point in the right direction.
>>
>> Any information would be awesome, thanks.
>>
>> Right now I've got something like:
>>
>> import random
>>>>> ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
>>>>> range(16))
>>
>> Which only prints 1 number, obviously.
>>
>> or possibly something like this:
>>
>>
>> def genKey():
>>     hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
>>     alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
>>     return alnum_hash[:16]
>>
>>
>> Keeping in mind that although I understand this code, I did not write
>> it,
>> I
>> got it from stackoverflow.
>>
>> Again any help would be great. Feel free to ask if you must know exactly
>> what I'm trying to do.
>>
>> I'm running Ubuntu 12.04 and python 2.7
>>
>> Scott
>
> This seems to work:
> #!/usr/bin/env python
>
> # file :  every.py
> print 'Running "every.py"'
>
> possible = "234567abcdefghijklmnopqrstuvwxyz"
> word_length = 16
>
> word_list = []
> def add_word(word):
>     if len(word)==word_length:
>         word_list.append(word)
>         print word   # There may come a time you won't want this line.
>     else:
>         for c in possible:
>             new_word = word + c
>             add_word(new_word)
>
> add_word("")
>
> # print word_list
>
Addendum:
As others have pointed out, you might run out of patience, time, memory,
or what ever ...long before completion.
one solution would be to make "word_length" &/or "possible" shorter.
It was fun trying to knock this one out.




More information about the Tutor mailing list