Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. def is_valid(id: str):
  2.     """
  3.    Calculates whether an ID number if valid
  4.    """
  5.     if len(id) != 9:
  6.         return False
  7.     _sum = 0
  8.     for i in range(8):
  9.         if i % 2 == 0:
  10.             weight = 1
  11.         else:
  12.             weight = 2
  13.         value = int(id[i]) * weight
  14.         while value > 9:
  15.             value = value // 10 + value % 10
  16.         _sum += value
  17.  
  18.     if _sum % 10 == 0:
  19.         return int(id[8]) == 0
  20.     else:
  21.         return int(id[8]) == 10 - (_sum % 10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement