Task

For each number list of 6-digit SEDOLs, calculate and append the checksum digit.



That is, given this input:

710889
B0YBKJ
406566
B0YBLH
228276
B0YBKL
557910
B0YBKR
585284
B0YBKT
B00030

Produce this output:

7108899
B0YBKJ7
4065663
B0YBLH2
2282765
B0YBKL9
5579107
B0YBKR5
5852842
B0YBKT7
B000300

Extra credit

Check each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string.



Related tasks

   Luhn test
   ISIN


//Return the code corresponding to a given character.
//ActionScript does not have a character type, so 1-digit strings
//are used instead
function toSEDOLCode(char:String):uint {
	//Make sure only a single character was sent.
	if(char.length != 1)
		throw new Error("toSEDOL expected string length of 1, got " + char.length);
	//Character is uppercase
	if (char >= "A" && char <= "Z") {
		return SEDOL.charCodeAt() + 10 - "A".charCodeAt();
	}
	//Character is numeric
	else if (char >= "0" && char <= "9"){
		return uint(char);
	}
	//Error: character is neither numeric nor uppercase
	else{
		throw new Error("toSEDOLCode expected numeric or uppercase character, recieved " + char);
	}
}
//Calculate the weighted sum for the SEDOL.
function toSum(str:String):uint {
	if(str.length != 6)
		throw new Error("toSum expected length 6, recieved " + str.length);
	var sum:uint=0;
	for (var i:uint = 0; i < str.length; i++) 
		sum+=toSEDOLCode(str.charAt(i))*[1,3,1,7,3,9][i];
	return sum;
}
//Calculate the check digit from the weighted sum.
function toCheck(num:int):uint
{
	return (10 -(num % 10)) % 10;
}
//Print the SEDOL with the check digit added.
function printWithCheck(SEDOL:String):void
{
	trace(SEDOL + toCheck(toSum(SEDOL)));
}
printWithCheck("710889");
printWithCheck("B0YBKJ");
printWithCheck("406566");
printWithCheck("B0YBLH");
printWithCheck("228276");
printWithCheck("B0YBKL");
printWithCheck("557910");
printWithCheck("B0YBKR");
printWithCheck("585284");
printWithCheck("B0YBKT");
printWithCheck("B00030");