#! /usr/bin/env python # def bank_check_digit_calculate ( s ): #*****************************************************************************80 # ## BANK_CHECK_DIGIT_CALCULATE returns the check digit of a bank checksum. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 October 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, a string containing at least 8 digits. # Dashes and other characters will be ignored. A 9th digit may be # included, but it will be ignored. # # Output, integer D, the check digit. # from s_to_digits import s_to_digits n = 8 dvec = s_to_digits ( s, n ) d = 3 * ( dvec[0] + dvec[3] + dvec[6] ) \ + 7 * ( dvec[1] + dvec[4] + dvec[7] ) \ + dvec[2] + dvec[5] d = ( d % 10 ) d = ( ( 10 - d ) % 10 ) return d def bank_check_digit_calculate_test ( ): #*****************************************************************************80 # ## BANK_CHECK_DIGIT_CALCULATE_TEST tests BANK_CHECK_DIGIT_CALCULATE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 07 October 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'BANK_CHECK_DIGIT_CALCULATE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' BANK_CHECK_DIGIT_CALCULATE calculates the 9-th digit' ) print ( ' (the check digit) of a bank checksum.' ) print ( '' ) # # Supply the full code, with dashes. # s1 = '123-456-780' d1 = 0 d2 = bank_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is %d, expecting %d' % ( s1, d2, d1 ) ) # # Supply a partial code, with spaces. # s1 = '123 456 78' d1 = 0 d2 = bank_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is %d, expecting %d' % ( s1, d2, d1 ) ) # # Supply a partial code, no spaces. # s1 = '323-371-076' d1 = 6 d2 = bank_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is %d, expecting %d' % ( s1, d2, d1 ) ) # # Supply a partial code, no spaces. # s1 = '87654321' d1 = 2 d2 = bank_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is %d, expecting %d' % ( s1, d2, d1 ) ) # # Supply a partial code, no spaces. # s1 = '13579864' d1 = 3 d2 = bank_check_digit_calculate ( s1 ) print ( ' Check digit of "%s" is %d, expecting %d' % ( s1, d2, d1 ) ) # # Terminate. # print ( '' ) print ( 'BANK_CHECK_DIGIT_CALCULATE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) bank_check_digit_calculate_test ( ) timestamp ( )