How to resolve the algorithm CUSIP step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm CUSIP step by step in the Scala programming language
Table of Contents
Problem Statement
A CUSIP is a nine-character alphanumeric code that identifies a North American financial security for the purposes of facilitating clearing and settlement of trades. The CUSIP was adopted as an American National Standard under Accredited Standards X9.6.
Ensure the last digit (i.e., the check digit) of the CUSIP code (the 1st column) is correct, against the following:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm CUSIP step by step in the Scala programming language
Source code in the scala programming language
object Cusip extends App {
val candidates = Seq("037833100", "17275R102", "38259P508", "594918104", "68389X106", "68389X105")
for (candidate <- candidates)
printf(f"$candidate%s -> ${if (isCusip(candidate)) "correct" else "incorrect"}%s%n")
private def isCusip(s: String): Boolean = {
if (s.length != 9) false
else {
var sum = 0
for (i <- 0 until 7) {
val c = s(i)
var v = 0
if (c >= '0' && c <= '9') v = c - 48
else if (c >= 'A' && c <= 'Z') v = c - 55 // lower case letters apparently invalid
else if (c == '*') v = 36
else if (c == '@') v = 37
else if (c == '#') v = 38
else return false
if (i % 2 == 1) v *= 2 // check if odd as using 0-based indexing
sum += v / 10 + v % 10
}
s(8) - 48 == (10 - (sum % 10)) % 10
}
}
}
You may also check:How to resolve the algorithm Strip control codes and extended characters from a string step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sort numbers lexicographically step by step in the Wren programming language
You may also check:How to resolve the algorithm Binary search step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Tokenize a string with escaping step by step in the C++ programming language
You may also check:How to resolve the algorithm Entropy step by step in the Lambdatalk programming language