Skip to content

Instantly share code, notes, and snippets.

@lichti
Created November 11, 2020 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lichti/e9b22d96446f068eda4092be113fd15c to your computer and use it in GitHub Desktop.
Save lichti/e9b22d96446f068eda4092be113fd15c to your computer and use it in GitHub Desktop.
Validate Brazilian RG
###
# Explain
# RG Number -> AB.CDE.FGH-V
# X=(Hx9)+(Gx8)+(Fx7)+(Ex6)+(Dx5)+(Cx4)+(Bx3)+(Ax2)+(100xV)
# Y= X%11
# If Y (rest of division) is 0 the RG is Valid
###
# Function to validate Brazilian "RG"
def isValidRG(number,dv):
multiplicator=9
acumulator=0
for n in number[::-1]:
acumulator+=int(n)*multiplicator
multiplicator-=1
if dv.lower() == "x":
dv=10
else:
dv=int(dv)
acumulator+=100*dv
if (acumulator%11) == 0:
return True
return False
####
import sys
# Read RG from first argument
rg=sys.argv[1]
# Get RG number and remove dot if exists
rgNumber=rg.split('-')[0].replace('.','')
# Get a Validator Digit
rgDV=rg.split('-')[1]
# Return Valid or not valid RG number using isValidRG Function
if isValidRG(rgNumber,rgDV):
print("RG Válido")
else:
print("RG Invalido")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment