Validate the check digit of an ISBN-13 code

/* Validate the check digit of an ISBN-13 code:
  Multiply every other digit by 3.
  Add the digits together.
  Take the remainder of division by  10.
  If it is  0, the ISBN-13 check digit is correct. 
  from RosettaCode */
fn check_isbn(isbn: &str) -> bool {
    if isbn.chars().filter(|c| c.is_digit(10)).count() != 13 {
            return false;
    } 
    let checksum = isbn.chars().filter_map(|c| c.to_digit(10))
        .zip([1, 3].iter().cycle())
        .fold(0, |acc, (val, fac)| acc + val * fac);
    checksum % 10 == 0
}

fn main() {
    let isbns = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"];
    isbns.iter().for_each(|isbn| println!("{}: {}", isbn, check_isbn(isbn)));
}

3.78
9
Workoverflow 120 points

                                    # Validate the check digit of an ISBN-13 code:
#  Multiply every other digit by 3.
#  Add the digits together.
#  Take the remainder of division by  10.
#  If it is  0, the ISBN-13 check digit is correct. 
#  from RosettaCode 
def validISBN13?(str)
  cleaned = str.delete("^0-9").chars
  return false unless cleaned.size == 13
  cleaned.each_slice(2).sum{|d1, d2| d1.to_i + 3*d2.to_i }.remainder(10) == 0
end
 
isbns = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"]
isbns.each{|isbn| puts "#{isbn}: #{validISBN13?(isbn)}" }  

3.78 (9 Votes)
0
3.38
8
Braybuddy 120 points

                                    /* Validate the check digit of an ISBN-13 code:
  Multiply every other digit by 3.
  Add the digits together.
  Take the remainder of division by  10.
  If it is  0, the ISBN-13 check digit is correct. 
  from RosettaCode */

#include <stdio.h>
 
int check_isbn13(const char *isbn) {
    int ch = *isbn, count = 0, sum = 0;
    for ( ; ch != 0; ch = *++isbn, ++count) {
        if (ch == ' ' || ch == '-') {
            --count;
            continue;
        }
        if (ch < '0' || ch > '9') return 0;
        if (count & 1) {
            sum += 3 * (ch - '0');
        } else {
            sum += ch - '0';
        }
    }
    if (count != 13) return 0;
    return !(sum%10);
}
 
int main() {
    int i;
    const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"};
    for (i = 0; i < 4; ++i) {
        printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad");
    }
    return 0;
}

3.38 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source