Created
March 16, 2021 06:07
-
-
Save typelogic/b0648c42b8e17b25ff86782bc7141448 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def permutations(s, before="", replace="O", replace_by="0"): | |
""" | |
This function will replace every occurance of a character or string with another character or string. | |
In every possible setup, e.g. 'xxx' as input, where 'y' replaces 'x' will be return ['yxx', 'xyx', 'xxy', 'yyx', 'yxy', ... ] | |
Written by Jurien Vegter in order to fix an issue where the passporteye ocr mistakes '0's for 'O's | |
""" | |
if len(s) == len(before): | |
result = [] | |
elif replace in s[len(before):]: | |
pattern = r'(^{before}[^{replace}]*)({replace})(.*$)'.format(before=before, replace=replace) | |
replaced = re.sub(pattern, r'\1[]\3', s).replace("[]", replace_by) | |
result = [s, replaced] | |
before = re.sub(pattern, r'\1', s) | |
result.extend(permutations(s, before + replace, replace, replace_by)) | |
result.extend(permutations(replaced, before + replace_by, replace, replace_by)) | |
else: | |
result = [s] | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace doubtful character with its alternative possible value and then re-compute check digit?