Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class LPCorrectorRo:
- COUNTIES = [
- "AB", "AR", "AG", "BC", "BH", "BN", "BT", "BR", "BV", "BZ", "CS", "CL", "CJ", "CT", "CV",
- "DB", "DJ", "GL", "GR", "GJ", "HR", "HD", "IL", "IS", "IF", "MM", "MH", "MS", "NT", "OT",
- "PH", "SM", "SJ", "SB", "SV", "TR", "TM", "TL", "VS", "VL", "VN", "B"
- ]
- REPLACEMENTS = {
- '0': 'O', 'O': '0',
- '1': 'I', 'I': '1',
- '2': 'Z', 'Z': '2',
- '5': 'S', 'S': '5',
- '8': 'B', 'B': '8'
- }
- @staticmethod
- def valid_number(nr):
- for county in LPCorrectorRo.COUNTIES:
- if nr.startswith(county):
- rest = nr[len(county):]
- if county == "B":
- if re.fullmatch(r"\d{2,3}[A-Z]{3}", rest):
- return True
- else:
- if re.fullmatch(r"\d{2}[A-Z]{3}", rest):
- return True
- return False
- @classmethod
- def correct_number(cls, nr):
- nr = nr.replace(" ", "").upper()
- if nr.startswith('RO'):
- nr = nr[2:]
- if cls.valid_number(nr):
- return nr
- for i, c in enumerate(nr):
- if c in cls.REPLACEMENTS:
- corrected = nr[:i] + cls.REPLACEMENTS[c] + nr[i + 1:]
- if cls.valid_number(corrected):
- return corrected
- for i, c in enumerate(nr):
- if c in cls.REPLACEMENTS:
- corrected = nr[:i] + cls.REPLACEMENTS[c] + nr[i + 1:]
- for j, d in enumerate(corrected):
- if d in cls.REPLACEMENTS and j != i:
- double_corrected = corrected[:j] + cls.REPLACEMENTS[d] + corrected[j + 1:]
- if cls.valid_number(double_corrected):
- return double_corrected
- return nr
- def main():
- s = 'RO B251w0 E '
- s = LPCorrectorRo.correct_number(s)
- print(s)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement