Skip to content

Instantly share code, notes, and snippets.

@tiramiseb
Created November 24, 2017 09:58
Show Gist options
  • Save tiramiseb/f3b019ff2a984d1047f599ae56a0c110 to your computer and use it in GitHub Desktop.
Save tiramiseb/f3b019ff2a984d1047f599ae56a0c110 to your computer and use it in GitHub Desktop.
Calculate a Tesla Model 3 VIN, useful to check where they are in te production process
#!/usr/bin/env python
# coding: utf-8
import sys
# A Tesla Model 3 VIN seems to look like:
#
# check digit
# |
# | rank
# ↓ ↓↓↓↓↓↓
# 5YJ3E1EA8HF000007
#
# Taking https://en.wikibooks.org/wiki/Vehicle_Identification_Numbers_(VIN_codes)/Check_digit in consideration :
#
# VIN 5 Y J 3 E 1 E A x H F a b c d e f
# value 5 8 1 3 5 1 5 1 - 8 6 a b c d e f
# weight 8 7 6 5 4 3 2 10 - 9 8 7 6 5 4 3 2
# products 40 56 6 15 20 3 10 10 - 72 48 a×7 b×6 c×5 d×4 e×3 f×2
#
# check digit =
# (40+56+6+15+20+3+10+10+72+48+a×7+b×6+c×5+d×4+e×3+f×2) % 11
# 280+a×7+b×6+c×5+d×4+e×3+f×2) % 11
#
# ... if the check digit calculates to 10, then it is replaced by X
#
# Now, take all that and make a beautiful function:
def calculate_model3_vin(rank):
rank = "{:06d}".format(int(rank))[-6:]
digit = (280 + int(rank[0])*7 + int(rank[1])*6 + int(rank[2])*5 + \
int(rank[3])*4 + int(rank[4])*3 + int(rank[5])*2) % 11
if digit == 10:
digit = "X"
return int(rank), "5YJ3E1EA{}HF{}".format(digit, rank)
if __name__ == "__main__":
rank, vin = calculate_model3_vin(sys.argv[1])
print "The VIN for rank {} is {}".format(rank, vin)
print "You can check if it exists at:"
print "https://www.nhtsa.gov/recalls?vin={}".format(vin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment