Skip to content

Instantly share code, notes, and snippets.

@wei2912
Last active November 11, 2018 10:05
Show Gist options
  • Save wei2912/7f4565e25f1c050590b85c0d17e53830 to your computer and use it in GitHub Desktop.
Save wei2912/7f4565e25f1c050590b85c0d17e53830 to your computer and use it in GitHub Desktop.
import unittest
def checksum_helper(plate):
"""
This helper function converts a license plate comprising an alphabetical
prefix (which can have one to three letters) and a numeral suffix num (which
can have one to four digits) into a tuple of six numbers.
"""
def split_plate(plate):
i = 0
for i in range(len(plate)):
if plate[i].isdigit():
break
return plate[:i], plate[i:]
def numerise(c):
return ord(c) - ord('A') + 1
def digits(num):
ds = list(map(int, num))
return [0] * (4 - len(ds)) + ds
cs, num = split_plate(plate)
c1 = numerise(cs[-2]) if len(cs) > 1 else 0
c2 = numerise(cs[-1])
d1, d2, d3, d4 = digits(num)
return (c1, c2, d1, d2, d3, d4)
def checksum(plate):
"""
Courtesy of Ryan Ch’ng, 16S47 - TA will be Wei En.
A license plate comprises an alphabetical prefix (which can have one to three
letters) and a numeral suffix (which can have one to four digits). The last
letter of a license plate number is a checksum, which is a letter generated
from the rest of the license plate number. Checksums are used to detect errors
in data.
The checksum letter is calculated by converting the letters into numbers,
i.e., where A=1 and Z=26, potentially giving six individual numbers from each
registration plate. A helper function checksum_helper has been provided to
convert the license plate number into a tuple of numbers. Through this
conversion, SBS3229, for example, would give the tuple (2, 19, 3, 2, 2, 9).
Each individual number is then multiplied by 6 fixed numbers (9, 4, 5, 4, 3,
2). These are added up, then divided by 19. The remainder corresponds to one
of the 19 letters used (A, Z, Y, X, U, T, S, R, P, M, L, K, J, H, G, E, D, C,
B), with 'A' corresponding to a remainder of 0, 'Z' corresponding to 1, 'Y'
corresponding to 2 and so on. In the case of SBS3229, the final letter should
be a P. (Note that letters F, I, N, O, Q, V and W are not used as checksum
letters.)
Complete the function checksum which takes in a license plate number and
outputs a checksum letter.
"""
# ts is the 6-digit tuple obtained after conversion from the license plate
ts = checksum_helper(plate)
# TODO: Fill up your code here, and set checksum to the final answer.
checksum = None
# This returns the final checksum letter.
return checksum
class TestChecksum(unittest.TestCase):
def test_checksum(self):
f = checksum
self.assertEqual(f('SBS3229'), 'P')
self.assertEqual(f('TIB838'), 'H')
self.assertEqual(f('SG5999'), 'Z')
self.assertEqual(f('PA2'), 'A')
self.assertEqual(f('PC4071'), 'M')
self.assertEqual(f('S32'), 'H')
self.assertEqual(f('CB4650'), 'B')
self.assertEqual(f('SG35'), 'M')
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment