Ověřte kontrolní číslici kódu ISBN-13

Příklady kódu

0
0

Ověřte kontrolní číslici kódu ISBN-13

/* 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)));
}
0
0

Ověřte kontrolní číslici kódu ISBN-13

# 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)}" }  

Související stránky

Související stránky s příklady

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................