def spanishNIFNIECIFhelper(cid):
<EOL>cid = cid.upper()<EOL>if len(cid) > <NUM_LIT:9>:<EOL><INDENT>return -<NUM_LIT:4><EOL><DEDENT>compiled = re.compile('<STR_LIT>')<EOL>if not compiled.match(cid):<EOL><INDENT>return -<NUM_LIT:5><EOL><DEDENT>num = '<STR_LIT>'<EOL>for i in range(<NUM_LIT:0>, len(cid)):<EOL><INDENT>num += cid[i]<EOL><DEDENT>compiled = re.compile('<STR_LIT>')<EOL>if compiled.match(cid):<EOL><INDENT>index = int(cid[<NUM_LIT:0>:<NUM_LIT:8>]) % <NUM_LIT><EOL>string = '<STR_LIT>'<EOL>if (num[<NUM_LIT:8>] == string[index]):<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT><DEDENT>suma = int(num[<NUM_LIT:2>]) + int(num[<NUM_LIT:4>]) + int(num[<NUM_LIT:6>])<EOL>for i in range(<NUM_LIT:1>, len(num), <NUM_LIT:2>):<EOL><INDENT>codigo = str(<NUM_LIT:2> * int(num[i]))<EOL>a = int(codigo[<NUM_LIT:0>])<EOL>if len(codigo) == <NUM_LIT:2>:<EOL><INDENT>b = int(codigo[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>b = <NUM_LIT:0><EOL><DEDENT>suma += (a+b)<EOL><DEDENT>n = <NUM_LIT:10> - int(str(suma)[-<NUM_LIT:1>])<EOL>compiled = re.compile('<STR_LIT>')<EOL>if compiled.match(cid):<EOL><INDENT>if num[<NUM_LIT:8>] == chr(<NUM_LIT:64> + n):<EOL><INDENT>return <NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>return -<NUM_LIT:1><EOL><DEDENT><DEDENT>compiled = re.compile('<STR_LIT>')<EOL>if compiled.match(cid):<EOL><INDENT>if (num[<NUM_LIT:8>] == chr(<NUM_LIT:64> + n)) or (num[<NUM_LIT:8>] == str(n)[-<NUM_LIT:1>]):<EOL><INDENT>return <NUM_LIT:2><EOL><DEDENT>else:<EOL><INDENT>return -<NUM_LIT:2><EOL><DEDENT><DEDENT>compiled = re.compile('<STR_LIT>')<EOL>if compiled.match(cid):<EOL><INDENT>if num[<NUM_LIT:8>] == len(re.split('<STR_LIT>', cid)[<NUM_LIT:0>]):<EOL><INDENT>return <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>return -<NUM_LIT:3><EOL><DEDENT><DEDENT>compiled = re.compile('<STR_LIT>')<EOL>if compiled.match(cid):<EOL><INDENT>string = '<STR_LIT>'<EOL>cid = cid.replace('<STR_LIT:X>', '<STR_LIT:0>')<EOL>cid = cid.replace('<STR_LIT:Y>', '<STR_LIT:1>')<EOL>cid = cid.replace('<STR_LIT>', '<STR_LIT:2>')<EOL>if num[<NUM_LIT:8>] == string[int(cid[<NUM_LIT:0>:<NUM_LIT:8>]) % <NUM_LIT>]:<EOL><INDENT>return <NUM_LIT:3><EOL><DEDENT>else:<EOL><INDENT>return -<NUM_LIT:3><EOL><DEDENT><DEDENT>return -<NUM_LIT:6><EOL>
Given a spanish NIF, CIF or NIE, it returns:

1 for a valid NIF
2 for a valid CIF
3 for a valid NIE
-1 for an invalid NIF (it looks like NIF but it doesn't validate the CRC)
-2 for an invalid CIF (it looks like CIF but it doesn't validate the CRC)
-3 for an invalid NIE (it looks like NIE but id doesn't validate the CRC)
-4 not a valid NIF/NIE/CIF because it is too long
-5 not a valid NIF/NIE/CIF because it doesn't have a valid format
-6 it can not recognize if it is NIF, CIF or NIE, conclusion: the given CID is incorrect.