#! /usr/bin/env python # def isbn_check_digit_calculate ( s ): #*****************************************************************************80 # ## ISBN_CHECK_DIGIT_CALCULATE determines the check digit for an ISBN. # # Discussion: # # ISBN stands for International Standard Book Number. A unique ISBN # is assigned to each new book. The ISBN includes 10 digits. There is # an initial digit, then a dash, then a set of digits which are a # code for the publisher, another digit, and then the check digit: # # initial-publisher-book-check # # The number of digits used for the publisher and book codes can vary, # but the check digit is always one digit, and the total number of # digits is always 10. # # The check digit is interesting, because it is a way of trying to # make sure that an ISBN has not been incorrectly copied. Specifically, # if the ISBN is correct, then its ten digits will satisfy # # 10 * A + 9 * B + 8 * C + 7 * D + 6 * E # + 5 * F * 4 * G * 3 * H + 2 * I + J = 0 mod 11. # # Here, we've taken 'A' to represent the first digit and 'J' the # last (which is the check digit). In order for the code to work, # the value of J must be allowed to be anything from 0 to 10. In # cases where J works out to be 10, the special digit 'X' is used. # An 'X' digit can only occur in this last check-digit position # on an ISBN. # # Example: # # S => 0-8493-9640-? # D <= 9 # # meaning the ISBN is 0-8493-9640-9 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 September 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, a string. Dashes and spaces and other # nonnumeric data is ignored, but at least 9 digits are expected, and only # the first 9 digits will be examined. # # Output, character ISBN_CHECK_DIGIT_CALCULATE, the ISBN # check digit that should be appended to form the full 10 digit ISBN. # from i4_to_isbn_digit import i4_to_isbn_digit from s_to_digits import s_to_digits # # Extract first 9 digits. # n = 9 dvec = s_to_digits ( s, n ) # # Compute the check digit. # d = 0 for i in range ( 0, 9 ): d = d + ( 10 - i ) * dvec[i] d = ( ( 11 - ( d % 11 ) ) % 11 ) # # Convert digits 0 through 9, 10 to characters '0' through '9', 'X'. # c = i4_to_isbn_digit ( d ) return c def isbn_check_digit_calculate_test ( ): #*****************************************************************************80 # ## ISBN_CHECK_DIGIT_CALCULATE_TEST tests ISBN_CHECK_DIGIT_CALCULATE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'ISBN_CHECK_DIGIT_CALCULATE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' ISBN_CHECK_DIGIT_CALCULATE calculates the 10-th digit' ) print ( ' (the check digit) of a 10-digit ISBN.' ) print ( '' ) # # Supply the full code, with dashes. # s1 = '0-306-40615-2' c1 = '2' c2 = isbn_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is "%c", expecting "%c"' % ( s1, c2, c1 ) ) # # Supply a partial code, with spaces. # s1 = '0 8493 9640' c1 = '9' c2 = isbn_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is "%c", expecting "%c"' % ( s1, c2, c1 ) ) # # Supply a partial code, no spaces. # s1 = '158488059' c1 = '7' c2 = isbn_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is "%c", expecting "%c"' % ( s1, c2, c1 ) ) # # Supply a partial code, no spaces. # s1 = '246897531' c1 = '6' c2 = isbn_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is "%c", expecting "%c"' % ( s1, c2, c1 ) ) # # Supply a partial code, no spaces. # s1 = '135798642' c1 = '4' c2 = isbn_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is "%c", expecting "%c"' % ( s1, c2, c1 ) ) # # Terminate. # print ( '' ) print ( 'ISBN_CHECK_DIGIT_CALCULATE_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) isbn_check_digit_calculate_test ( ) timestamp ( )