primitive password cracker
Greg Ewing
greg.ewing at canterbury.ac.nz
Thu Jan 7 19:25:23 EST 2021
Another way to approach this problem is using recursion, e.g.
def find_combinations(items, n, comb, result):
if n == 0:
result.append(comb)
else:
for item in items:
find_combinations(items, n - 1, comb + [item], result)
words = []
find_combinations(['a', 'b', 'c'], 3, [], words)
print(words)
For this particular problem it's less efficient than the
technique used by itertools.product, because it generates
sub-combinations multiple times. However, it's a useful
technique to keep in mind whenever you have a "variable
number of nested for-loops" kind of problem.
--
Greg
More information about the Python-list
mailing list