Question

I am trying to write an application to brute force a fully lower case, letters only, 6 character long password that has 2 letters repeating twice. I tried using itertools.product(string.lowercase, 6) but even though I have a very "beefy" computer, it didnt have enough RAM to finish up computing, that is because itertools actually builds a whole list before returning it.

So then I thought of having a list like so [0, 0, 0, 0, 0, 0] and it would count from right to left, everytime an index hits 26 it would go back to 0 and the slot to it's left would increment 1, except I havent figured out a way to code that.

I've been trying to crack a work arround on this one for quite a while now and turning here is my last resort, any algorithm tips are very welcome. I'm sorry to make this sort of a "homework" type question, but I am really dumbfounded by this one.

Was it helpful?

Solution

Using something like this will work without taking much memory:

import itertools
import string

for guess in itertools.product(string.lowercase, repeat=6):
    if checkguess(''.join(guess)):
        print("Password is: {0}".format(''.join(guess)))

itertools.product generates the possible combinations one by one. It does not return anything, it is a generator function. See the python documentation. All the functions in itertools behave like or handle iterators, hence the name.

If your code takes up much memory, you might want to post it here, so we may find the culprit.

Based on DSM's suggestion for faster code, you can do something like this:

import string
from itertools import product, combinations, permutations

for guess_chars in combinations(string.lowercase, 4):
    for doubled_chars in combinations(guess_chars, 2):
        for guess in permutations(guess_chars + doubled_chars):
            #...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top