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

Published on 12 May 2024 09:40 PM

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

Source code in the rexx programming language

/*REXX program validates that the  last digit (the check digit)  of a  CUSIP  is valid. */
@.=
parse arg @.1 .
if @.1=='' | @.1==","  then do;   @.1= 037833100       /* Apple Incorporated            */
                                  @.2= 17275R102       /* Cisco Systems                 */
                                  @.3= 38259P508       /* Google Incorporated           */
                                  @.4= 594918104       /* Microsoft Corporation         */
                                  @.5= 68389X106       /* Oracle Corporation (incorrect)*/
                                  @.6= 68389X105       /* Oracle Corporation            */
                            end

     do j=1  while @.j\='';   chkDig=CUSIPchk(@.j)     /*calculate check digit from func*/
     OK=word("isn't is", 1 + (chkDig==right(@.j,1) ) ) /*validate  check digit with func*/
     say 'CUSIP '    @.j    right(OK, 6)     "valid."  /*display the CUSIP and validity.*/
     end   /*j*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
CUSIPchk: procedure;  arg x 9;  $=0;                     abc= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                                       do k=1  for 8
                                       y=substr(x, k, 1)
                                          select
                                          when datatype(y,'W')  then #=y
                                          when datatype(y,'U')  then #=pos(y, abc) + 9
                                          when          y=='*'  then #=36
                                          when          y=='@'  then #=37
                                          when          y=='#'  then #=38
                                          otherwise  return 0       /*invalid character.*/
                                          end   /*select*/
                                       if k//2==0  then #=#+#       /*K even?  Double it*/
                                       $=$ + #%10 + #//10
                                       end      /*k*/
           return (10- $//10) // 10


/*REXX program validates that the  last digit (the check digit)  of a  CUSIP  is valid. */
@.=
parse arg @.1 .
if @.1=='' | @.1==","  then do;   @.1= 037833100       /* Apple Incorporated            */
                                  @.2= 17275R102       /* Cisco Systems                 */
                                  @.3= 38259P508       /* Google Incorporated           */
                                  @.4= 594918104       /* Microsoft Corporation         */
                                  @.5= 68389X106       /* Oracle Corporation (incorrect)*/
                                  @.6= 68389X105       /* Oracle Corporation            */
                            end

     do j=1  while @.j\='';   chkDig=CUSIPchk(@.j)     /*calculate check digit from func*/
     OK=word("isn't is", 1 + (chkDig==right(@.j,1) ) ) /*validate  check digit with func*/
     say 'CUSIP '    @.j    right(OK, 6)     "valid."  /*display the CUSIP and validity.*/
     end   /*j*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
CUSIPchk: procedure; arg x 9;  $=0;         abc= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*@#'
                                      /* [↓]  if  Y  isn' found,  then POS returns zero.*/
                                    do k=1  for 8;   y=substr(x,k,1) /*get a character. */
                                    #=pos(y, abc) - 1                /*get its position.*/
                                    if #   == -1  then return 0      /*invalid character*/
                                    if k//2==  0  then #=#+#         /*K even? double it*/
                                    $=$ + #%10 + #//10
                                    end      /*k*/
          return (10-$//10) // 10


  

You may also check:How to resolve the algorithm Active Directory/Connect step by step in the Phix programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Factor programming language
You may also check:How to resolve the algorithm Word wrap step by step in the OCaml programming language
You may also check:How to resolve the algorithm Twin primes step by step in the Julia programming language