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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "/str" for Char
import "/fmt" for Conv

var sedol = Fn.new { |s|
    if (!(s is String && s.count == 6)) return false
    var weights = [1, 3, 1, 7, 3, 9]
    var sum = 0
    for (i in 0..5) {
        var c = s[i]
        if (!Char.isUpper(c) && !Char.isDigit(c)) return null
        if ("AEIOU".contains(c)) return null
        sum = sum + Conv.atoi(c, 36) * weights[i]
    }
    var cd = (10 - sum%10) % 10
    return s + "%(cd)"
}

var tests = [
    "710889",
    "B0YBKJ",
    "406566",
    "B0YBLH",
    "228276",
    "B0YBKL",
    "557910",
    "B0YBKR",
    "585284",
    "B0YBKT",
    "B00030",
    "I23456"
]

for (test in tests) {
    var a
    var ans = (a = sedol.call(test)) ? a : "not valid"
    System.print("%(test) -> %(ans)")
}

  

You may also check:How to resolve the algorithm System time step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the REXX programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the PL/I programming language