{"id":3093,"date":"2013-09-11T09:18:33","date_gmt":"2013-09-11T01:18:33","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=3093"},"modified":"2013-09-11T09:18:33","modified_gmt":"2013-09-11T01:18:33","slug":"%e4%bf%a1%e7%94%a8%e5%8d%a1%e6%a0%a1%e9%aa%8c%e7%ae%97%e6%b3%95%ef%bc%9aluhn%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/3093.html","title":{"rendered":"\u4fe1\u7528\u5361\u6821\u9a8c\u7b97\u6cd5\uff1aLuhn\u7b97\u6cd5"},"content":{"rendered":"<p><span style=\"font-size: small;\">LUHN\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u6765\u8ba1\u7b97\u4fe1\u7528\u5361\u7b49\u8bc1\u4ef6\u53f7\u7801\u7684\u5408\u6cd5\u6027\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">1\u3001\u4ece\u5361\u53f7\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u5f00\u59cb\uff0c\u9006\u5411\u5c06\u5947\u6570\u4f4d(1\u30013\u30015\u7b49\u7b49)\u76f8\u52a0\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">2\u3001\u5c06\u5076\u6570\u4f4d\u6570\u5b57\u76f8\u52a0\uff0c\u4f46\u662f\u8fd9\u91cc\u6709\u4e2a\u9ebb\u70e6\u3002\u5fc5\u987b\u5148\u5c06\u6570\u5b57\u4e58\u4ee52\uff0c\u5982\u679c\u7ed3\u679c\u662f\u4e24\u4f4d\u6570\uff0c\u5c06\u4e24\u4e2a\u4f4d\u4e0a\u6570\u5b57\u76f8\u52a0\u3002\u7136\u540e\u5c06\u8fd9\u4e9b\u7ed3\u679c\u52a0\u5165\u603b\u548c\u4e2d\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">3\u3001\u5c06\u5947\u6570\u4f4d\u603b\u548c\u52a0\u4e0a\u5076\u6570\u4f4d\u603b\u548c\uff0c\u5982\u679c\u4fe1\u7528\u5361\u53f7\u7801\u662f\u5408\u6cd5\u7684\uff0c\u7ed3\u679c\u5e94\u8be5\u53ef\u4ee5\u88ab10\u6574\u9664\u3002<\/span><\/p>\n<p><span style=\"font-size: small;\">Python\u4ee3\u7801\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nLUHN_ODD_LOOKUP = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) # sum_of_digits(index * 2)\r\n\r\ndef luhn(candidate):\r\n    &quot;&quot;&quot;\r\n    Checks a candidate number for validity according to the Luhn\r\n    algorithm (used in validation of, for example, credit cards).\r\n    Both numeric and string candidates are accepted.\r\n    &quot;&quot;&quot;\r\n    if not isinstance(candidate, basestring):\r\n        candidate = str(candidate)\r\n    try:\r\n        evens = sum(&#x5B;int(c) for c in candidate&#x5B;-1::-2]])\r\n        odds = sum(&#x5B;LUHN_ODD_LOOKUP&#x5B;int(c)] for c in candidate&#x5B;-2::-2]])\r\n        return ((evens + odds) % 10 == 0)\r\n    except ValueError:  # Raised if an int conversion fails\r\n        return False\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>LUHN\u7b97\u6cd5\uff0c\u4e3b\u8981\u7528\u6765\u8ba1\u7b97\u4fe1\u7528\u5361\u7b49\u8bc1\u4ef6\u53f7\u7801\u7684\u5408\u6cd5\u6027\u3002 1\u3001\u4ece\u5361\u53f7\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u5f00\u59cb\uff0c\u9006\u5411\u5c06\u5947\u6570\u4f4d(1\u30013\u30015\u7b49\u7b49 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-3093","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/3093\/revisions\/3094"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}