[Community puzzle] Next car license plate?

all you have to use is modulo

For French plates: the letters I, O and U are not present. A special format exists also for the garages/car dealers/automotive professionals with ā€œW Garageā€ format like ā€œW-001-AAā€. WW is also a special format for temporary license plate valid up to 4 months (like WW-001-AA). SS is removed also (left and right side).

Some elements here: https://plaques-immatriculation.info

I pass all the tests before submitting, but after submitting I get a fail on test case 6. I read the forums here on why that happens. But even after reading that I donā€™t know why my code fails. Any chance someone can view my code and give their opinion on the matter?

I know my code isnā€™t the cleanest yet, I am still getting acquainted with things.

1 Like

Ok, solved the problem. User Last Rick said it best in his post thatā€™s in this thread, just search his name. His hint ā€œBest hint I can give is consider the edge case where ā€œcarrying the 1ā€ trickles all the way from the middle to the right to the left side.ā€, You have to cover those cases where something starts out as ZZ, or 999. Also, use modulo :slight_smile:

AA-998-AA
1

I was able to write code that passed all the test cases and passed all the validators. As I thought about it, my code could not pass the test case above. Iā€™d like to suggest adding the above test case to make sure the mistake I made is caught.

Both of my solutions are published.

3 Likes

I added a test and a validator for this case.

I apparently did the same mistake and fixed it :sweat_smile:ā€¦and I tested some published solutions containing the same mistake too :smiley: .

3 Likes

It would have been easier actually if this rule had been included in this challenge, because incrementing plate number step by step is easier (but unfortunately it causes timeout for ā€œvery bigā€ testcase).

1 Like

No, for me itā€™s easier to think mathematically (ie to find a bijection between the set of licence plates and the set of integers).

1 Like

There is a small mistake in the problem statement, the ordering presented is not alphabetical :
console.log('AA-001-AA' < 'AA-001-AB', 'AA-001-AB' < 'AA-002-AA'); // true true

I canā€™t seem to see my mathematical error, can someone help me out (for the 1,000,000 and Big test cases)[code is written in c#]:

Spoiler Alert
class Solution
{
//1 9
//10 99
//100 999
//1000 999A
//26000 999AA
//676000 999AAA
//17576000 999AAAA

    static int convertToNumber(string Entry){
        int Converted = 0;
        //Console.Error.WriteLine("debug: converting from string: {0}", Entry);

        Converted += (int)(Entry[5] - '0');
        Converted += (int)(Entry[4] - '0')*10;
        Converted += (int)(Entry[3] - '0')*100;
        Converted += (int)(Entry[8] - 'A')*1000;
        Converted += (int)(Entry[7] - 'A')*26000;
        Converted += (int)(Entry[1] - 'A')*676000;
        Converted += (int)(Entry[0] - 'A')*17576000;

        //Console.Error.WriteLine("debug: converted to number: {0}", Converted);

        return Converted;
    }

    static string convertToString(int Value){
        List<char> Converted = new List<char>();
        string Output = "";
        
        //Console.Error.WriteLine("debug: converting from number: {0}", Value);

        Converted.Add((char)((Value %10) + '0'));
        Converted.Add((char)(((Value / 10) %10) + '0'));
        Converted.Add((char)(((Value / 100) %10) + '0'));
        Converted.Add((char)(((Value / 1000) %26) + 'A'));
        Converted.Add((char)(((Value / 26000) %26) + 'A'));
        Converted.Add((char)(((Value / 676000) %26) + 'A'));
        Converted.Add((char)(((Value / 17576000) %26) + 'A'));

        //Console.Error.Write("debug: converted to string(0): ");
        foreach( char c in Converted){
        //    Console.Error.Write("{0} ",c);
        }
        //Console.Error.WriteLine("");

        Output += Converted[6];
        Output += Converted[5];
        Output += '-';
        Output += Converted[2];
        Output += Converted[1];
        if ( Converted[0] == '0' && Converted[1] == '0' && Converted[2] == '0' )
            Output += '1';
        else
            Output += Converted[0];
        Output += '-';
        Output += Converted[4];
        Output += Converted[3];

        return Output;
    }


    static void Main(string[] args)
    {
        string x = Console.ReadLine();
        int n = int.Parse(Console.ReadLine());

        Console.Error.WriteLine("debug: starting license: {0} number to iterate: {1}", x, n );

        int Converted = convertToNumber(x);
        Converted = Converted + n + (n/1000);

        string final = convertToString(Converted);
        // Write an answer using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        Console.WriteLine(final);
    }
}

Your code produces the correct answer for:

AA-001-AA
674325

but not for

AA-001-AA
674326

You may investigate from there.

By the way you may format your code properly by using the </> button on the formatting toolbar.

I guess I donā€™t understand. I think youā€™re talking about the wrap from 999 to 000 that occurs every thousand, and I account for that with the ā€œ+ (n/1000)ā€ statement. Iā€™ve been looking at out of sequence or for a triplicate that occurs and I cannot seem to locate it. I ran the numbers you suggested in my console app without a starting license and my returns are:
debug: From License: QS-456-DF To License: QT-780-DD From Number: 294141780
debug: Without Starting License: AA-324-ZY From Number: 674324
debug: From License: QS-456-DF To License: QT-781-DD From Number: 294141781
debug: Without Starting License: AA-325-ZY From Number: 674325
debug: From License: QS-456-DF To License: QT-782-DD From Number: 294141782
debug: Without Starting License: AA-326-ZY From Number: 674326

I appreciate your suggestions, and Iā€™ll try to remember to use the ā€œ</>ā€ button :slight_smile:

Additionally Iā€™ve written this to see if there is an easy to spot sequence issue:

Potential Spoiler
int converted = convertToNumber(x);
List<int> rows = new List<int>();

for (int i = 0; i < 1000000; i++)
{
    int sequence = Convert.ToInt32(convertToString(i+converted).Substring(3, 3));
    rows.Add(sequence);

    //Console.WriteLine("debug: From License: {0} To License: {1} From Number: {2}", x, convertToString(i + converted), i + converted);
    //Console.WriteLine("debug: Without Starting License: {1} From Number: {2}", x, convertToString(i), i);
}

for (int i = 2; i < rows.Count; i++)
{
    if (rows[i] == rows[i - 1] && rows[i] == rows[i - 2])
    {
        Console.WriteLine("found triple at: {0} value: {1}", i, rows[i]);
    }

    if (rows[i] == rows[i - 1] && rows[i] != 1)
    {
        Console.WriteLine("found out of sequence duplicate at: {0} value: {1}", i, rows[i]);
    }

    if (rows[i] != (rows[i - 1] + 1) && rows[i - 1] != 1 && rows[i - 1] != 999)
    {
        Console.WriteLine("found out of sequence at: {0} value: {1}", i, rows[i]);
    }

    //Console.WriteLine("Value: {0} at Row: {1}", rows[i], i);
}

Your code produces the same output to the two cases in my last comment, doesnā€™t it?

No it doesnā€™t :expressionless: I can pass all the tests and validators, but Iā€™m doing something hacky; my value is off somewhere around every 984880 licensesā€¦ I donā€™t know if itā€™s a boundary issue (with my math) or missing some detail. So I added the line to my code to simply pass; but Iā€™d like to know what I did wrong that the math didnā€™t work with simply accounting for the 1/1000 records. 2nd question, is there no one else anymore that does these puzzles but me (am I on my own internet with bots or something)?

Potential Spoiler
Converted + n + (n/1000) + (n/984880);

Either you misread/misunderstand something or youā€™ve changed your code; Iā€™ve no idea why you say your code doesnā€™t produce the same output for the two cases. See the screenshots below:

Look more closely to how the description examples wrap..

AB-000-AA is not in the example listing as it is not a valid plate

1 Like

[Editted]
I didnā€™t understand that you tested by inputting specific test cases, Iā€™ve never done that or thought of doing that. I was testing using my console app with specific input looking for specific output. I didnā€™t see that you had an expected value for the output either of AA-001-ZZ. Thank you for clarifying by using screen shots 5DN1L!

[Also, Iā€™m not allowed to write custom test cases or at least thereā€™s no visible option for me to do it]

Thank you for looking at this :slight_smile:

1 Like

[Editted]
@Jeetee - thank you, and none of my output allows ā€œ000ā€ as an answer.

Indeed, you seemingly compensate for a wrong internal value during the toString process.
My point is that your numbers donā€™t wrap every 1000 values; they wrap every 999 values. So Iā€™m wondering whether your mathematical compensation isnā€™t off because youā€™re using an off-by-one value for wrapping compensation.