I need to implement alphanumeric increment algorithm like AAA001 should become AAA002 AAA999 should become AAB000 and so on.
All alphabets are in uppercase and letters are from 0-9. It can contain alphabet or letter at any position in the alphanumeric string.
There are some rules though, like some 000 or 666 should not come in a series. That can be done later on but I am in need of basic logic to implement the algorithm.
I see many people did not understand my question. Just imagine the Plate Number of a vehicle which is nothing but a alphanumeric series which can have some excluded characters like BB6660 -> 666, triple 6 in between is not allowed.
It should support different formats like-
@@@##
@#@@##
1@#@@##
@@@@####
##@@#@
@ means alphabet A-Z
# means numbers 0-9
Examples:
AFG99 + 1= AFH00
A2GF23 + 1 = A2GF24
1A9AU99 + 1 = 1A9AV00
AAZZ9999 + 1 = ABAA0000
11AA9Z + 1 = 11AB0A
I need some sort of mathematical solution so that I can do math and increment it easily without using character increment.
I also need the count between the two ranges like how many counts are there between AAA003 and AA010 ?
AAA010 - AAA003 = 7
I would appreciate the help..
Here's 3 solutions: the first two are somewhat arithmetic incrementations while the third is more a character manipulations.
The 3 implementations all pass the same unit tests:
First:
Second:
Third :
This works with your current "reqs". Compared to how the question what asked at the beginning, this is not just a "left-part composed of letter" + "right part composed of digits". Now, it's "anything goes", and letters roll from A to Z to A, while digits from 0 to 9 to 0. When a letter reaches Z, it is reset to A, then the digit/letter on its left is incremented.
If all numbers are incremented, it does not add a new digit on the left. You did not mention that in your question, but I'm sure you can figure this out from here:
As for the "count", your example isn't sufficient to grasp the logic. Does it count only numbers ? what about the letters ? Does it follow a baseXx ?
how can AA010-AAA003 = 7, the 3 A's versus 2 A's do no matter ? I feel this is rather on you to understand what are your requirements (ie: homework..)
Technically, this answers the question as it was asked originally (with many modifications along the way).
[doing] math and increment [alphanumerics] without using character increment
can be decomposed into simpler problems: consider your "alphanumerics" integers coded with a mixed base. This would leave conversion from alphanumeric to integer, (math
) operations (increment, count between: difference/subtraction) on integers, and conversion from integer to alphanumeric.For integer, have a look at LongAdder. For the conversions, you need to keep a sequence of bases to use - I recommend to start from unit/the little end and just use something easily iterable and able to keep (small) integers. Going from alphanumeric to integer, start with zero. For each character, add its value. For a letter, keep the number of different letters (alphabet size, 26 with the Latin alphabet as used now) as the base to use for that place, for a digit the number of different digits (ten, usually). If another character follows, multiply by that base and repeat.
Conversion from integer to alphanumeric would be the usual divide&remainder (using the bases as kept) - with a catch: how do you handle a carry from the most significant position?
There are some rules though, like some 000 or 666 should not come in a series. That can be done later on
and will killmath
.