Guest User

Untitled

a guest
Apr 20th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. """ This module has utility methods related to Singapore NRIC number"""
  2.  
  3. def check_nric_sg(nric):
  4. """ check if a nric is valid or not"""
  5. list_m = [2, 7, 6, 5, 4, 3, 2]
  6. list_st = list('JZIHGFEDCBA')
  7. list_fg = list('XWUTRQPNMLK')
  8.  
  9. if len(nric) != 9:
  10. return False
  11.  
  12. nric = nric.upper()
  13. if nric[0] not in ['S', 'T', 'F', 'G']:
  14. return False
  15.  
  16. list_nric = list(nric[1:8])
  17. result = sum([int(a)*b for a, b in zip(list_nric, list_m)])
  18.  
  19. if nric[0] == 'T' or nric[0] == 'G':
  20. result += 4
  21.  
  22. result_mod = result % 11
  23.  
  24. if nric[0] == "S" or nric[0] == "T":
  25. checksum = list_st[result_mod]
  26. elif nric[0] == "F" or nric[0] == "G":
  27. checksum = list_fg[result_mod]
  28.  
  29. return nric[8] == checksum
  30.  
  31. def fix_nric_sg(nric):
  32. """Fix the first char of the NRIC as many patients don't input that"""
  33. if not check_nric_sg(nric):
  34. for prefix in ['S', 'T', 'F', 'G']:
  35. nric_temp = prefix + nric
  36. if check_nric_sg(nric_temp):
  37. return nric_temp
  38. return False
Add Comment
Please, Sign In to add comment