The car registering system of a city assigns two types of numbers:
The Customer ID - it's a natural number between 0 and 17558423 inclusively, which is assigned to the car buyers in the following order: the first customer receives ID 0, the second customer receives ID 1, the third customer receives ID 2, and so on.
Number Plate - assigned to the car and contains the series ( three Latin letters from ‘a’ to ‘z’) and the serial number (three digits from 0 to 9).
Example: aaa001. Each Number Plate is related to the given Customer ID. For example: Number Plate aaa000
is related to Customer ID 0; Number Plate aaa001
is related to Customer ID 1, and so on.
Write a function findPlate
which takes the Customer ID as an argument, calculates the Number Plate corresponding to this ID and returns it as a string, considering the following:
The serial numbers start at 001, end at 999.
The series change once the serial number hits 999. The left letter changes first, alphabetically:
aaa001...aaa999,
baa001...baa999,
...... ,
zaa001...zaa999
As soon as the left letter reaches z and the series z** has moved through all the serial numbers the middle letter changes alphabetically as follows:
aba001...aba999,
bba001... bba999,
cba001...cba999,
......,
zba001...zba999,
aca001...aca999,
bca001...bca999,
As soon as the middle letter and the left letter both reaches z and the series zz* has moved through all its serial numbers, the right letter changes alphabetically as follows:
aab001...aab999,
bab001...bab999,
cab001...cab999,
......,
zab001...zab999,
abb001...abb999,
bbb001...bbb999,
cbb001...cbb999,
......,
zbb001...zbb999,
acb001...acb999,
......,
zzz001...zzz999
When all the possible series and their serial numbers are exhausted the last Number Plate will be zzz999.
Notes:
- No spaces are allowed between the characters in the returned string. So 'abc123' is valid, but 'a b c 1 2 3' is not.
- If the serial number has less than 3 digits, the missing places should be filled with zeroes. Example: serial number '12' should equal '012'; serial number '4' should equal '004'. Customer IDs starts with 0.
Tests
findPlate(3)
findPlate(1487)
findplate(17558423)
findPlate(234567)
Good luck!
This challenge comes from William_2004 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (9)
The problem statement contradicts itself.
First it says that "Number Plate aaa000 is related to Customer ID 0; Number Plate aaa001 is related to Customer ID 1, and so on."
Then it says "The serial numbers start at 001, end at 999", and goes on to show lists of plates with serial numbers in the format xxx001..xxx999.
Together, they mean that number plate aaa000 both does and does not exist.
Here is the simple solution with Python to create mapping letter lists to complete this challenge :).
With the use UTF-8 table
Lowercase alphabet position 97-122
For the 'abz099' Result:
And add 99
I thought to use Elixir since it has built-in base conversion (and not just for the usual powers of two suspects like octal and hex), but theirs assumes the digit sequence is 0..9A..Z, so there is still an impedance mismatch to a..z encoding, and the need (apparently) to bludgeon one's way through the edge cases, particularly 'magic constants' based on knowledge of the ASCII character set.
APL (using Dyalog APL):
(Use the bookmarklet on this post to see the code with APL font and syntax highlighting.)
Demo can be found here.
Explanation:
In JS with
toString
and a custom radix ended up being pretty damn unwieldy...Testn...
seems to agree with the kata test cases
Advantage: avoids hardcoding the number of letters and digits (they could be brought out as params easily)
Disadvantages: all the other things
Seems to be working -
letters = 'abcdefghijklmnopqrstuvwxyz'
def find_the_number_plate(n):
# As serial numbers go from 001 to 999, we take n % 999 and convert it to a 0 padded string
ser = str(n%999 + 1).rjust(3, '0')
# To find the letters, we need to divide by 999 and then repeatedly divide by 26 to get the 3 letters
l = n//999
return letters[l%26]+letters[(l//26)%26]+letters[(l//(26*26))%26] + ser