How to resolve the algorithm CUSIP step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CUSIP step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import strutils

proc cusipCheck(cusip: string): bool =
  if cusip.len != 9:
    return false
  
  var 
    sum, v = 0
  for i, c in cusip[0 .. ^2]:
    if c.isDigit:
      v = parseInt($c)
    elif c.isUpperAscii:
      v = ord(c) - ord('A') + 10
    elif c == '*':
      v = 36
    elif c == '@':
      v = 37
    elif c == '#':
      v = 38
    
    if i mod 2 == 1:
      v *= 2
    
    sum += v div 10 + v mod 10
  let check = (10 - (sum mod 10)) mod 10
  return $check == $cusip[^1]

proc main =
  let codes = [
    "037833100",
    "17275R102",
    "38259P508",
    "594918104",
    "68389X106",
    "68389X105"
  ]

  for code in codes:
    echo code, ": ", if cusipCheck(code): "Valid" else: "Invalid"

main()


  

You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Julia programming language
You may also check:How to resolve the algorithm Pinstripe/Printer step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Polymorphic copy step by step in the Common Lisp programming language