How to resolve the algorithm SEDOLs step by step in the C# programming language
How to resolve the algorithm SEDOLs step by step in the C# programming language
Table of Contents
Problem Statement
For each number list of 6-digit SEDOLs, calculate and append the checksum digit.
That is, given this input: Produce this output: Check each input is correctly formed, especially with respect to valid characters allowed in a SEDOL string.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm SEDOLs step by step in the C# programming language
The provided C# code defines a method called sedolChecksum
that calculates the checksum digit for a given SEDOL (Stock Exchange Daily Official List) code. A SEDOL code is a seven-character alphanumeric identifier used to uniquely identify UK and Irish securities. The checksum digit is the last character of a valid SEDOL code and is used to verify the accuracy of the code.
Here's how the code works:
-
It starts by defining an array called
sedol_weights
with the following values:[1, 3, 1, 7, 3, 9]
. These weights are used to calculate the checksum. -
The
sedolChecksum
method takes a string parameter calledsedol
, which represents the SEDOL code for which the checksum needs to be calculated. -
It first checks the length of the
sedol
string. If the length is 7, it assumes that the SEDOL code is already checksummed and returns the last character of thesedol
string as the checksum digit. -
If the length of the
sedol
string is not 7, it checks if the string contains only uppercase letters and digits using regular expressions. If it contains any vowels ("AEIOUaeiou") or invalid characters, it returns -1, indicating an invalid SEDOL code. -
If the
sedol
string passes the validity checks, it enters a loop that iterates through the first six characters of the string (excluding the checksum digit). -
For each character in the loop, it checks if it is a digit or a letter.
-
If it is a digit, it calculates the product of the digit and its corresponding weight from the
sedol_weights
array and adds it to thesum
variable. -
If it is a letter, it converts the character to uppercase and calculates the product of the corresponding letter's position in the alphabet (minus 55) and its weight. It then adds this product to the
sum
variable.
-
-
After processing all six characters, it calculates the checksum digit as follows:
- It takes the remainder of dividing the
sum
by 10. - It then subtracts this remainder from 10 to get the checksum digit.
- If the result is 10, it is replaced with 0.
- It takes the remainder of dividing the
-
The method returns the calculated checksum digit as an integer.
In summary, the sedolChecksum
method validates the given SEDOL code, calculates the checksum digit based on the specific weights for each character, and returns the result. This checksum digit is used to verify the integrity of the SEDOL code and detect errors or alterations.
Source code in the csharp programming language
static int[] sedol_weights = { 1, 3, 1, 7, 3, 9 };
static int sedolChecksum(string sedol)
{
int len = sedol.Length;
int sum = 0;
if (len == 7) //SEDOL code already checksummed?
return (int)sedol[6];
if ((len > 7) || (len < 6) || System.Text.RegularExpressions.Regex.IsMatch(sedol, "[AEIOUaeiou]+")) //invalid SEDOL
return -1;
for (int i = 0; i < 6; i++)
{
if (Char.IsDigit(sedol[i]))
sum += (((int)sedol[i] - 48) * sedol_weights[i]);
else if (Char.IsLetter(sedol[i]))
sum += (((int)Char.ToUpper(sedol[i]) - 55) * sedol_weights[i]);
else
return -1;
}
return (10 - (sum % 10)) % 10;
}
You may also check:How to resolve the algorithm Search a list step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Limbo programming language
You may also check:How to resolve the algorithm User input/Text step by step in the FALSE programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Groovy programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the Delphi programming language