-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_word_variations.py
34 lines (23 loc) · 1.08 KB
/
generate_word_variations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import itertools
def generate_variations(template_str, replace_with_chars):
"""Generate variations of a string with certain characters substituted.
All instances of the '*' character in the template_str parameter are
substituted by characters from the replace_with_chars string. This function
generates the entire set of possible permutations."""
count = template_str.count('*')
_template_str = template_str.replace('*', '{}')
variations = []
for element in itertools.product(*itertools.repeat(list(replace_with_chars), count)):
variations.append(_template_str.format(*element))
return variations
if __name__ == '__main__':
# use this set to test
REPLACE_CHARS = '!@#$%^&*'
# excuse the bad language...
a = generate_variations('sh*t', REPLACE_CHARS)
b = generate_variations('s**t', REPLACE_CHARS)
c = generate_variations('s***', REPLACE_CHARS)
d = generate_variations('f*ck', REPLACE_CHARS)
e = generate_variations('f**k', REPLACE_CHARS)
f = generate_variations('f***', REPLACE_CHARS)
print list(set(a+b+c+d+e+f))