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

Published on 12 May 2024 09:40 PM

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

Source code in the zig programming language

const std = @import("std");
const print = std.debug.print;

pub fn CusipCheckDigit(cusip: *const [9:0]u8) bool {
    var i: usize = 0;
    var sum: i32 = 0;
    while (i < 8) {
        const c = cusip[i];
        var v: i32 = undefined;
        if (c <= '9' and c >= '0') {
            v = c - 48;
        }
        else if (c <= 'Z' and c >= 'A') {
            v = c - 55;
        }
        else if (c == '*') {
            v = 36;
        }
        else if (c == '@') {
            v = 37;
        }
        else if (c == '#') {
            v = 38;
        }
        else {
            return false;
        }
        if (i % 2 == 1) {
            v *= 2;
        }
        sum = sum + @divFloor(v, 10) + @mod(v, 10);
        i += 1;
    }
    return (cusip[8] - 48 == @mod((10 - @mod(sum, 10)), 10));
}

pub fn main() void {
    const cusips = [_]*const [9:0]u8 {
        "037833100",
        "17275R102",
        "38259P508",
        "594918104",
        "68389X106",
        "68389X105"
    };
    for (cusips) |cusip| {
        print("{s} -> {}\n", .{cusip, CusipCheckDigit(cusip)});
    }
}


  

You may also check:How to resolve the algorithm FizzBuzz step by step in the Maxima programming language
You may also check:How to resolve the algorithm Ulam spiral (for primes) step by step in the D programming language
You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the OCaml programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the Elixir programming language