#! /usr/bin/env python3 # def char_to_num ( ch ): #*****************************************************************************80 # ## char_to_num() ??? # "Assumes all characters are digits or capital letters." n = ord ( ch ) if n <= ord('9'): # digits return n - ord('0') if n < ord('I'): # A-I return n - ord('A') + 1 if n <= ord('R'): # J-R return n - ord('J') + 1 return n - ord('S') + 2 # S-Z def checksum ( vin ): #*****************************************************************************80 # ## checksum() computes the checksum of a VIN. # assert ( len ( vin ) == 17 ) w = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ] t = 0 for i, c in enumerate(vin): t = t + char_to_num(c)*w[i] t = ( t % 11 ) check = 'X' if t == 10 else str(t) return check def validate ( vin ): #*****************************************************************************80 # ## validate() ??? # return checksum(vin) == vin[8] def vin_test ( ): #*****************************************************************************80 # ## vin_test() tests vin(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 September 2022 # # Author: # # John Burkardt # # Reference: # # John D Cook, # Vehicle Identification Number (VIN) Check Sum, # https://www.johndcook.com/blog/2019/09/12/vin-check-sum/ # 12 September 2019. # # John D Cook, # Computing VIN Checksums, # https://www.johndcook.com/blog/2022/09/04/computing-vin-checksums/ # 04 September 2022. # import platform vin_data = [ \ "11111111111111111", \ "5GZCZ43D13S812715", \ "1M8GDM9AXKP042788" ] print ( '' ) print ( 'vin_test():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' vin() verifies a Vehicle Identifical Number (VIN).' ) for vin in vin_data: print ( '"' + vin + '"', end = '' ) print ( ' ', checksum ( vin ), end = '' ) print ( ' ', validate ( vin ) ) # # Terminate. # print ( '' ) print ( 'vin_test():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == "__main__" ): timestamp ( ) vin_test ( ) timestamp ( )