How to resolve the algorithm SEDOLs step by step in the Haskell programming language
How to resolve the algorithm SEDOLs step by step in the Haskell 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 Haskell programming language
The code above implements a program that checks if a certain string has a valid SEDOL. A SEDOL is a code that identifies a security in the UK, comprising of seven characters:
- six characters which identify the security
- one check digit that is calculated from the other six characters
The program first attempts to convert the string into a list of integers, using the sedolValue
function.
The sedolValue
function takes a single character and either returns an integer or an error message.
The sedolValue
function checks if the character is a vowel, in which case it returns an error message.
If the character is a digit, it returns the integer value of the digit.
If the character is an upper case letter, it returns the numerical value of the letter (e.g. 'A' = 10, 'B' = 11, etc.)
If the sedolValue
function returns a list of integers, the program then calculates the check digit using the checkSumFromSedolValues
function.
The checkSumFromSedolValues
function takes a list of integers and returns the check digit.
The check digit is calculated by multiplying each integer in the list by a weighting factor, summing the results, and then taking the remainder of the sum when divided by 10.
The result is then subtracted from 10, and the remainder of that result when divided by 10 is the check digit.
If the checkSumFromSedolValues
function returns a valid check digit, the program then prints the check digit to the console.
If the checkSumFromSedolValues
function returns an error message, the program prints the error message to the console.
The main
function of the program takes a list of strings and applies the checkSum
function to each string.
The checkSum
function takes a string and returns either the check digit or an error message.
The main
function then prints the check digit or error message to the console for each string.
Source code in the haskell programming language
import Data.Char (isAsciiUpper, isDigit, ord)
-------------------------- SEDOLS ------------------------
checkSum :: String -> String
checkSum x =
case traverse sedolValue x of
Right xs -> (show . checkSumFromSedolValues) xs
Left annotated -> annotated
checkSumFromSedolValues :: [Int] -> Int
checkSumFromSedolValues xs =
rem
( 10
- rem
( sum $
zipWith
(*)
[1, 3, 1, 7, 3, 9]
xs
)
10
)
10
sedolValue :: Char -> Either String Int
sedolValue c
| c `elem` "AEIOU" = Left " ← Unexpected vowel."
| isDigit c = Right (ord c - ord '0')
| isAsciiUpper c = Right (ord c - ord 'A' + 10)
--------------------------- TEST -------------------------
main :: IO ()
main =
mapM_
(putStrLn . ((<>) <*> checkSum))
[ "710889",
"B0YBKJ",
"406566",
"B0YBLH",
"228276",
"B0YBKL",
"557910",
"B0YBKR",
"585284",
"B0YBKT",
"BOYBKT", -- Ill formed test case - illegal vowel.
"B00030"
]
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Slate programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the Delphi programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Lua programming language
You may also check:How to resolve the algorithm Balanced brackets step by step in the Kotlin programming language