text: # -*- coding: utf-8 -*-
  """Fun with Car Plate Numbers!
  
  Automatically generated by Colaboratory.
  
  Original file is located at
      https://colab.research.google.com/drive/1kgkowDKhtVaVJl4hacZrjE03nekWu9C_
  
  Good morning! You have completed the math trail on car plate numbers in a somewhat (semi-)automated way.
  
  Can you actually solve the same tasks with code? Read on and you will be amazed how empowering programming can be to help make mathematics learning more efficient and productive! :)
  
  # Task
  
  Given the incomplete car plate number `SLA9??2H`
  
  Find the missing ?? numbers.
  
  A valid Singapore car plate number typically starts with 3 letters, followed by 4 digits and ending with a 'check' letter. 
  
  For example, for the valid car plate number is 'SDG6136T',
  
  -  The first letter is 'S' for Singapore. 
  - The next two letters and the digits are used to compute the check letter, using the following steps:
    - Ignoring the first letter 'S', the letters are converted to their positions in the alphabet. For example, 'D' is 4, 'G' is 7 and 'M' is 13. 
    - The converted letters and the digits form a sequence of 6 numbers. For example, 'DG6136' will give (4, 7, 6, 1, 3, 6).
    - The sequence of 6 numbers is multiplied term by term by the sequence of 6 weights (9, 4, 5, 4, 3, 2) respectively, summed up and then divided by 19 to obtain the remainder. 
    - For example, '476136' will give 4x9 + 7x4 + 6x5 + 1x4 + 3x3 + 6x2 = 119, and this leaves a remainder of 5 after dividing by 19.
    - The 'check' letter is obtained by referring to the following table. Thus the check letter corresponding to remainder 5 is T.
  ``` 
  | Remainder | 'check' letter | Remainder | 'check' letter | Remainder | 'check' letter |
  | 0         | A              |  7        | R              | 13        | H              |
  | 1         | Z              |  8        | P              | 14        | G              |
  | 2         | Y              |  9        | M              | 15        | E              |
  | 3         | X              | 10        | L              | 16        | D              |
  | 4         | U              | 11        | K              | 17        | C              |
  | 5         | T              | 12        | J              | 18        | B              |
  | 6         | S              |           |                |           |                |
  ```
  Reference: https://sgwiki.com/wiki/Vehicle_Checksum_Formula
  
  Pseudocode
  ```
  FOR i = 0 to 99
    Car_Plate = 'SJT9' + str(i) + '2H'
      IF Check_Letter(Car_Plate) is True
        print (Car_Plate) on screen
      ENDIF
    NEXT
  ```
  """
  
  # we need to store the mapping from A to 1, B to 2, etc. 
  # for the letters part of the car plate number
  # a dictionary is good for this purpose
  letter_map = {}
  for i in range(27): # 26 alphabets
    char = chr(ord('A') + i)
    letter_map[char] = i + 1
  #print(letter_map) # this will output {'A':1, 'B':2, 'C':3, ..., 'Z':26}
  
  # we also need to store the mapping from remainders to the check letter
  # and we can also use a dictionary! :)
  check_map = {0:'A', 1:'Z', 2:'Y', 3:'X', 4:'U', 5:'T', 6:'S', 7:'R', 8:'P',	\
               9:'M', 10:'L', 11:'K',	12:'J', 13:'H', 14:'G', 15:'E',	16:'D',	\
               17:'C', 18:'B'}
  
  # we define a reusable Boolean function to generate the check letter and
  # check if it matches the last letter of the car plate number
  def check_letter(car_plate):
    weights = [9, 4, 5, 4, 3, 2]
    total = 0
    for i in range(len(car_plate)-1):
      if i < 2: # letters
        num = letter_map[car_plate[i]]
      else: # digits
        num = int(car_plate[i])
      total += num * weights[i]
    remainder = total % 19
    return check_map[remainder] == car_plate[-1]
  
  #main
  car_plate = 'DG6136T' # you can use this to verify the given example
  if check_letter(car_plate):
    print('S' + car_plate, car_plate[3:5])
  print()
  for i in range(100): # this loop repeats 100 times for you! :)
    car_plate = 'LA9' + str(i).zfill(2) + '2H' # 'LA9002H', 'LA9012H', ...
    if check_letter(car_plate):
      print('S' + car_plate, car_plate[3:5])
  
  #main
  for i in range(100):
    car_plate = 'LA' + str(i).zfill(2) + '68Y'
    if check_letter(car_plate):
      print('S' + car_plate, car_plate[2:4])
  
  '0'.zfill(2)
  
  """# Challenge
  - How many car_plate numbers start with SMV and end with D?
  """
  
  #main
  count = 0
  for i in range(10000):
    car_plate = 'MV' + str(i).zfill(4) + 'D'
    if check_letter(car_plate):
      count += 1
  print(count)
  
  #main
  wanted = []
  for i in range(10000):
    car_plate = 'MV' + str(i).zfill(4) + 'D'
    if check_letter(car_plate):
      print('S' + car_plate, end=' ')
      wanted.append('S' + car_plate)
  print(len(wanted))
  
  """# More challenges!
  
  Suggest one or more variations of problems you can solve with car plate numbers using the power of Python programming. Some ideas include:
  * Check if a given car plate number is valid
  * Which valid car plate numbers have a special property (eg prime number, contains at least two '8' digits, does not contain the lucky number 13, etc.)
  * If there are the same number of available car plate numbers each series (eg SMV and SMW)
  * (your idea here)
  
  Submit a pull request with your ideas and/or code to contribute to learning Mathematics using programming to benefit the world! :)
  """
  
  
  
  """# This is really more than car plate numbers!
  
  You have just learned an application of mathematics called modulus arithmetic in generating check letters/digits. Do you know that actually the following are also applications of modulus arithmetic?
  * Singapore NRIC numbers (http://www.ngiam.net/NRIC/NRIC_numbers.ppt)
  * international ISBNs (https://en.wikipedia.org/wiki/International_Standard_Book_Number)
  * credit card numbers (https://en.wikipedia.org/wiki/Luhn_algorithm)
  * universal product codes (https://en.wikipedia.org/wiki/Universal_Product_Code)
  
  Can you research on other applications modulus arithmetic has? Better still, contribute by submitting Python code to unleash the power of automation!
  
  You can submit a pull request by doing one of the following:
  - suggesting a new application for modulus arithmetic
  - creating a new .py file
  - uploading an existing .py file
  
  We look forward to your pull requests! :)
  """