如何计算巴拉圭国家税号的验证码

在巴拉圭(南美洲)国家,每个纳税人都有一个由政府(Ministerio de Hacienda, Secretaríade Tributación)分配的税号(称为 RUC:Registro Único del Contribuyente)。

此 RUC 是一个数字,后跟一个验证数字 (dígito verificador),例如123456-0。当您申请 RUC 时,政府会告诉您验证码。

有没有办法根据 RUC 计算验证码?它是一个已知的公式吗?

就我而言,我有一个供应商和客户数据库,多年来由公司的几名员工收集。现在我需要运行检查以查看所有 RUC 是否输入正确或是否有输入错误。

我的偏好是一个Python解决方案,但我会采取任何解决方案来为我指明正确的方向。


编辑:这是一个分享知识的自我答案,我花了几个小时/几天才找到。我将此问题标记为“回答您自己的问题”(不知道这是否会改变任何事情)。


慕尼黑的夜晚无繁华
浏览 179回答 1
1回答

MMTTMM

RUC 的验证数字是使用与称为 的方法非常相似(但不等于)的公式计算的Modulo 11;这至少是我在阅读以下技术网站时获得的信息(内容为西班牙语):https://www.yoelprogramador.com/funncion-para-calcular-el-digito-verificador-del-ruc/http://groovypy.wikidot.com/blog:02https://es.wikipedia.org/wiki/C%C3%B3digo_de_control#M.C3.B3dulo_11我分析了上述页面中提供的解决方案,并针对 RUC 列表及其已知验证数字运行了自己的测试,这使我得出了一个返回预期输出的最终公式,但与上述链接中的解决方案不同。我得到的计算 RUC 验证数字的最终公式如本例 ( 80009735-1) 所示:将 RUC 的每个数字(不考虑验证数字)乘以基于该数字在 RUC 内的位置(从 RUC 右侧开始)的因子,并将这些乘法的所有结果相加:RUC:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;9&nbsp; &nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp;5Position:&nbsp; &nbsp; &nbsp; &nbsp; 7&nbsp; &nbsp; &nbsp; &nbsp;6&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;0Multiplications: 8x(7+2) 0x(6+2) 0x(5+2) 0x(4+2) 9x(3+2) 7x(2+2) 3x(1+2) 5x(0+2)Results:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;72&nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;45&nbsp; &nbsp; &nbsp; 28&nbsp; &nbsp; &nbsp; 9&nbsp; &nbsp; &nbsp; &nbsp;10Sum of results:&nbsp; 164将总和除以11并使用除法的余数来确定验证数字:如果余数大于1,则校验位为11 - remainder若余数为0或1,则校验位为0在输出示例中:Sum of results:&nbsp; &nbsp; &nbsp; &nbsp;164Division:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;164 / 11&nbsp; &nbsp; ==>&nbsp; &nbsp;quotient 14, remainder 10Verification digit:&nbsp; &nbsp;11 - 10&nbsp; &nbsp; &nbsp;==>&nbsp; &nbsp;1这是我Python的公式版本:def calculate_dv_of_ruc(input_str):&nbsp; &nbsp; # assure that we have a string&nbsp; &nbsp; if not isinstance(input_str, str):&nbsp; &nbsp; &nbsp; &nbsp; input_str = str(input_str)&nbsp; &nbsp; # try to convert to 'int' to validate that it contains only digits.&nbsp; &nbsp; # I suspect that this is faster than checking each char independently&nbsp; &nbsp; int(input_str)&nbsp; &nbsp; the_sum = 0&nbsp; &nbsp; for i, c in enumerate(reversed(input_str)):&nbsp; &nbsp; &nbsp; &nbsp; the_sum += (i + 2) * int(c)&nbsp; &nbsp; base = 11&nbsp; &nbsp; _, rem = divmod(the_sum, base)&nbsp; &nbsp; if rem > 1:&nbsp; &nbsp; &nbsp; &nbsp; dv = base - rem&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; dv = 0&nbsp; &nbsp; return dv测试这个函数会返回预期的结果,当输入的字符不是数字时会引发错误:>>> calculate_dv_of_ruc(80009735)1>>> calculate_dv_of_ruc('80009735')1>>> calculate_dv_of_ruc('80009735A')Traceback (most recent call last):&nbsp; File "<input>", line 1, in <module>&nbsp; File "<input>", line 8, in calculate_dv_of_rucValueError: invalid literal for int() with base 10: '80009735A'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python