How to resolve the algorithm SEDOLs step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

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

This Julia code defines a function, appendchecksum, that appends a checksum digit to a given string of alphanumeric characters, ensuring that it follows the SEDOL (Stock Exchange Daily Official List) number format. The function takes a string chars as input and returns a new string with the checksum digit appended. Here's a detailed breakdown of the code:

  1. Input Validation: Before calculating the checksum, the function checks if all the characters in chars are alphanumeric using the all(isalnum, chars) condition. If any non-alphanumeric character is found, it throws an ArgumentError with the message "invalid SEDOL number '$chars'".

  2. Checksum Calculation: It initializes a vector weights with the values [1, 3, 1, 7, 3, 9, 1]. These weights are multiplied with each character in the input string chars, and the resulting values are summed to calculate the checksum digit.

  3. Conversion to Integer and Multiplication: Each character in chars is converted to an integer using the parse(Int, c, 36) function, assuming the characters represent numbers in base-36 (the base used in SEDOL numbers). The resulting integers are multiplied with their corresponding weights.

  4. Checksum Calculation: The sum of the weighted values is calculated and stored in the variable s.

  5. Digit Calculation: To obtain the checksum digit, the code calculates (10 - s % 10) % 10. This ensures that the checksum digit is always a single digit between 0 and 9.

  6. Checksum Append: Finally, the calculated checksum digit is appended to the input string chars, resulting in a new string with the checksum.

The code is tested using the @testset and @test macros from the Base.Test module. A list of test cases (tests) and their expected outputs (csums) are defined. The test checks if the appendchecksum function produces the correct checksums for the given inputs.

Source code in the julia programming language

using Base.Test

function appendchecksum(chars::AbstractString)
    if !all(isalnum, chars) throw(ArgumentError("invalid SEDOL number '$chars'")) end
    weights = [1, 3, 1, 7, 3, 9, 1]

    s = 0
    for (w, c) in zip(weights, chars)
        s += w * parse(Int, c, 36)
    end
    return string(chars, (10 - s % 10) % 10)
end

tests = ["710889", "B0YBKJ", "406566", "B0YBLH", "228276", "B0YBKL", "557910", "B0YBKR", "585284", "B0YBKT", "B00030"]
csums = ["7108899", "B0YBKJ7", "4065663", "B0YBLH2", "2282765", "B0YBKL9", "5579107", "B0YBKR5", "5852842", "B0YBKT7", "B000300"]

@testset "Checksums" begin
    for (t, c) in zip(tests, csums)
        @test appendchecksum(t) == c
    end
end


  

You may also check:How to resolve the algorithm Copy a string step by step in the Lasso programming language
You may also check:How to resolve the algorithm Wieferich primes step by step in the Racket programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the C programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Factor programming language
You may also check:How to resolve the algorithm Tau function step by step in the MiniScript programming language