Advertisement
luisnaranjo733

phone validation

Feb 18th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #-------------------------------------------------------------------------------
  4. # Name:        phone
  5. # Purpose:     A response to the easy python challenge - to parse telephone numbers and validate them.
  6. #
  7. # Author:      luis
  8. #
  9. # Created:     02/18/12
  10. # Copyright:   (c) luis 2012
  11. #-------------------------------------------------------------------------------
  12. type1 = [int,int,int,int,int,int,int,int,int,int] #10
  13. type2 = [int,int,int,str,int,int,int,str,int,int,int,int] #12
  14. type3 = [int,int,int,str,int,int,int,str,int,int,int,int] #12
  15. type4 = [str,int,int,int,str,int,int,int,str,int,int,int,int] #13
  16. type5 = [str,int,int,int,str,str,int,int,int,str,int,int,int,int] #14
  17. type6 = [int,int,int,str,int,int,int,int]
  18.  
  19. def convert(pn):
  20.     new = []
  21.     for each in pn:
  22.         try:
  23.             int(each)
  24.             new.append(int(each))
  25.         except:
  26.             new.append(each)
  27.  
  28.     return new
  29.            
  30. def fits(lpn,typeid):
  31.     for each in lpn:
  32.         current = lpn.index(each)
  33.         if type(each) != typeid[current]:
  34.             return False
  35.     return True
  36.    
  37. def valid(pn):
  38.     lpn = convert(pn)#list(pn)
  39.     lenpn = len(pn)
  40.     if lenpn <8 or lenpn > 14: #Invalidates anything out of the domain of possible phone number length before testing.
  41.         return False
  42.  
  43.     if lenpn == 8:
  44.         if fits(lpn,type6):
  45.             return True
  46.  
  47.         if not fits(lpn,type6):
  48.             return False
  49.  
  50.     if lenpn == 10: #Working
  51.         try:
  52.             pntype = [type(x) for x in lpn]
  53.         except:
  54.             return False
  55.         return pntype == type1
  56.  
  57.     if lenpn == 12: #Working for both
  58.         if fits(lpn,type2):
  59.             acceptable = ["-","."]
  60.             if lpn[3] in acceptable and lpn[7] in acceptable:
  61.                 return True
  62.         return False
  63.     if lenpn == 13: #Working
  64.         if fits(lpn,type4):
  65.             if lpn[0] == "(" and lpn[4] == ")" and lpn[8] == "-":
  66.                 return True
  67.         return False
  68.  
  69.     if lenpn == 14:
  70.         if fits(lpn,type5):
  71.             return True
  72.         return False
  73.  
  74.  
  75. def test():
  76.     valid_phone_numbers = "1234567890,123-456-7890,123.456.7890,(123)456-7890,(123) 456-7890,456-7890".split(",")
  77.     print "THESE SHOULD SUCCEED"
  78.     for phone_number in valid_phone_numbers:
  79.         assert valid(phone_number) == True
  80.         print "Is %s validated?\t%r" % (str(phone_number),valid(phone_number))
  81.  
  82.     invalid_phone_numbers = "123-45-6789 123:4567890 123/456-7890".split()
  83.     print "\nTHESE SHOULD FAIL:"
  84.     for phone_number in invalid_phone_numbers:
  85.         print "Is %s validated?\t%r" % (str(phone_number),valid(phone_number))
  86.        
  87.  
  88.    
  89. if __name__ == '__main__':
  90.     test()
  91.  
  92. """
  93. valid telephone numbers: 1234567890, 123-456-7890, 123.456.7890, (123)456-7890, (123) 456-7890 (note the white space following the area code), and 456-7890.
  94.  
  95. invalid telephone numbers: 123-45-6789, 123:4567890, and 123/456-7890.
  96. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement