S = Sum(Ai * Wi), i=0,.......16 (现在的身份证号码都是18位长,其中最后一位是校验位,15位的身份证号码好像不用了)
Ai对应身份证号码,Wi则为用于加权计算的值,它一串固定的数值,应该是根据某种规则得出的吧,用于取得最好的随机性,Wi的取之如下:
7 9 10 5
8 4 2 1
6 3 7 9
10 5 8 4 2
经过加权计算之后,得到一个S,用这个S去模11,取余值,然后查表得到校验位,这个索引表如下:
0 ----- 1
1 ----- 0
2 ----- x
3 ----- 9
4 ----- 8
5 ----- 7
6 ----- 6
7 ----- 5
8 ----- 4
9 ----- 3
10 ----- 2
程序代码如下:
import sys Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7,9, 10, 5, 8, 4, 2] IndexTable = { #此处实际是无需使用字典的,使用一个包含11个元素的数组便可,数组中存放 0 : '1', #相应位置的号码,但是这也正好演示了Python高级数据结构的使用 1 : '0', 2 : 'x', 3 : '9', 4 : '8', 5 : '7', 6 : '6', 7 : '5', 8 : '4', 9 : '3', 10 : '2' } No = [] sum = 0 if (len(sys.argv[1:][0]) != 17): print "error number" sys.exit() for x in sys.argv[1:][0]: No.append(x) for i in range(17): sum = sum + (int(No[i]) * Wi[i]) Index = sum % 11 print "So, your indicates parity is : %s" % (IndexTable[Index])
运行程序方式如下:
#python getParity.py your-indentity-number-but-except-the-last-number
我的天啊,Python内置的数据结构是如此强大而易用,越来越为之而着迷啊,继续diving~
用函数封装一下,改进的代码如下:
import sys if __name__ != '__main__': print "Cannot run in module" sys.exit() Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7,9, 10, 5, 8, 4, 2] IndexTable = { 0 : '1', 1 : '0', 2 : 'x', 3 : '9', 4 : '8', 5 : '7', 6 : '6', 7 : '5', 8 : '4', 9 : '3', 10 : '2' } def check(identity): if(len(identity) == 0): print "please input your identity number" sys.exit() elif (len(identity[0]) != 17): print "error number" sys.exit() def calculate(identity): No = [] sum = 0 for x in identity[0]: #这个方法是很笨拙的,直接使用No = list(identity[0])便可达到同样的目的 No.append(x) for i in range(17): sum = sum + (int(No[i]) * Wi[i]) Index = sum % 11 return IndexTable[Index] check(sys.argv[1:]) result = calculate(sys.argv[1:]) print "So, your indicates parity is : %s" % (result)
忘记函数原型吧,这里不需要指明返回值类型,不需要指明形参数据类型。

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.

The article discusses unit tests in Python, their benefits, and how to write them effectively. It highlights tools like unittest and pytest for testing.

Article discusses access specifiers in Python, which use naming conventions to indicate visibility of class members, rather than strict enforcement.

Article discusses Python's \_\_init\_\_() method and self's role in initializing object attributes. Other class methods and inheritance's impact on \_\_init\_\_() are also covered.

The article discusses the differences between @classmethod, @staticmethod, and instance methods in Python, detailing their properties, use cases, and benefits. It explains how to choose the right method type based on the required functionality and da

InPython,youappendelementstoalistusingtheappend()method.1)Useappend()forsingleelements:my_list.append(4).2)Useextend()or =formultipleelements:my_list.extend(another_list)ormy_list =[4,5,6].3)Useinsert()forspecificpositions:my_list.insert(1,5).Beaware


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
