It is a module that verifies My Number with pip installable Python. We are doing a cross check with the ruby version and 10,000 cases. The article Calculate the check digit of My Number was wonderful, so I was able to make a Python version by referring to it.
install
install
pip install mynumber
https://pypi.python.org/pypi/mynumber
sample code
install
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from mynumber import MyNumber
#My number verification
my_number = 123456789018
print MyNumber.validate(my_number)
#More my number verification
for my_number in MyNumber.gets(1000):
assert MyNumber.validate(my_number)
#My number verification by iterator:There is duplication
for my_number in MyNumber():
assert MyNumber.validate(my_number)
○ Ministry of Internal Affairs and Communications Ordinance No. 85
$ Pn $: The number of the $ n $ digit when the least significant digit of the 11-digit number other than the inspection digit that constitutes the personal number is the $ 1 $ digit $ Qn $: $ n + 1 $ when $ 1 ≤ n ≤ 6 $, $ n-5 $ when $ 7 ≤ n ≤ 11 $
Articles of Original and Calculate Check Digit of My Number I quoted the formula of.
-Ruby --Calculate my number check digit -Calculate the check digit of My Number in C ++
I cross-checked with the code of the article Calculate the check digit of my number.
ruby_mynumber_validate.rb
#!/usr/bin/env ruby
def validate_my_number(mynumber)
#Integer sequence
digits = mynumber.to_s.chars.map(&:to_i)
#Only 12 digits allowed
return false unless digits.length == 12
#Separate check digit
check_digit = digits.pop
#Check the remaining numbers from the smallest
digits.reverse!
#Calculate the remainder of dividing the sum of the sequences by 11.
remainder = (1..11).inject(0) {|sum, i|
p = digits[i-1]
q = (i <= 6) ? i+1 : i-5
sum + p*q
} % 11
case remainder
when 0,1
check_digit === 0
else
check_digit === (11 - remainder)
end
end
my_numbers = [
xxxxx,
xxxxx,
xxxxx,
xxxxx,
]
my_numbers.each do |num|
r = validate_my_number(num)
if r == false then
p num
end
end
Recommended Posts