#! /usr/bin/env python # def s_to_isbn_digits ( s, n ): #*****************************************************************************80 # ## S_TO_ISBN_DIGITS extracts N ISBN digits from a string. # # Discussion: # # The string may include spaces, letters, and dashes, but only the # digits '0' through '9' and 'X' will be extracted. # # Example: # # S => 34E9X-70.6 # N => 5 # D <= (/ 3, 4, 9, 10, 7 /) # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # # Parameters: # # Input, string S, the string. # # Input, integer N, the number of digits to extract. # # Output, integer DVEC(N), the extracted digits. # import numpy as np from ch_is_isbn_digit import ch_is_isbn_digit from isbn_digit_to_i4 import isbn_digit_to_i4 from sys import exit s_len = len ( s ) s_pos = 0 d_pos = 0 dvec = np.zeros ( n, dtype = np.int32 ) while ( d_pos < n ): if ( s_len <= s_pos ): print ( '' ) print ( 'S_TO_ISBN_DIGITS - Fatal error!' ) print ( ' Could not read enough data from string.' ) error ( 'S_TO_ISBN_DIGITS - Fatal error!' ); c = s[s_pos] s_pos = s_pos + 1 if ( ch_is_isbn_digit ( c ) ): dvec[d_pos] = isbn_digit_to_i4 ( c ) d_pos = d_pos + 1 return dvec def s_to_isbn_digits_test ( ): #*****************************************************************************80 # ## S_TO_ISBN_DIGITS_TEST tests S_TO_ISBN_DIGITS. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # import platform from i4vec_print import i4vec_print print ( '' ) print ( 'S_TO_ISBN_DIGITS_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' S_TO_ISBN_DIGITS: string -> ISBN digit vector' ) s = '34E9X-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 5 dvec = s_to_isbn_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 5 digits:' ) s = '34E9X-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 7 dvec = s_to_isbn_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 7 digits:' ) # # Terminate. # print ( '' ) print ( 'S_TO_ISBN_DIGITS_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) s_to_isbn_digits_test ( ) timestamp ( )