Last active
October 19, 2016 01:48
-
-
Save takuya/293edd55a6b2da94eb2d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
module CarLicenseNumber | |
def area_code_list | |
[["北海道", 10], | |
["東北地方", 20], | |
["東京都", 30], | |
["関東甲信越", 40], | |
["北部中部", 50], | |
["近畿", 60], | |
["中国", 70], | |
["四国", 80], | |
["九州", 90]] | |
end | |
def pref_code_list | |
[ | |
["北海道本部","10"], | |
["函館","11"], | |
["旭川","12"], | |
["釧路","13"], | |
["北見","14"], | |
["青森","20"], | |
["岩手","21"], | |
["宮城","22"], | |
["秋田","23"], | |
["山形","24"], | |
["福島","25"], | |
["東京","30"], | |
["茨城","40"], | |
["栃木","41"], | |
["群馬","42"], | |
["埼玉","43"], | |
["千葉","44"], | |
["神奈川","45"], | |
["新潟","46"], | |
["山梨","47"], | |
["長野","48"], | |
["静岡","49"], | |
["富山","50"], | |
["石川","51"], | |
["福井","52"], | |
["岐阜","53"], | |
["愛知","54"], | |
["三重","55"], | |
["滋賀","60"], | |
["京都","62"], | |
["大阪","61"], | |
["兵庫","63"], | |
["奈良","64"], | |
["和歌山","65"], | |
["鳥取","70"], | |
["島根","71"], | |
["岡山","72"], | |
["広島","73"], | |
["山口","74"], | |
["徳島","80"], | |
["香川","81"], | |
["愛媛","82"], | |
["高知","83"], | |
["福岡","90"], | |
["佐賀","91"], | |
["長崎","92"], | |
["熊本","93"], | |
["大分","94"], | |
["宮崎","95"], | |
["鹿児島","96"], | |
["沖縄","97"] | |
].inject({}){|ret,e| ret[e[1]] = e[0] ;ret } | |
end | |
def parse( card_number ) | |
raise "免許証番号は12桁" if (card_number =~ /[0-9]{12}/).nil? | |
year = card_number.chars[2,2].join.to_i | |
if year <= 50 | |
year=year+2000 | |
else | |
year=year+1900 if year > 50 | |
end | |
{ | |
"発行都道府県" => pref_code_list[card_number.chars[0,2].join], | |
"免許取得年" => year, | |
"再発行回数" => card_number.chars.last.to_i, | |
"チェックデジット" => check_digit(card_number) | |
} | |
end | |
def check_digit( card_number ) | |
num = card_number.chars[0,10].map{|e| e.to_i } | |
x = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2].zip(num).map{|e| e[0]*e[1] }.inject(0){|ret,e| ret=ret+e } | |
y = 11-(x%11) | |
(card_number[10].to_i == y ) ? "OK" : "NG" | |
end | |
module_function :parse,:pref_code_list,:check_digit | |
end | |
if $0 == __FILE__ then | |
cln = CarLicenseNumber | |
p cln.parse("621400099920") | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment