Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. def numberOfDigits(n):
  2.     tot = 0
  3.     while n:
  4.         n/=10
  5.         tot+=1
  6.     return tot
  7. def isValid(n):
  8.     n_digits = numberOfDigits(n)
  9.     if(n_digits < 13):
  10.         return False
  11.     if(n_digits > 16):
  12.         return False
  13.     # It is not one of the 4 credit card brands
  14.     div = 10 ** (n_digits-1)
  15.     div2 = 10 ** (n_digits-2)
  16.     if (n / div != 4) and (n / div != 5) and (n / div != 6) and (n / div2 != 37):
  17.         return False
  18.     aux = n / 10
  19.     tot = 0
  20.     # steps 1 and 2
  21.     while(aux):
  22.         digit = aux % 10
  23.         digit = digit * 2
  24.         digit = digit/10 + digit % 10
  25.         tot += digit
  26.         aux /= 100
  27.        
  28.     # step 3 nad 4
  29.     aux = n
  30.     while(aux):
  31.         tot += aux % 10
  32.         aux /= 100
  33.        
  34.     if(tot % 10 == 0):
  35.         return True
  36.     return False
  37.    
  38. print isValid(100)
  39. print isValid(4388576018410707)
  40. print isValid(4388576018402626)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement