python - Finding every permutation of character-substitutions in a word -
while may seem already-answered question, it's not.
i trying find way following:
- i have known string, e.g "i love dogs"
- i have bunch of possible characters, eg "@!470"
- i have bunch of characters should substitute in "known string", e.g s,e
- i want way substitute every interaction, in known string "i love dogs", 1 single character known in "possible characters", , let else unchanged.
- when finished, start substituting 2 characters @ time
- when finished, start substituting 3 characters @ time
- and on till have no more "possible characters"
an example of output expected described below:
- i have known string "this string"
- i have list of possible characters "!$"
- i have characters substitute "is"
first, program proceed changing every in !, having:
"th!s !s str!ng"
next, change s in $:
"thi$ i$ $tring"
now finished 1-character permutation , need start 2 characters permutations:
"th!$ !s $tr!ng"
since have 1 pair, have 1 possible permutation here, hence program finishes.
i trying figure out how in python, end in infinite list of "if .. then.." , there must more efficient way of doing it.
for curious, interested in because "lost" password. mean, password "i love ice cream", being responsible person am, changed of characters symbols , have no clue it...
this doesn't you're asking for, might help:
from itertools import product substitutions = { "i": "!1|", "o": "0", "s": "$5", } def sub(text): possibilities = [c + substitutions.get(c, "") c in text] # 'spoils' -> ['s$5', 'p', 'o0', 'i!1|', 'l', 's$5'] subbed in product(*possibilities): print("".join(subbed))
it uses itertools.product()
iterate on every possible combination of substitutions
text
:
>>> sub('spoils') spoils spoil$ spoil5 spo!ls spo!l$ spo!l5 spo1ls spo1l$ spo1l5 spo|ls spo|l$ spo|l5 sp0ils sp0il$ sp0il5 sp0!ls sp0!l$ sp0!l5 sp01ls sp01l$ sp01l5 sp0|ls sp0|l$ sp0|l5 $poils $poil$ $poil5 $po!ls $po!l$ $po!l5 $po1ls $po1l$ $po1l5 $po|ls $po|l$ $po|l5 $p0ils $p0il$ $p0il5 $p0!ls $p0!l$ $p0!l5 $p01ls $p01l$ $p01l5 $p0|ls $p0|l$ $p0|l5 5poils 5poil$ 5poil5 5po!ls 5po!l$ 5po!l5 5po1ls 5po1l$ 5po1l5 5po|ls 5po|l$ 5po|l5 5p0ils 5p0il$ 5p0il5 5p0!ls 5p0!l$ 5p0!l5 5p01ls 5p01l$ 5p01l5 5p0|ls 5p0|l$ 5p0|l5
i've reformatted output compactness, idea. obviously, number of candidate passwords going increase exponentially number of substitutions, there's not can that.
Comments
Post a Comment