How to resolve the algorithm ISBN13 check digit step by step in the V (Vlang) programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm ISBN13 check digit step by step in the V (Vlang) 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 V (Vlang) programming language
Source code in the v programming language
fn check_isbn13(isbn13 string) bool {
// remove any hyphens or spaces
isbn := isbn13.replace('-','').replace(' ','')
// check length == 13
le := isbn.len
if le != 13 {
return false
}
// check only contains digits and calculate weighted sum
mut sum := 0
for i, c in isbn.split('') {
if c.int() < '0'.int() || c.int() > '9'.int() {
return false
}
if i%2 == 0 {
sum += c.int() - '0'.int()
} else {
sum += 3 * (c.int() - '0'.int())
}
}
return sum%10 == 0
}
fn main() {
isbns := ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
for isbn in isbns {
mut res := "bad"
if check_isbn13(isbn) {
res = "good"
}
println("$isbn: $res")
}
}
You may also check:How to resolve the algorithm Sort stability step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm 24 game step by step in the Huginn programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the Delphi programming language
You may also check:How to resolve the algorithm Totient function step by step in the Arturo programming language
You may also check:How to resolve the algorithm Shortest common supersequence step by step in the Phix programming language