擅长:python、mysql、java
<p>这是一个简短的版本</p>
<pre><code>def check(vat_num):
if len(vat_num) != 9 or vat_num[0] not in "ABCDEFGHIJKLMNPQRSVW":
return False # trivial checks first
nums = list(map(int, vat_num[1:8])) # numbers to check
nums[1::2] = ((i * 2) - (9 if i > 4 else 0) for i in nums[1::2]) # alter odds
return int(vat_num[8]) == (sum(nums) % 10) # compare to check digit
</code></pre>
<p>请注意以下有趣的*功能:</p>
<ol>
<li>使用<code>[1::2]</code>从iterable中获取所有其他项</li>
<li>使用<code>map(int, ...)</code>将interable中的所有项转换为整数;以及</li>
<li>使用<code>% 10</code>(模)可以很容易地得到数字的位数部分。在</li>
</ol>
<p>*可能不有趣</p>