#! /usr/bin/env python # 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 GNU LGPL license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # # Parameters: # # 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 GNU LGPL license. # # Modified: # # 30 January 2016 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'CH_TO_ROT13_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) 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 ) ) # # Terminate. # print ( '' ) print ( 'CH_TO_ROT13_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ch_to_rot13_test ( ) timestamp ( )