Mainly Tech projects on Python and Electronic Design Automation.

Sunday, August 03, 2008

SEDOL


Whilst browsing Rosetta Code, I came across a good algorithm for training students, SEDOL, so coded up a version in what I would call a very litteral translation of  itsexplanation.
I changed the Python example on Wikipedia, as it did not have a validation function as other examples have, and added my code to the Rosetta page.
'''
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 \
          &nbspall(ch in sedolchars for ch in sedol) and \
          &nbspsum(sedol2value[ch] * sedolweight[n]
               for n,ch in enumerate(sedol)
               ) % 10 == 0
def checksum(sedol):
   &nbsptmp = sum(sedol2value[ch] * sedolweight[n]
               for n,ch in enumerate(sedol[:6])
               )
    return sedolchars[ (10 - (tmp % 10)) % 10]

sedol = '0263494'
print 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)
Note:
 I came back to the code after a few hours of sleep and thought initially that I would have trouble in the return statement of  checksum() as I thought I would need a dictionary doing the full reverse matching of what sedol2value, from ints to characters. sedolchars nolonger includes the vowels so using that method for a value over 9 will not work.
Luckily, the result of the calculation is always in the range 0<= n >=9, and within that range, sedolchars[n] works.

4 comments:

  1. Your code is much appreciated. What brought you to Rosetta Code?

    ReplyDelete
  2. "
    Interwibble - the Final Frontier. These are the voyages of the Paddy3118. My five-year mission: To explore strange new sites. To seek out new blogs and new blog communities. To boldly go where no man has gone before. Then politely introduce Python.
    "

    ReplyDelete
  3. I think (10 - (i % 10)) % 10 is the same as -i % 10.

    ReplyDelete
  4. Hi Peter,
    I think I remember a comment to that effect in a Rosetta Code example in another language (or was it on Wikipedia). But thanks for the comment but I'll leave it that way as that is what was mentioned in the description and a large part of the exercise for me was in concentrating on getting the code out of the textual description faithfully.
    I've already made a comment on the Rosetta Code talk page that several of the examples forgot the bit about English vowels not appearing in valid SEDOl.

    - Paddy.

    ReplyDelete

Followers

Subscribe Now: google

Add to Google Reader or Homepage

Go deh too!

whos.amung.us

Blog Archive