How to resolve the algorithm ISBN13 check digit step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm ISBN13 check digit step by step in the D programming language

Table of Contents

Problem Statement

Validate the check digit of an ISBN-13 code:

You might use the following codes for testing:

Show output here, on this page

Let's start with the solution:

Step by Step solution about How to resolve the algorithm ISBN13 check digit step by step in the D programming language

Source code in the d programming language

import std.stdio;

bool isValidISBN13(string text) {
    int sum, i;
    foreach (c; text) {
        if ('0' <= c && c <= '9') {
            int value = c - '0';
            if (i % 2 == 0) {
                sum += value;
            } else {
                sum += 3 * value;
            }

            i++;
        }
    }
    return i == 13 && 0 == sum % 10;
}

unittest {
    assert(isValidISBN13("978-1734314502"));
    assert(!isValidISBN13("978-1734314509"));
    assert(isValidISBN13("978-1788399081"));
    assert(!isValidISBN13("978-1788399083"));
}


  

You may also check:How to resolve the algorithm Flipping bits game step by step in the Action! programming language
You may also check:How to resolve the algorithm Partial function application step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Camel case and snake case step by step in the jq programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the 8086 Assembly programming language
You may also check:How to resolve the algorithm UTF-8 encode and decode step by step in the Action! programming language