I need a counter algortihm which use arbitrary given digits for counting purpose.
My code is similar to this:
static char digits[] = {'x','y','z'}; /* Arbitrary number of arbitrary digits. */
int i;
for(i=0; i<100; i++) {
printf("%s\n", get_next());
}
My expected output:
x
y
z
yx
yy
yz
zx
zy
zz
yxx
yxy
yxz
yyx
yyy
yyz
yzx
yzy
yzz
zxx
... and so on
As you see, I need algorithm for implementing get_next() function, so using C language is not the point.
Edit I for clarification purpose:
My get_next() function may similar to this:
char get_next() {
static previous = digits[0];
char *next_number;
/* do something here using previous and digits[] */
return next_number;
}
Note that using get_next(void) or next(previous_number) or next(digits, previous_number) prototype for your function which generates next number is not important for me.
Edit II for clarification purpose:
My real scenario is more complex from the simple example above, I need a generic solution that works with arbitrary number of arbitrary digits.
Example digit inputs:
static char digits[] = {'a', 'b', 'c', ... 'z', '0', '1', ...}; /* Lots of digits */
static char digits[] = {'s','t','a','c','k','o','v','e','r'}; /* Arbitrary sequence */
It’s quite simple. You want to convert into base digit_count and then instead of converting the digits to numbers, you index into your array.
To convert to an arbitrary base, you need division and remainder.
Here’s a better version than what I used before because it actually creates a buffer (rather than prints it out), drops recursion for iteration and is in C instead of my previous C/Python hodgepodge.
Because it uses a static buffer, the code is not thread safe. Also note that there is no error checking that the code doesn’t underflow the buffer if the number is too large. Finally, it uses a trick of building the string from the end to the front and returning a pointer to the middle of the buffer so it doesn’t have to reverse the digits at the end.