Skip to content

Instantly share code, notes, and snippets.

@Highstaker
Last active June 27, 2016 11:22
Show Gist options
  • Save Highstaker/b2f738bd3566ce89697d19b9d5fad919 to your computer and use it in GitHub Desktop.
Save Highstaker/b2f738bd3566ce89697d19b9d5fad919 to your computer and use it in GitHub Desktop.
A function that generates a set of variations of a string based on replacers. Good for coming up with passwords with popular replacers (1 for i, 0 for o, etc.)
INPUT = "terminator"
REPLACERS = {
"o" : {"0", "Q"},
"i" : {"1",},
"a" : {"@",},
"e" : {"3",},
}
# Add capitals
for i in range(97, 123):
ch = chr(i)
REPLACERS.setdefault(ch, set()).update((ch.upper(),))
# actual generator
def perebor(inp, inp_prev=""):
results = set()
for n, ch in enumerate(inp):
try:
for repl in REPLACERS[ch]:
inpprev = inp_prev + inp[:n] + repl
inp_next = inp[n+1:]
results = results.union(perebor(inp_next, inp_prev=inpprev))
except KeyError:
pass
else:
# got a full result
result = inp_prev+inp
results.update([result])
return results
print(perebor(INPUT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment