#! /usr/bin/env python # def isbn_is_valid ( s ): #*****************************************************************************80 # ## ISBN_IS_VALID reports whether an ISBN is valid. # # 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. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 September 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, a string containing 12 digits. # Dashes and other characters will be ignored. # # Output, bool VALUE, is TRUE if the string is valid. # from isbn_check_digit_calculate import isbn_check_digit_calculate from isbn_digit_to_i4 import isbn_digit_to_i4 from s_to_isbn_digits import s_to_isbn_digits n = 10 dvec = s_to_isbn_digits ( s, n ) c1 = isbn_check_digit_calculate ( s ) d1 = isbn_digit_to_i4 ( c1 ) d2 = dvec[9] if ( d1 == d2 ): value = True else: value = False return value def isbn_is_valid_test ( ): #*****************************************************************************80 # ## ISBN_IS_VALID_TEST tests ISBN_IS_VALID. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 17 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'ISBN_IS_VALID_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' ISBN_IS_VALID reports whether a ISBN is valid.' ) print ( '' ) # # Supply a valid code, with dashes. # s1 = '0-306-40615-2' value1 = True value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Modify one digit. # s1 = '0-326-40615-2' value1 = False value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Supply a valid code, with spaces. # s1 = '0 8493 9640 9'; value1 = True value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Modify the check digit. # s1 = '0 8493 9640 3' value1 = False value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Supply a valid code, with final digit 'X'. # s1 = '0-3870-9654-X' value1 = True value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Supply a valid code, with final digit 'x'. # s1 = '0-201-38597-x' value1 = True value2 = isbn_is_valid ( s1 ) print ( ' Validity of "%s" is %s, expecting %s' % ( s1, value2, value1 ) ) # # Terminate. # print ( '' ) print ( 'ISBN_IS_VALID_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) isbn_is_valid_test ( ) timestamp ( )