forked from victorgau/CompanySerialNo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialNoChecker.py
More file actions
77 lines (60 loc) · 1.86 KB
/
Copy pathSerialNoChecker.py
File metadata and controls
77 lines (60 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 12 11:59:35 2015
@author: victor
"""
def company_serial_no_checker(serial):
# 共八位,全部為數字型態
if len(serial)!=8 or not serial.isdigit():
return False
# 各數字分別乘以 1,2,1,2,1,2,4,1
# 例:統一編號為 53212539
#
# Step1 將統編每位數乘以一個固定數字固定值
# 5 3 2 1 2 5 3 9
# x 1 2 1 2 1 2 4 1
# ================================
# 5 6 2 2 2 10 12 9
#
serial_list = list(serial)
coeff = [1, 2, 1, 2, 1, 2, 4, 1]
# print serial_list
# Define a list with 8 zeros
multiple = [0]*8
count = 0
while count < 8:
# print count
temp = int(serial_list[count])*coeff[count]
multiple[count] = temp if temp < 10 else (temp/10) + (temp % 10)
count = count + 1
# Step2 將所得值取出十位數及個位數
# 十位數 個位數
# 0 5
# 0 6
# 0 2
# 0 2
# 0 2
# 1 0
# 1 2
# 0 9
#
# 並將十位數與個位數全部結果值加總
#
total = sum(multiple)
# Step3 判斷結果
# 第一種:加總值取10的餘數為0
# 第二種:加總值取10的餘數等於9而且統編的第6碼為7
if (total % 10 == 0):
return True
elif ((total + 1) % 10 == 0 and serial[5] == '7'):
return True
else:
return False
if __name__=='__main__':
# 位數不足
#print company_serial_no_checker('5312539') # false
# 符合
#print company_serial_no_checker('53212539') # true
# 不符合
#print company_serial_no_checker('12222539') # nil, 在邏輯判斷的時候也會被當false
print company_serial_no_checker('27206130')