#! /usr/bin/env python3 # def chrpak_test ( ): #*****************************************************************************80 # ## chrpak_test() tests chrpak(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 September 2024 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'chrpak_test():' ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' numpy version: ' + np.version.version ) print ( ' Test chrpak().' ) boolean_to_string_test ( ) ch_cap_test ( ) ch_is_digit_test ( ) ch_is_isbn_digit_test ( ) ch_to_digit_test ( ) ch_to_rot13_test ( ) chvec_lt_test ( ) chvec_reduce_test ( ) digit_to_ch_test ( ) digits_decimal_to_r8_test ( ) digits_sexagesimal_print_test ( ) digits_sexagesimal_to_r8_test ( ) hex_digit_to_i4_test ( ) i4_length_test ( ) i4_to_hex_digit_test ( ) i4_to_month_abb_test ( ) i4_to_month_name_test ( ) i4_to_isbn_digit_test ( ) i4vec_print_test ( ) isbn_digit_to_i4_test ( ) r8_to_digits_decimal_test ( ) r8_to_digits_sexagesimal_test ( ) rat_to_s_test ( ) s_digits_count_test ( ) s_escape_tex_test ( ) s_len_trim_test ( ) s_quote_test ( ) s_to_caesar_test ( ) s_to_digits_test ( ) s_to_isbn_digits_test ( ) s_to_rot13_test ( ) # # Terminate. # print ( '' ) print ( 'chrpak_test():' ) print ( ' Normal end of execution.' ) return def boolean_to_string ( b ): #*****************************************************************************80 # # boolean_to_string() returns 'True' or 'False' depending on a boolean value. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 June 2024 # # Author: # # John Burkardt # # Input: # # boolean B: the boolean value. # # Output: # # string S: the corresponding string. # if ( b ): s = 'True' else: s = 'False' return s def boolean_to_string_test ( ): #*****************************************************************************80 # ## boolean_to_string_test() tests boolean_to_string(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 18 June 2024 # # Author: # # John Burkardt # print ( '' ) print ( 'boolean_to_string_test():' ) print ( ' boolean_to_string() creates the string "True" or "False"' ) print ( ' based on a boolean value.' ) print ( ' This can be useful when printing boolean results.' ) print ( '' ) print ( ' I isPrime?' ) print ( '' ); for i in range ( 1, 11 ): b = ( i == 2 ) or ( i == 3 ) or ( i == 5 ) or ( i == 7 ) s = boolean_to_string ( b ) print ( ' ', i, ' ', s ) return def ch_cap ( c ): #*****************************************************************************80 # ## ch_cap() capitalizes a single character. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 August 2023 # # Author: # # John Burkardt # # Input: # # character C, the character to capitalize. # # Output: # # character C2, the capitalized character. # i = ord ( c ) if ( ord ( 'a' ) <= i and i <= ord ( 'z' ) ): i2 = i + ord ( 'A' ) - ord ( 'a' ) c2 = chr ( i2 ) else: c2 = c return c2 def ch_cap_test ( ): #*****************************************************************************80 # ## ch_cap_test() tests ch_cap(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 June 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'ch_cap_test():' ) print ( ' ch_cap() uppercases a character.' ) print ( '' ) print ( ' C ch_cap(C)' ) print ( '' ) c = 'F' c2 = ch_cap ( c ) print ( ' %c %c' % ( c, c2 ) ) c = 'f' c2 = ch_cap ( c ) print ( ' %c %c' % ( c, c2 ) ) c = '1' c2 = ch_cap ( c ) print ( ' %c %c' % ( c, c2 ) ) c = 'b' c2 = ch_cap ( c ) print ( ' %c %c' % ( c, c2 ) ) c = '&' c2 = ch_cap ( c ) print ( ' %c %c' % ( c, c2 ) ) return def ch_is_digit ( c ): #*****************************************************************************80 # ## ch_is_digit() returns TRUE if the character C is a digit. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 September 2015 # # Author: # # John Burkardt # # Input: # # character C, the character to be checked. # # Output: # # logical VALUE is TRUE if C is a decimal digit. # i0 = ord ( '0' ) i9 = ord ( '9' ) ic = ord ( c ) if ( i0 <= ic and ic <= i9 ): value = True else: value = False return value def ch_is_digit_test ( ): #*****************************************************************************80 # ## ch_is_digit_test() tests ch_is_digit(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 September 2015 # # Author: # # John Burkardt # import numpy as np import platform c_test = np.array ( [ '0', '1', '2', '3', '4', \ '5', '6', '7', '8', '9', \ 'X', '?', ' ' ] ) print ( '' ) print ( 'ch_is_digit_test():' ) print ( ' ch_is_digit() is TRUE if a character is a decimal digit.' ) print ( '' ) for i in range ( 0, 13 ): c = c_test[i] value = ch_is_digit ( c ) print ( ' %8d "%c" %s' % ( i, c, value ) ) return def ch_is_isbn_digit ( c ): #*****************************************************************************80 # ## ch_is_isbn_digit() returns TRUE if the character C is an ISBN digit. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 September 2015 # # Author: # # John Burkardt # # Input: # # character C, the character to be checked. # # Output: # # logical VALUE is TRUE if C is an ISBN digit. # i0 = ord ( '0' ) i9 = ord ( '9' ) ic = ord ( c ) if ( i0 <= ic and ic <= i9 ): value = True elif ( c == 'X' or c == 'x' ): value = True else: value = False return value def ch_is_isbn_digit_test ( ): #*****************************************************************************80 # ## ch_is_isbn_digit_test() tests ch_is_isbn_digit(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 September 2015 # # Author: # # John Burkardt # import numpy as np import platform c_test = np.array ( [ '0', '1', '2', '3', '4', \ '5', '6', '7', '8', '9', \ 'X', 'x', 'Y', '*', '?', \ ' ' ] ) print ( '' ) print ( 'ch_is_isbn_digit_test()' ) print ( ' ch_is_isbn_digit() is TRUE if a character is an ISBN digit.' ) print ( '' ) for i in range ( 0, 16 ): c = c_test[i] value = ch_is_isbn_digit ( c ) print ( ' "%c" %s' % ( c, value ) ) return def ch_to_digit ( c ): #*****************************************************************************80 # ## ch_to_digit() returns the integer value of a base 10 digit. # # Example: # # C DIGIT # --- ----- # '0' 0 # '1' 1 # ... ... # '9' 9 # ' ' -1 # 'X' -1 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 June 2015 # # Author: # # John Burkardt # # Input: # # character C, the decimal digit, '0' through '9' # are legal. # # Output: # # integer DIGIT, the corresponding integer value. If C was # 'illegal', then DIGIT is -1. # i0 = ord ( '0' ) i9 = ord ( '9' ) ic = ord ( c ) if ( i0 <= ic and ic <= i9 ): digit = ic - i0 else: digit = -1 return digit def ch_to_digit_test ( ): #*****************************************************************************80 # ## ch_to_digit_test() tests ch_to_digit(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 September 2015 # # Author: # # John Burkardt # import numpy as np import platform c_test = np.array ( [ '0', '1', '2', '3', '4', \ '5', '6', '7', '8', '9', \ 'X', '?', ' ' ] ) print ( '' ) print ( 'ch_to_digit_test():' ) print ( ' ch_to_digit(): character -> decimal digit' ) print ( '' ) for i in range ( 0, 13 ): c = c_test[i] i2 = ch_to_digit ( c ) print ( ' %8d "%c" %8d' % ( i, c, i2 ) ) return def ch_to_rot13 ( ch ): #*****************************************************************************80 # ## ch_to_rot13() converts a character to its ROT13 equivalent. # # Discussion: # # Two applications of CH_TO_ROT13 to a character will return the original.! # # As a further scrambling, digits are similarly rotated using # a "ROT5" scheme. # # Example: # # Input: Output: # # a n # C P # J W # 1 6 # 5 0 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # # Input: # # character CH, the character to be converted. # # Output: # # character VALUE, the ROT13 equivalent of the character. # i = ord ( ch ) # # [0:4] -> [5:9] # if ( 48 <= i and i <= 52 ): value = i + 5 # # [5:9] -> [0:4] # elif ( 53 <= i and i <= 57 ): value = i - 5 # # [A:M] -> [N:Z] # elif ( 65 <= i and i <= 77 ): value = i + 13 # # [N:Z] -> [A:M] # elif ( 78 <= i and i <= 90 ): value = i - 13 # # [a:m] -> [n:z] # elif ( 97 <= i and i <= 109 ): value = i + 13 # # [n:z] -> [a:m] # elif ( 110 <= i and i <= 122 ): value = i - 13 else: value = i value = chr ( value ) return value def ch_to_rot13_test ( ): #*****************************************************************************80 # ## ch_to_rot13_test() tests ch_to_rot13(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'ch_to_rot13_test():' ) print ( ' ch_to_rot13() "encodes" characters using ROT13' ) print ( ' (and digits using ROT5).' ) print ( ' A second application of the function returns the' ) print ( ' original character.' ) s1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' s1_length = len ( s1 ) s2 = '' s3 = '' for i in range ( 0, s1_length ): s2 = s2 + ch_to_rot13 ( s1[i] ) s3 = s3 + ch_to_rot13 ( s2[i] ) print ( '' ) print ( ' CH : %s' % ( s1 ) ) print ( ' ROT13(CH) : %s' % ( s2 ) ) print ( 'ROT13(ROT13(CH)): %s' % ( s3 ) ) s1 = 'CH_TO_ROT13 "encodes" characters using ROT13' s1_length = len ( s1 ) s2 = '' s3 = '' for i in range ( 0, s1_length ): s2 = s2 + ch_to_rot13 ( s1[i] ) s3 = s3 + ch_to_rot13 ( s2[i] ) print ( '' ) print ( ' CH : %s' % ( s1 ) ) print ( ' ROT13(CH) : %s' % ( s2 ) ) print ( 'ROT13(ROT13(CH)): %s' % ( s3 ) ) return def chvec_eq ( c1, c2 ): #*****************************************************************************80 # ## chvec_eq() is TRUE if one character vector is equal to another. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 May 2021 # # Author: # # John Burkardt # # Input: # # char C1(N1), C2(N2): two character vectors. # # Output: # # logical equals: is TRUE if N1=N2 and C1=C2 for all entries. # n1 = len ( c1 ) n2 = len ( c2 ) if ( n1 != n2 ): return False n = n1 for i in range ( 0, n ): if ( c1[i] != c2[i] ): return False return True def chvec_lt ( c1, c2 ): #*****************************************************************************80 # ## chvec_lt() is TRUE if one character vector is less than another. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 June 2020 # # Author: # # John Burkardt # # Input: # # char C1(N1), C2(N2), two character vectors. # # Output: # # logical is_less_than: is TRUE if C1 < C2. # is_less_than = True n1 = len ( c1 ) n2 = len ( c2 ) n = min ( n1, n2 ) for i in range ( 0, n ): if ( c1[i] < c2[i] ): is_less_than = True return is_less_than elif ( c2[i] < c1[i] ): is_less_than = False return is_less_than if ( n1 < n2 ): is_less_than = True else: is_less_than = False return is_less_than def chvec_lt_test ( ): #*****************************************************************************80 # ## chvec_lt_test() tests chvec_lt(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 20 June 2020 # # Author: # # John Burkardt # print ( '' ) print ( 'chvec_lt_test():' ) print ( ' chvec_lt(c1,c2) is TRUE if c1 < c2' ) print ( ' for character strings c1 and c2.' ) print ( '' ) print ( ' C1 C2 C1 character.' ) print ( '' ) for i in range ( -2, 12 ): c = digit_to_ch ( i ) i2 = ch_to_digit ( c ) print ( ' %8d "%c" %8d' % ( i, c, i2 ) ) return def digits_decimal_to_r8 ( d, p ): #*****************************************************************************80 # ## digits_decimal_to_r8() evalulates an N digit decimal digit number. # # Discussion: # # R = d(0) * 10^p + d(1) * 10^(p-1) + ... + d(n-1) * 10^(p+1-n). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 June 2022 # # Author: # # John Burkardt # # Input: # # integer D(N): the decimal digits. # # integer P: the power of 10 multiplying the first digit. # # Output: # # real R: the value represented. # power = 10.0 ** p n = len ( d ) r = 0.0 for i in range ( 0, n ): r = r + d[i] * power power = power / 10.0 return r def digits_decimal_to_r8_test ( ): #*****************************************************************************80 # ## digits_decimal_to_r8_test() tests digits_decimal_to_r8(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 June 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'digits_decimal_to_r8_test():' ) print ( ' digits_decimal_to_r8() converts base 10 digits of a' ) print ( ' number into a real value;' ) for test in range ( 0, 3 ): if ( test == 0 ): d = np.array ( [ 1, 2, 0, 3, 4 ] ) p = -2 elif ( test == 1 ): d = np.array ( [ 9, 8, 7, 6, 5, 4, 3 ] ) p = 3 elif ( test == 2 ): d = np.array ( [ 3, 1, 4, 1, 5, 9, 2, 6, 5 ] ) p = 0 r8 = digits_decimal_to_r8 ( d, p ) print ( '' ) print ( ' Given decimal digits:' ) print ( d ) print ( ' and "decimal place" p = ', p ) print ( ' corresponding value is r8 = ', r8 ) return def digits_sexagesimal_print ( d, p ): #*****************************************************************************80 # ## digits_sexagesimal_print() prints a sexagesimal value. # # Discussion: # # R = d(0) * 60^p + d(1) * 60^(p-1) + ... + d(n-1) * 60^(p+1-n) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # # Input: # # integer D(N): the digits # # integer P: the power of 60 multiplying the first digit. # n = len ( d ) p1 = max ( p, 0 ) p2 = min ( p + 1 - n, 0 ) for p3 in range ( p1, p2 - 1, -1 ): if ( p + 1 - n <= p3 and p3 <= p ): i = p - p3 if ( p3 == 0 ): print ( '%02d.' % ( d[i] ), end = '' ) elif ( p3 == p2 ): print ( '%02d' % ( d[i] ), end = '' ) else: print ( '%02d,' % ( d[i] ), end = '' ) else: if ( p3 == 0 ): print ( '00.', end = '' ) else: print ( '00,', end = '' ) print ( '' ) return def digits_sexagesimal_print_test ( ): #*****************************************************************************80 # ## digits_sexagesimal_print_test() tests digits_sexagesimal_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'digits_sexagesimal_print_test():' ) print ( ' digits_sexagesimal_print() prints a real number ' ) print ( ' as a sexagesimal quantity;' ) d = np.array ( [ 11, 12, 0, 14, 15 ] ) print ( ' Sexagesimal digit vector:' ) print ( d ); for p in range ( -5, 6 ): print ( ' p = %2d: ' % ( p ), end = '' ) digits_sexagesimal_print ( d, p ) return def digits_sexagesimal_to_r8 ( d, p ): #*****************************************************************************80 # ## digits_sexagesimal_to_r8() evalulates an N digit sexagesimal digit number. # # Discussion: # # R = d(0) * 60^p + d(1) * 60^(p-1) + ... + d(n-1) * 60^(p+1-n). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # # Input: # # integer D(N): the sexagesimal digits. # # integer P: the power of 60 multiplying the first digit. # # Output: # # real R: the value represented. # power = 60.0 ** p r = 0.0 for c in d: r = r + c * power power = power / 60.0 return r def digits_sexagesimal_to_r8_test ( ): #*****************************************************************************80 # ## digits_sexagesimal_to_r8_test() tests digits_sexagesimal_to_r8(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'digits_sexagesimal_to_r8_test():' ) print ( ' digits_sexagesimal_to_r8() converts base 60 digits of a' ) print ( ' number into a real value;' ) for test in range ( 0, 3 ): if ( test == 0 ): d = np.array ( [ 4, 37, 46, 40 ] ) p = 3 elif ( test == 1 ): d = np.array ( [ 3, 8, 29, 44, 0, 47, 25 ] ) p = 0 elif ( test == 2 ): d = np.array ( [ 1, 24, 51, 10, 7, 46, 6, 4 ] ) p = 0 r8 = digits_sexagesimal_to_r8 ( d, p ) print ( '' ) print ( ' Given sexagesimal digits:' ) print ( d ) print ( ' and sexagesimal "decimal place" p = ', p ) print ( ' corresponding value is r8 = ', r8 ) return def hex_digit_to_i4 ( c ): #*****************************************************************************80 # ## hex_digit_to_i4() converts a hexadecimal digit to an I4. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 June 2022 # # Author: # # John Burkardt # # Input: # # character C, the hexadecimal digit. # ' ' or # '0' through '9', or # 'A' through 'F', or # 'a' through 'f'. # # Output: # # integer I4, the corresponding integer, or -1 if C was illegal. # k = ord ( c ) if ( ord ( '0' ) <= k and k <= ord ( '9' ) ): i4 = k - ord ( '0' ) elif ( ord ( 'a' ) <= k and k <= ord ( 'f' ) ): i4 = 10 + k - ord ( 'a' ) elif ( ord ( 'A' ) <= k and k <= ord ( 'F' ) ): i4 = 10 + k - ord ( 'A' ) elif ( c == ' ' ): i4 = 0 else: i4 = -1 return i4 def hex_digit_to_i4_test ( ): #*****************************************************************************80 # ## hex_digit_to_i4_test() tests hex_digit_to_i4(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 August 2023 # # Author: # # John Burkardt # print ( '' ) print ( 'hex_digit_to_i4_test():' ) print ( ' hex_digit_to_i4() converts hex_digit -> integer' ) print ( '' ) print ( ' Hex -> I4 -> Hex' ) print ( '' ) for h1 in [ ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F', '?' ]: i4 = hex_digit_to_i4 ( h1 ) h2 = i4_to_hex_digit ( i4 ) print ( ' "%s" %2d "%s"' % ( h1, i4, h2 ) ) return def i4_length ( i4 ): #*****************************************************************************80 # ## i4_length() computes the number of characters needed to print an I4. # # Example: # # I4 I4_LENGTH # # 0 1 # 1 1 # -1 2 # 1952 4 # 123456 6 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # # Input: # # integer I, the integer whose length is desired. # # Output: # # integer VALUE, the number of characters required # to print the integer. # # # Ensure that I4 is an integer. # i4 = int ( i4 ) if ( i4 < 0 ): value = 1 i4 = - i4 elif ( i4 == 0 ): value = 1 return value else: value = 0 while ( i4 != 0 ): value = value + 1 i4 = ( i4 // 10 ) return value def i4_length_test ( ): #*****************************************************************************80 # ## i4_length_test() tests i4_length(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # import numpy as np import platform test_num = 6 i4_test = np.array ( [ 0, 1, -1, 140, -1952, 123456 ], dtype = int ) print ( '' ) print ( 'i4_length_test()' ) print ( ' i4_length() computes the "length" of an integer.' ) print ( '' ) print ( ' I4 Length' ) print ( '' ) for test in range ( 0, test_num ): i4 = i4_test[test] j4 = i4_length ( i4 ) print ( ' %8d %8d' % ( i4, j4 ) ) return def i4_to_a ( i ): #*****************************************************************************80 # ## i4_to_a() returns the I-th alphabetic character. # # Example: # # I A # # -8 ' ' # 0 ' ' # 1 'A' # 2 'B' # .. # 26 'Z' # 27 'a' # 52 'z' # 53 ' ' # 99 ' ' # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 September 2024 # # Author: # # John Burkardt # # Input: # # integer I, the index of the letter to be returned. # 0 is a space; # 1 through 26 requests 'A' through 'Z', (ASCII 65:90); # 27 through 52 requests 'a' through 'z', (ASCII 97:122); # # Output: # # character A, the requested alphabetic letter. # if ( i <= 0 ): a = ' ' elif ( 1 <= i and i <= 26 ): a = chr ( ord ( 'A' ) + i - 1 ) elif ( 27 <= i and i <= 52 ): a = chr ( ord ( 'a' ) + i - 27 ) else: a = ' ' return a def i4_to_hex_digit ( i ): #*****************************************************************************80 # ## i4_to_hex_digit() converts a (small) I4 to a hexadecimal digit. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 February 2010 # # Author: # # John Burkardt # # Input: # # integer I, the integer, between 0 and 15. # # Output: # # character CH, the hexadecimal representation of the integer. # if ( 0 <= i and i <= 9 ): ch = chr ( ord ( '1' ) + i - 1 ) elif ( 10 <= i and i <= 15 ): ch = chr ( ord ( 'A' ) + i - 10 ) else: ch = '?' return ch def i4_to_hex_digit_test ( ): #*****************************************************************************80 # ## i4_to_hex_digit_test() tests i4_to_hex_digit(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 June 2022 # # Author: # # John Burkardt # print ( '' ) print ( 'i4_to_hex_digit_test():' ) print ( ' i4_to_hex_digit() converts integer -> hex digit' ) print ( '' ) print ( ' I4 Hex I4' ) print ( '' ) for i4 in range ( 0, 16 ): h = i4_to_hex_digit ( i4 ) i42 = hex_digit_to_i4 ( h ) print ( ' %2d "%s" %2d' % ( i4, h, i42 ) ) return def i4_to_isbn_digit ( i ): #*****************************************************************************80 # ## i4_to_isbn_digit() converts an integer to an ISBN digit. # # Discussion: # # Only the integers 0 through 10 can be input. The representation # of 10 is 'X'. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # # Input: # # integer I, an integer between 0 and 10. # # Output: # # character VALUE, the ISBN character code of the integer. # If I is illegal, then VALUE is set to '?'. # if ( i == 0 ): value = '0' elif ( i == 1 ): value = '1' elif ( i == 2 ): value = '2' elif ( i == 3 ): value = '3' elif ( i == 4 ): value = '4' elif ( i == 5 ): value = '5' elif ( i == 6 ): value = '6' elif ( i == 7 ): value = '7' elif ( i == 8 ): value = '8' elif ( i == 9 ): value = '9' elif ( i == 10 ): value = 'X' else: value = '?' return value def i4_to_isbn_digit_test ( ): #*****************************************************************************80 # ## i4_to_isbn_digit_test() tests i4_to_isbn_digit(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'i4_to_isbn_digit_test():' ) print ( ' i4_to_isbn_digit() converts digits 0 to 10 to an ISBN digit.' ) print ( '' ) for i4 in range ( 0, 11 ): c = i4_to_isbn_digit ( i4 ) print ( ' %8d "%c"' % ( i4, c ) ) return def i4_to_month_abb ( m ): #*****************************************************************************80 # ## i4_to_month_abb() returns the abbreviated name of a given month. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 August 2023 # # Author: # # John Burkardt # # Input: # # integer M: the month index, between 0 and 11. # # Output: # # string month_abb: the abbreviated month's name. # name_abb = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] if ( m < 0 or 11 < m ): month_abb = '?' else: month_abb = name_abb[m] return month_abb def i4_to_month_abb_test ( ): #*****************************************************************************80 # ## i4_to_month_abb_test() tests i4_to_month_abb(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 August 2023 # # Author: # # John Burkardt # print ( '' ) print ( 'i4_to_month_abb_test():' ) print ( ' i4_to_month_abb() returns an abbreviated month name.' ) print ( '' ) print ( ' I4 Month abb' ) print ( '' ) for i in range ( -1, 13 ): name = i4_to_month_abb ( i ) print ( ' %2d "%s"' % ( i, name ) ) return def i4_to_month_name ( m ): #*****************************************************************************80 # ## i4_to_month_name() returns the name of a given month. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 August 2023 # # Author: # # John Burkardt # # Input: # # integer M: the month index, between 0 and 11. # # Output: # # string MONTH_NAME, the month's name. # name = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] if ( m < 0 or 11 < m ): month_name = '?' else: month_name = name[m] return month_name def i4_to_month_name_test ( ): #*****************************************************************************80 # ## i4_to_month_name_test() tests i4_to_month_name(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 August 2023 # # Author: # # John Burkardt # print ( '' ) print ( 'i4_to_month_name_test():' ) print ( ' i4_to_month_name() returns a month name.' ) print ( '' ) print ( ' I4 Month name' ) print ( '' ) for i in range ( -1, 13 ): name = i4_to_month_name ( i ) print ( ' %2d "%s"' % ( i, name ) ) return def i4_to_s_roman ( i ): #*****************************************************************************80 # ## i4_to_s_roman() converts an integer to a string of Roman numerals. # # Example: # # I S # # -2 -II <-- Not a Roman numeral # -1 -I <-- Not a Roman numeral # 0 0 <-- Not a Roman numeral # 1 I # 2 II # 3 III # 4 IV # 5 V # 10 X # 20 XX # 30 XXX # 40 XL # 50 L # 60 LX # 70 LXX # 80 LXXX # 90 XC # 100 C # 500 D # 1000 M # 4999 MMMMCMLXLIX # # Discussion: # # To generate numbers greater than 4999, the numeral 'V' had a bar # above it, representing a value of 5000, a barred 'X' represented # 10,000 and so on. # # In the subtractive representation of 4 by 'IV', 9 by 'IX' and so on, # 'I' can only subtract from 'V' or 'X', # 'X' can only subtract from 'L' or 'C', # 'C' can only subtract from 'D' or 'M'. # Under these rules, 1999 cannot be written IMM! # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 04 September 2024 # # Author: # # John Burkardt # # Input: # # integer I, an integer to be converted. If the integer # has absolute value greater than 4999, the string '?' will be returned. # If the integer is 0, then the string '0' will be returned. If # the integer is negative, then a minus sign will precede it, even # though this has nothing to do with Roman numerals. # # Output: # # string S, the representation of the integer as a Roman numeral. # s = '' if ( 4999 < abs ( i ) ): s = '?' return s if ( i == 0 ): s = '0' return s if ( i <= 0 ): s = '-' i = -i while ( 0 < i ) : if ( 1000 <= i ): s = s + 'M' i = i - 1000 elif ( 900 <= i ): s = s + 'CM' i = i - 900 elif ( 500 <= i ): s = s + 'D' i = i - 500 elif ( 400 <= i ): s = s + 'CD' i = i - 400 elif ( 100 <= i ): s = s + 'C' i = i - 100 elif ( 90 <= i ): s = s + 'XC' i = i - 90 elif ( 50 <= i ): s = s + 'L' i = i - 50 elif ( 40 <= i ): s = s + 'XL' i = i - 40 elif ( 10 <= i ): s = s + 'X' i = i - 10 elif ( 9 <= i ): s = s + 'IX' i = i - 9 elif ( 5 <= i ): s = s + 'V' i = i - 5 elif ( 4 <= i ): s = s + 'IV' i = i - 4 else: s = s + 'I' i = i - 1 return s def i4vec_print ( n, a, title ): #*****************************************************************************80 # ## i4vec_print() prints an I4VEC. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the vector. # # integer A(N), the vector to be printed. # # string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( '%6d %6d' % ( i, a[i] ) ) return def i4vec_print_test ( ): #*****************************************************************************80 # ## i4vec_print_test() tests i4vec_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 September 2016 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'i4vec_print_test():' ) print ( ' i4vec_print() prints an I4VEC.' ) n = 4 v = np.array ( [ 91, 92, 93, 94 ], dtype = int ) i4vec_print ( n, v, ' Here is an I4VEC:' ) return def isbn_digit_to_i4 ( c ): #*****************************************************************************80 # ## isbn_digit_to_i4() converts an ISBN digit to an I4. # # Discussion: # # The characters '0' through '9' stand for themselves, but # the character 'X' or 'x' stands for 10. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # # Input: # # character C, the ISBN character code to be converted. # # Output: # # integer VALUE, the numeric value of the character # code, between 0 and 10. This value is returned as -1 if C is # not a valid character code. # if ( c == '0' ): value = 0 elif ( c == '1' ): value = 1 elif ( c == '2' ): value = 2 elif ( c == '3' ): value = 3 elif ( c == '4' ): value = 4 elif ( c == '5' ): value = 5 elif ( c == '6' ): value = 6 elif ( c == '7' ): value = 7 elif ( c == '8' ): value = 8 elif ( c == '9' ): value = 9 elif ( c == 'X' or c == 'x' ): value = 10 else: value = -1 return value def isbn_digit_to_i4_test ( ): #*****************************************************************************80 # ## isbn_digit_to_i4_test() tests isbn_digit_to_i4(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # import numpy as np import platform c_test = np.array ( [ \ '0', '1', '2', '3', '4', \ '5', '6', '7', '8', '9', \ 'X', 'x', 'Y', '*', '?', \ ' ' ] ) print ( '' ) print ( 'isbn_digit_to_i4_test():' ) print ( ' isbn_digit_to_i4() converts an ISBN digit to an I4' ) print ( '' ) for i in range ( 0, 16 ): c = c_test[i] i4 = isbn_digit_to_i4 ( c ) print ( ' "%c" %2d' % ( c, i4 ) ) return def list_to_string ( w ): #*****************************************************************************80 # ## list_to_string() converts a list of characters to a string. # # Discussion: # # w = [ 'H', 'e', 'l', 'l', 'o' } # s = 'Hello' # # Just keep repeating to yourself the idiotic "Python strings are immutable" # # To reverse the process, the corresponding "string_to_list()" would be # # w = list ( s ) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 June 2024 # # Author: # # John Burkardt # # Input: # # list W: a list of characters. # # Output: # # string S: the corresponding string. # s = "".join ( w ) return s def r8_to_digits_decimal ( r, n ): #*****************************************************************************80 # ## r8_to_digits_decimal() computes N decimal digits representing R. # # Discussion: # # R = d(0) * 10^p + d(1) * 10^(p-1) + ... + d(n-1) * 10^(p+1-n) approximately # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 June 2022 # # Author: # # John Burkardt # # Input: # # real R, the number to be represented. # # integer N: the number of digits to compute. # # Output: # # integer D(N): the decimal digits. # # integer P: the power of 10 multiplying the first digit. # import numpy as np d = np.zeros ( n, dtype = int ) p = 0 if ( r == 0.0 ): return d, p # # Try to deal with negative input. # if ( r < 0.0 ): sgn = -1 r = - r else: sgn = +1 # # Determine P such that 10^P <= R < 10^(P+1) # rcopy = r base = 10.0 while ( base <= rcopy ): rcopy = rcopy / base p = p + 1 while ( rcopy < 1.0 ): rcopy = rcopy * base p = p - 1 # # Now read off N digits. # rcopy = r div = base ** p for i in range ( 0, n ): d[i] = int ( np.floor ( rcopy / div ) ) rcopy = ( rcopy % div ) div = div / base # # Multiply by sgn. # d = sgn * d return d, p def r8_to_digits_decimal_test ( ): #*****************************************************************************80 # ## r8_to_digits_decimal_test() tests r8_to_digits_decimal(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 22 June 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8_to_digits_decimal_test():' ) print ( ' r8_to_digits_decimal() converts a real number' ) print ( ' to its representation in base 10;' ) hundredth = 1.0 / 100.0 sqrt2 = np.sqrt ( 2.0 ) million = 1000000.0 for r8 in [ hundredth, sqrt2, np.pi, million ]: n = 8 d, p = r8_to_digits_decimal ( r8, n ) print ( '' ) print ( ' ', n, ' decimal digits of ', r8 ) for i in range ( 0, n ): print ( i, d[i], 10**(p-i) ) return def r8_to_digits_sexagesimal ( r, n ): #*****************************************************************************80 # ## r8_to_digits_sexagesimal() computes N sexagesimal digits representing R. # # Discussion: # # R = d(0) * 60^p + d(1) * 60^(p-1) + ... + d(n-1) * 60^(p+1-n) approximately # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # # Input: # # real R, the number to be represented. # # integer N: the number of digits to compute. # # Output: # # integer D(N): the sexagesimal digits. # # integer P: the power of 60 multiplying the first digit. # import numpy as np d = np.zeros ( n, dtype = int ) p = 0 if ( r == 0.0 ): return d, p # # Try to deal with negative input. # if ( r < 0.0 ): sgn = -1 r = - r else: sgn = +1 # # Determine P such that 60^P <= R < 60^(P+1) # rcopy = r base = 60.0 while ( base <= rcopy ): rcopy = rcopy / base p = p + 1 while ( rcopy < 1.0 ): rcopy = rcopy * base p = p - 1 # # Now read off N digits. # rcopy = r div = base ** p for i in range ( 0, n ): d[i] = int ( np.floor ( rcopy / div ) ) rcopy = ( rcopy % div ) div = div / base # # Multiply by sgn. # d = sgn * d return d, p def r8_to_digits_sexagesimal_test ( ): #*****************************************************************************80 # ## r8_to_digits_sexagesimal_test() tests r8_to_digits_sexagesimal(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 June 2022 # # Author: # # John Burkardt # import numpy as np print ( '' ) print ( 'r8_to_digits_sexagesimal_test():' ) print ( ' r8_to_digits_sexagesimal() converts a real number' ) print ( ' to its representation in base 60;' ) hundredth = 1.0 / 100.0 sqrt2 = np.sqrt ( 2.0 ) million = 1000000.0 for r8 in [ hundredth, sqrt2, np.pi, million ]: n = 8 d, p = r8_to_digits_sexagesimal ( r8, n ) print ( '' ) print ( ' ', n, ' sexagesimal digits of ', r8 ) for i in range ( 0, n ): print ( i, d[i], 60**(p-i) ) return def rat_to_s ( a, b ): #*****************************************************************************80 # ## rat_to_s() returns a left-justified representation of A/B. # # Discussion: # # If the ratio is negative, a minus sign precedes A. # A slash separates A and B. # # Note that if A is nonzero and B is 0, S will # be returned as "Inf" or "-Inf" (Infinity), and if both # A and B are zero, S will be returned as "NaN" # (Not-a-Number). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 02 June 2015 # # Author: # # John Burkardt # # Input: # # integer A, B, the numerator and denominator. # # Output: # # character S(*), a left-justified string # containing the representation of A/B. # # # Take care of simple cases right away. # if ( a == 0 ): if ( b != 0 ): s = '0' else: s = 'NaN' elif ( b == 0 ): if ( 0 < a ): s = 'Inf' else: s = '-Inf' else: s = str ( a ) + '/' + str ( b ) return s def rat_to_s_test ( ): #*****************************************************************************80 # ## rat_to_s_test() tests rat_to_s(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 02 June 2015 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'rat_to_s_test():' ) print ( ' rat_to_s() converts a rational to a string.' ) rat_num = 7 rat_top = np.array ( [ 3, 1, 20, 8, -10, 9, -11 ] ) rat_bot = np.array ( [ 4, 1000, 1, 4, 7, -15, -11 ] ) print ( '' ) print ( ' A B A/B' ) print ( '' ) for i in range ( 0, rat_num ): a = rat_top[i] b = rat_bot[i] s = rat_to_s ( a, b ) print ( ' %10d %10d %s' % ( a, b, s ) ) return def s_digits_count ( s ): #*****************************************************************************80 # ## s_digits_count() counts the digits in a string. # # Discussion: # # The string may include spaces, letters, and dashes, but only the # digits 0 through 9 will be counted. # # Example: # # S => 34E94-70.6 # N <= 7 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # # Input: # # string S, the string. # # Output: # # integer N, the number of digits. # s_len = len ( s ) s_pos = 0 n = 0 while ( s_pos < s_len ): c = s[s_pos] s_pos = s_pos + 1 if ( ch_is_digit ( c ) ): n = n + 1 return n def s_digits_count_test ( ): #*****************************************************************************80 # ## s_digits_count_test() tests s_digits_count(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_digits_count_test():' ) print ( ' s_digits_count() counts the digits in a string.' ) print ( '' ) s = '34E94-70.6' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) s = 'Not a one!' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) s = '#8*k >>>> & SEVEN-0.3' n = s_digits_count ( s ) print ( ' We count %d digits in "%s"' % ( n, s ) ) return def s_escape_tex ( s1 ): #*****************************************************************************80 # ## s_escape_tex() de-escapes TeX escape sequences. # # Discussion: # # In particular, every occurrence of the characters '\', '_', # '^', '{' and '}' will be replaced by '\\', '\_', '\^', # '\{' and '\}'. A TeX interpreter, on seeing these character # strings, is then likely to return the original characters. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 September 2024 # # Author: # # John Burkardt # # Input: # # string S1, the string to be de-escaped. # # Output: # # string S2, a copy of the string, modified to avoid TeX escapes. # s1_length = len ( s1 ) s1_pos = 0 s2_pos = 0 s2 = '' while ( s1_pos < s1_length ): if ( s1[s1_pos] == '\\' or \ s1[s1_pos] == '_' or \ s1[s1_pos] == '^' or \ s1[s1_pos] == '{' or \ s1[s1_pos] == '}' ): s2_pos = s2_pos + 1 s2 = s2 + '\\' s2_pos = s2_pos + 1 s2 = s2 + s1[s1_pos] s1_pos = s1_pos + 1 return s2 def s_escape_tex_test ( ): #*****************************************************************************80 # ## s_escape_tex_test() tests s_escape_tex(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 September 2024 # # Author: # # John Burkardt # print ( '' ) print ( 's_escape_tex_test():' ) print ( ' s_escape_tex() "protects" characters in a string' ) print ( ' that might otherwise be interpreted as TeX' ) print ( ' escape characters.' ) s1 = 'The file A_B.TXT is {I think__so} of size 2^8 or C\B.' s2 = s_escape_tex ( s1 ) print ( '' ) print ( ' Original string: "' + s1 + '"' ) print ( ' Modified string: "' + s2 + '"' ) s1 = 'file_name: Fat T.' s2 = s_escape_tex ( s1 ) print ( '' ) print ( ' Original string: "' + s1 + '"' ) print ( ' Modified string: "' + s2 + '"' ) return def s_len_trim ( s ): #*****************************************************************************80 # ## s_len_trim() returns the length of a character string to the last nonblank. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 September 2018 # # Author: # # John Burkardt # # Input: # # string S, a string. # # Output: # # integer VALUE, the length of the string to the last nonblank. # n = len ( s ) for i in range ( n - 1, -1, -1 ): if ( s[i] != ' ' ): value = i + 1 return value value = 0 return value def s_len_trim_test ( ): #*****************************************************************************80 # ## s_len_trim_test() tests s_len_trim(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 September 2018 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_len_trim_test():' ) print ( ' s_len_trim() returns the length of string to the last nonblank.' ) print ( '' ) print ( ' LEN S_LEN_TRIM ---------S---------' ) print ( '' ) s = 'Hi, Bob!' l1 = len ( s ) l2 = s_len_trim ( s ) print ( ' %2d %2d "%s"' % ( l1, l2, s[0:l1] ) ) s = ' How are you? ' l1 = len ( s ) l2 = s_len_trim ( s ) print ( ' %2d %2d "%s"' % ( l1, l2, s[0:l1] ) ) s = ' ' l1 = len ( s ) l2 = s_len_trim ( s ) print ( ' %2d %2d "%s"' % ( l1, l2, s[0:l1] ) ) return def s_quote ( s1, mark ): #*****************************************************************************80 # ## s_quote() "quotes" a string. # # Discussion: # # Actually, it simply puts the string MARK before and after the string S1. # # Sometimes, when you print a string, you want to put quote marks around it. # This is one way to do that. # # Examples: # # S1 MARK S2 # -------- ---- ---------- # Hi, Bob! " "Hi, Bob!" # De Loop LoopDeLoop # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # # Input: # # string S1, a string to be "quoted". # # string MARK, the "quote mark". # # Output: # # string S2, the "quoted" string. # s2 = mark + s1 + mark return s2 def s_quote_test ( ): #*****************************************************************************80 # ## s_quote_test() tests s_quote(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_quote_test():' ) print ( ' s_quote() quotes a string S1 with a mark MARK.' ) print ( '' ) print ( ' ----S1---- ---MARK--- ----S2----' ) print ( '' ) s1 = 'Hi, Bob!' mark = '"' s2 = s_quote ( s1, mark ) print ( ' %-10s %-10s %-10s' % ( s1, mark, s2 ) ) s1 = 'De' mark = 'Loop' s2 = s_quote ( s1, mark ) print ( ' %-10s %-10s %-10s' % ( s1, mark, s2 ) ) return def s_to_caesar ( s1, k ): #*****************************************************************************80 # ## s_to_caesar() applies a Caesar shift cipher to a string. # # Discussion: # # The Caesar shift cipher incremented each letter by 1, with Z going to A. # # This function can apply a Caesar shift cipher to a string of characters, # using an arbitrary shift K, which can be positive, negative or zero. # # Letters A through Z will be shifted by K, mod 26. # Letters a through z will be shifted by K, mod 26. # Non-alphabetic characters are unchanged. # # s2 = s_to_caesar ( s1, 1 ) will apply the traditional Caesar cipher. # s3 = s_to_caesar ( s2, -1 ) will decipher the result. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2015 # # Author: # # John Burkardt # # Input: # # string S1, a string of characters. # # integer K, the increment. # # Output: # # string S2, the string of enciphered characters. # iacap = ord ( 'A' ) ialow = ord ( 'a' ) s1_length = len ( s1 ) s2 = '' for i in range ( 0, s1_length ): i1 = ord ( s1[i] ) if ( iacap <= i1 and i1 <= iacap + 25 ): i2 = ( ( i1 + k - iacap ) % 26 ) + iacap s2 = s2 + chr ( i2 ) elif ( ialow <= i1 and i1 <= ialow + 25 ): i2 = ( ( i1 + k - ialow ) % 26 ) + ialow s2 = s2 + chr ( i2 ) else: s2 = s2 + s1[i] return s2 def s_to_caesar_test ( ): #*****************************************************************************80 # ## s_to_caesar_test() tests s_to_caesar(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_to_caesar_test():' ) print ( ' s_to_caesar() applies a Caesar shift cipher to a string.' ) print ( '' ) print ( ' S2 = S_TO_CAESAR ( S1, K ), varying K.' ) print ( '' ) print ( ' K ---------------S1---------------- ---------------S2----------------' ) print ( '' ) for k in range ( -5, 6 ): s1 = 'A man, a plan, a canal: Panama!' s2 = s_to_caesar ( s1, k ) print ( ' %2d "%s" "%s"' % ( k, s1, s2 ) ) print ( '' ) print ( ' S2 = S_TO_CAESAR ( S1, K ).' ) print ( ' S3 = S_TO_CAESAR ( S2, -K )' ) print ( '' ) print ( ' K ------------S1------------ ------------S2------------ ------------S3------------' ) print ( '' ) for k in range ( -5, 6 ): s1 = 'The key is under the mat' s2 = s_to_caesar ( s1, k ) s3 = s_to_caesar ( s2, -k ) print ( ' %2d "%s" "%s" "%s"' % ( k, s1, s2, s3 ) ) return def s_to_digits ( s, n ): #*****************************************************************************80 # ## s_to_digits() extracts N digits from a string. # # Discussion: # # The string may include spaces, letters, and dashes, but only the # digits 0 through 9 will be extracted. # # Example: # # S => 34E94-70.6 # N => 5 # D <= (/ 3, 4, 9, 4, 7 /) # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # # Input: # # string S, the string. # # integer N, the number of digits to extract. # # Output: # # integer DVEC(N), the extracted digits. # import numpy as np s_len = len ( s ) s_pos = 0 d_pos = 0 dvec = np.zeros ( n, dtype = int ) while ( d_pos < n ): if ( s_len <= s_pos ): print ( '' ) print ( 's_to_digits(): Fatal error!' ) print ( ' Could not read enough data from string.' ) raise Exception ( 's_to_digits(): Fatal error!' ); c = s[s_pos] s_pos = s_pos + 1 if ( ch_is_digit ( c ) ): d = ch_to_digit ( c ) dvec[d_pos] = d d_pos = d_pos + 1 return dvec def s_to_digits_test ( ): #*****************************************************************************80 # ## s_to_digits_test() tests s_to_digits(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 09 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_to_digits_test():' ) print ( ' s_to_digits: string -> digit vector' ) s = '34E94-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 5 dvec = s_to_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 5 digits:' ) s = '34E94-70.6' print ( '' ) print ( ' Test string: "%s"' % ( s ) ) n = 7 dvec = s_to_digits ( s, n ) i4vec_print ( n, dvec, ' Extracted 7 digits:' ) return 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 MIT license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # # Input: # # string S, the string. # # integer N, the number of digits to extract. # # Output: # # integer DVEC(N), the extracted digits. # import numpy as np s_len = len ( s ) s_pos = 0 d_pos = 0 dvec = np.zeros ( n, dtype = int ) 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.' ) raise Exception ( '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 MIT license. # # Modified: # # 16 September 2015 # # Author: # # John Burkardt # import platform print ( '' ) print ( 's_to_isbn_digits_test():' ) 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:' ) return def s_to_rot13 ( s1 ): #*****************************************************************************80 # ## s_to_rot13() "rotates" the alphabetical characters in a string by 13 positions. # # Discussion: # # Two applications of the routine will return the original string. # # Examples: # # Input: Output: # # abcdefghijklmnopqrstuvwxyz nopqrstuvwxyzabcdefghijklm # Cher Pure # James Thurston Howell Wnzrf Guhefgba Ubjryy # 0123456789 56789012345 # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # # Input: # # string S1, a string to be "rotated". # # Output: # # string S2, the rotated string. # s1_length = len ( s1 ) s2 = '' for i in range ( 0, s1_length ): s2 = s2 + ch_to_rot13 ( s1[i] ) return s2 def s_to_rot13_test ( ): #*****************************************************************************80 # ## s_to_rot13_test() tests s_to_rot13(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # print ( '' ) print ( 's_to_rot13_test():' ) print ( ' s_to_rot13(): applies the ROT13 cipher to a string.' ) print ( '' ) print ( ' S2 = S_TO_ROT13 ( S1 ).' ) print ( '' ) print ( ' -------------------S1------------------- -------------------S2-------------------' ) print ( '' ) s1 = 'abcdefghijklmnopqrstuvwxyz' s2 = s_to_rot13 ( s1 ) s1 = s_quote ( s1, '"' ) s2 = s_quote ( s2, '"' ) print ( ' %-40s %-40s' % ( s1, s2 ) ) s1 = 'Cher' s2 = s_to_rot13 ( s1 ) s1 = s_quote ( s1, '"' ) s2 = s_quote ( s2, '"' ) print ( ' %-40s %-40s' % ( s1, s2 ) ) s1 = 'James Thurston Howell III' s2 = s_to_rot13 ( s1 ) s1 = s_quote ( s1, '"' ) s2 = s_quote ( s2, '"' ) print ( ' %-40s %-40s' % ( s1, s2 ) ) s1 = 'The bill is $1,205,837.49 so pay now!' s2 = s_to_rot13 ( s1 ) s1 = s_quote ( s1, '"' ) s2 = s_quote ( s2, '"' ) print ( ' %-40s %-40s' % ( s1, s2 ) ) print ( '' ) print ( ' S2 = S_TO_ROT13 ( S1 ).' ) print ( ' S3 = S_TO_ROT13 ( S2 ).' ) print ( '' ) print ( ' -------------------S1------------------- -------------------S3-------------------' ) print ( '' ) s1 = 'abcdefghijklmnopqrstuvwxyz' s2 = s_to_rot13 ( s1 ) s3 = s_to_rot13 ( s2 ) s1 = s_quote ( s1, '"' ) s3 = s_quote ( s3, '"' ) print ( ' %-40s %-40s' % ( s1, s3 ) ) s1 = 'Cher' s2 = s_to_rot13 ( s1 ) s3 = s_to_rot13 ( s2 ) s1 = s_quote ( s1, '"' ) s3 = s_quote ( s3, '"' ) print ( ' %-40s %-40s' % ( s1, s3 ) ) s1 = 'James Thurston Howell III' s2 = s_to_rot13 ( s1 ) s3 = s_to_rot13 ( s2 ) s1 = s_quote ( s1, '"' ) s3 = s_quote ( s3, '"' ) print ( ' %-40s %-40s' % ( s1, s3 ) ) s1 = 'The bill is $1,205,837.49 so pay now!' s2 = s_to_rot13 ( s1 ) s3 = s_to_rot13 ( s2 ) s1 = s_quote ( s1, '"' ) s3 = s_quote ( s3, '"' ) print ( ' %-40s %-40s' % ( s1, s3 ) ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) chrpak_test ( ) timestamp ( )