Wednesday, November 27, 2013

groovy cusips

Some sample code for verifying cusips


// Note must map these character specifically since ascii mapping does not work
//println "*#@ = "+((int)('*'-'A'))+", "+((int)('#'-'A'))+", "+((int)('@'-'A'))

// sample cusips taken from http://www.ksvali.com/2009/02/security-ids-symbol-cusip-isin-sedol-ric-code/
def cusips = [  "14149YAR9", // - Corporate Bond - Cardinal Health Inc
                "126650BG4", // - Corporate Bond -CVS Caremark Corp
                "254709AC2", // - Corporate Bond -Discover Finl Services
                "437076AQ5", // - Corporate Bond -Home Depot Inc
                "441060AG5", // - Corporate Bond -Hospira Inc
                "50075NAN4", // - Corporate Bond -Kraft Foods Inc
                "574599BE5", // - Corporate Bond -Masco Corp
                "617446B99", // - Corporate Bond -Morgan Stanley
                "637640AC7", // - Corporate Bond -Natl Semicon Corp
                "713291AL6", // - Corporate Bond -Pepco Hldgs Inc
                "852061AE0", // - Corporate Bond -Sprint Nextel Corp
                "887317AA3", // - Corporate Bond -Time Warner Inc
                "925524BF6", // - Corporate Bond -Viacom
                "125509BG3", // - Corporate Bond -Cigna Corp
                "125896AV2"] // - Corporate Bond -CMS Engy Corp

cusips.each(){
    println "Verifying $it "+verifyCusip(it)   
}

public boolean verifyCusip(cusip){
   int check = getCheckCode(cusip.trim().toUpperCase())
   //println "  CheckCode = "+check)
   //println "Checking $check == "+cusip.charAt(cusip.size()-1)
   return check==((int)cusip.charAt(cusip.size()-1)-(int)'0')
}

/**
 *Code based on algorithm at https://en.wikipedia.org/wiki/CUSIP
 */
public int getCheckCode(String cusip){
   int sum = 0
   int v,p
   for(i=0;i<8;i++){
      char c = cusip.charAt(i)
      if(Character.isDigit(c))
         v = (int)c-(int)'0'
      else if(Character.isLetter(c))
         v = (int)c - (int)'A'+10
      else if(c == '*')
         v = 36
      else if(c == '@')
         v = 37
      else if(c == '#')
         v = 38
      if(i%2!=0) // if i NOT even   N.B. since we count from 0 it is not even.. If we count from 1 it is even
         v *= 2
      //println "  for letter $c val = $v"
      sum += v/10 + (v %10)
   }
  
   return (10 - (sum % 10)) % 10
}

Monday, November 04, 2013

7 zip

7zip is a nice free ware zip program.

For some reason my install had failed to get it opening .zip files by default. (Despite setting it multiple times via the 7zip options page.)

Heres how I fixed it (thanks winzip).. http://kb.winzip.com/kb/entry/155/

Also worth noting, is that it is good at deleting files and directories that other programs (including windows explorer) can't. I have had to install it, in order to delte some stubborn files that were lying around.