Create a class SingaporeNumbers.
Part 1
A typical vehicle registration number comes in the format xxx #### y:
• x – prefixes
• #### – Numerical series (from 1 to 9999, without leading zeroes)
• y – Checksum
• The checksum letter is calculated by converting the letters into numbers, i.e., where A=1 and Z=26, potentially giving seven individual numbers from each registration plate. However, only two letters of the prefix are used in the checksum. For a three-letter prefix, only the last two letters are used; for a two-letter prefix, both letters are used; for a single letter prefix, the single letter corresponds to the second position, with the first position as 0. For numerals less than four digits, additional zeroes are added in front as placeholders, for example "1" is "0001". SBS 3229 would therefore give 2, 19, 3, 2, 2 and 9 (note that "S" is discarded); E 12 would give 0, 5, 0, 0, 1 and 2. SS 108 would be given as 19, 19, 0, 1, 0, 8.
• 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 SBS 3229, the final letter should be a P; for E 23, the final letter should be a H. SS 11 back letter should be a T. The letters F, I, N, O, Q, V and W are not used as checksum letters. Write a static method car_plate_checksum that returns the checksum from a given string. You should use the try and except blocks to find out is a character in a string is an integer or not. The input string may contain 1-3 letters for prefixes while there can be 1 to 4 digits for the numerical series that follows.
What I have tried:
This answers is not correct
def car_plate_checksum(registration_number):
remainder_mappings = {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"}
weights = [9, 4, 5, 4, 3, 2]
numericals = list()
prefix = ""
for character in registration_number:
try:
numericals.append(int(character))
except ValueError:
prefix += character
prefix = prefix.strip()
if len(prefix) == 3:
effective_prefix = prefix[1:]
elif len(prefix) == 2:
effective_prefix = prefix
elif len(prefix) == 1:
effective_prefix = "0" + prefix
prefix_numericals = []
for prefix_char in effective_prefix:
if prefix_char == "0":
prefix_numericals.append(0)
else:
prefix_numericals.append(ord(prefix_char) - 64)
numericals = [0] * (4 - len(numericals)) + numericals
numericals = prefix_numericals + numericals
elementwise_product = [a*b for a, b in zip(numericals, weights)]
sum_elements = sum(elementwise_product)
remainder = sum_elements % 19
checksum = remainder_mappings[remainder]
return checksum