1

Can anyone help with the logic for the below scenario please?

**Input -> Output**
00000 --> 00001
00009 --> 0000A
0000Z --> 00010
..
..
0002Z --> 00030
00039 --> 0003A

Any suggestions please?

EDIT Thanks all for your suggestions. :) This is what I tried and it works, not sure if could break at some condition though? :/

    public static void Main(string[] args)
    {
        string number = "0001Z";            
        var result = Increment(number);

    }

    private static String Increment(String number)
    {
        String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";            
        char lastChar = number[number.Length - 1];
        string fragment = number.Substring(0, number.Length - 1);

        if (chars.IndexOf(lastChar) < 35)
        {
            lastChar = chars[chars.IndexOf(lastChar) + 1];
           string nextNumber = fragment + lastChar;
            return nextNumber;
        }
        return Increment(fragment) + '0';            
    }

PS: Increment an index that uses numbers and characters (aka Base36 numbers) - this is where I got it from, so may be duplicate question.. sorry.

Community
  • 1
  • 1

1 Answers1

1

I have a couple of methods that I made to convert an integer to/from a different base. I'm sure they probably could be improved. But they may get you started. So convert your base 36 "number" to int, increment by 1, then convert back to base 36. This method uses recursion which is probably not necessary. I'd be interested to know if there are any more efficient methods for this.

These method assumes no other characters besides 0-9 and A-Z

void Main()
{
    string input = "0000Z";
    int value = baseToInt(input, 36);
    value++;
    string output = intToBase(value, 36).PadLeft(5, '0');

    Console.WriteLine("Input: {0}", input);
    Console.WriteLine("Output: {0}", output);
}

public string intToBase(int input, int @base)
{
    var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    if (@base < 2 || @base > 36)
    {
        throw new ArgumentOutOfRangeException("base", "Must specify a base between 2 and 36, inclusive");
    }

    if (input < @base && input >= 0)
    {
        return digits[input].ToString();
    }
    else
    {
        return intToBase(input / @base, @base) + digits[input % @base].ToString();
    }
}

public int baseToInt(string input, int @base)
{
    var digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    if (@base < 2 || @base > 36)
    {
        throw new ArgumentOutOfRangeException("base", "Must specify a base between 2 and 36, inclusive");
    }

    var digitsInBase = digits.Substring(0, @base);

    if (input.Any(c => !digitsInBase.Contains(c)))
    {
        throw new ArgumentOutOfRangeException("input", string.Format("Input is not a valid base {0} number", @base));
    }

    return (int)input.Select((c, i) => Math.Pow(@base, input.Length - (i + 1)) * digitsInBase.IndexOf(c)).Sum();

}

Output:

Input: 0000Z
Output: 00010

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48