Skip to content

Instantly share code, notes, and snippets.

@typelogic
Created March 16, 2021 06:07
Show Gist options
  • Save typelogic/b0648c42b8e17b25ff86782bc7141448 to your computer and use it in GitHub Desktop.
Save typelogic/b0648c42b8e17b25ff86782bc7141448 to your computer and use it in GitHub Desktop.
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
@typelogic
Copy link
Author

Replace doubtful character with its alternative possible value and then re-compute check digit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment