Will check SEDOL numbers as well as generate a SEDOL checksum digit.
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 | '''
Copied by author from: http://paddy3118.blogspot.com/2008/08/sedol.html
From: http://en.wikipedia.org/wiki/SEDOL
SEDOLs are seven characters in length, consisting of two parts:
a six-place alphanumeric code and a trailing check digit.
'''
import string
# constants
sedolchars = string.digits + string.ascii_uppercase
sedol2value = dict((ch, n) for n,ch in enumerate(sedolchars))
for ch in 'AEIOU':
del sedol2value[ch]
sedolchars = sorted(sedol2value.keys())
sedolweight = [1,3,1,7,3,9,1]
def check(sedol):
return len(sedol) == 7 and \
all(ch in sedolchars for ch in sedol) and \
sum(sedol2value[ch] * sedolweight[n]
for n,ch in enumerate(sedol)
) % 10 == 0
def checksum(sedol):
tmp = sum(sedol2value[ch] * sedolweight[n]
for n,ch in enumerate(sedol[:6])
)
return sedolchars[ (10 - (tmp % 10)) % 10]
sedol = '0263494'
print sedol, check(sedol), checksum(sedol)
print
# From: http://www.rosettacode.org/wiki/SEDOL
for sedol in '''
710889
B0YBKJ
406566
B0YBLH
228276
B0YBKL
557910
B0YBKR
585284
B0YBKT
'''.split():
print sedol + checksum(sedol)
|
No attempt was made to be fast. I concentrated on following the description. (The check() function for example correctly makes sure that the English vowels are not present).
Tags: security