Advertisement
STANAANDREY

lp corrector

May 1st, 2025
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.00 KB | None | 0 0
  1. import re
  2.  
  3. class LPCorrectorRo:
  4.     COUNTIES = [
  5.         "AB", "AR", "AG", "BC", "BH", "BN", "BT", "BR", "BV", "BZ", "CS", "CL", "CJ", "CT", "CV",
  6.         "DB", "DJ", "GL", "GR", "GJ", "HR", "HD", "IL", "IS", "IF", "MM", "MH", "MS", "NT", "OT",
  7.         "PH", "SM", "SJ", "SB", "SV", "TR", "TM", "TL", "VS", "VL", "VN", "B"
  8.     ]
  9.  
  10.     REPLACEMENTS = {
  11.         '0': 'O', 'O': '0',
  12.         '1': 'I', 'I': '1',
  13.         '2': 'Z', 'Z': '2',
  14.         '5': 'S', 'S': '5',
  15.         '8': 'B', 'B': '8'
  16.     }
  17.  
  18.     @staticmethod
  19.     def valid_number(nr):
  20.         for county in LPCorrectorRo.COUNTIES:
  21.             if nr.startswith(county):
  22.                 rest = nr[len(county):]
  23.                 if county == "B":
  24.                     if re.fullmatch(r"\d{2,3}[A-Z]{3}", rest):
  25.                         return True
  26.                 else:
  27.                     if re.fullmatch(r"\d{2}[A-Z]{3}", rest):
  28.                         return True
  29.         return False
  30.  
  31.     @classmethod
  32.     def correct_number(cls, nr):
  33.         nr = nr.replace(" ", "").upper()
  34.         if nr.startswith('RO'):
  35.             nr = nr[2:]
  36.         if cls.valid_number(nr):
  37.             return nr
  38.  
  39.         for i, c in enumerate(nr):
  40.             if c in cls.REPLACEMENTS:
  41.                 corrected = nr[:i] + cls.REPLACEMENTS[c] + nr[i + 1:]
  42.                 if cls.valid_number(corrected):
  43.                     return corrected
  44.  
  45.         for i, c in enumerate(nr):
  46.             if c in cls.REPLACEMENTS:
  47.                 corrected = nr[:i] + cls.REPLACEMENTS[c] + nr[i + 1:]
  48.                 for j, d in enumerate(corrected):
  49.                     if d in cls.REPLACEMENTS and j != i:
  50.                         double_corrected = corrected[:j] + cls.REPLACEMENTS[d] + corrected[j + 1:]
  51.                         if cls.valid_number(double_corrected):
  52.                             return double_corrected
  53.  
  54.         return nr
  55.  
  56.  
  57. def main():
  58.     s = 'RO B251w0 E '
  59.     s = LPCorrectorRo.correct_number(s)
  60.     print(s)
  61.  
  62.  
  63. if __name__ == '__main__':
  64.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement