Skip to content

Instantly share code, notes, and snippets.

@dudanogueira
Last active March 5, 2024 18:50
Show Gist options
  • Save dudanogueira/7af722477c33bd4bb85843cf0e035b77 to your computer and use it in GitHub Desktop.
Save dudanogueira/7af722477c33bd4bb85843cf0e035b77 to your computer and use it in GitHub Desktop.
Validar CNS Cartão Nacional de Saúde em Python
#
# Autoria:
# Amaury Gonçalves <[email protected]>
# Duda Nogueira <[email protected]>
#
def validaCNS(numero):
numero = str(numero)
if numero.isdigit():
if re.match(r'[1-2]\d{10}00[0-1]\d$',numero) or re.match(r'[7-9]\d{14}$',numero):
return somaPoderada(numero) % 11 == 0
return False
def somaPoderada(numero):
i = 0
soma = 0
while i<len(numero):
soma = soma+int(numero[i]) * (15 - i)
i = i+1
return soma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment