I thought I might compliment the other answers with an approach that follows the OP intuition closer.
I made it recursive, and factored out the creation of the sequence to a helper function :
def nth_label(n,symbols,accumulator=""):
q = n // len(symbols)
m = n % len(symbols)
if q==0:
return symbols[m]+accumulator
else:
return nth_label(q-1,symbols,symbols[m]+accumulator)
def generate_labels():
i = 0
while True:
yield nth_label(i, "abcdefghijklmnopqrstuvwxyz")
i += 1
Please be aware I just tested the equivalent javascript, not this python version!
Note that though this uses a recursive function, the depth of the recursion is only logarithmic on the number, with the base being the number of symbols (so a small number of recursions in practice). It wouldn't be hard
It's easy to convert it to an iterative function anyway (left as an exercise for, if a little less elegant IMO. It might be easier to see how this is different from itertools.product in the reader ;)explicitly iterative version:
def nth_label(n,symbols):
result = ""
q = n // len(symbols)
m = n % len(symbols)
while q>0:
result = symbols[m]+result
n = q - 1
q = n // len(symbols)
m = n % len(symbols)
return symbols[m]+result
def generate_labels():
i = 0
while True:
yield nth_label(i, "abcdefghijklmnopqrstuvwxyz")
i += 1
It's proportional to log_k of n, where k is the number of symbols, in both space and time.
Sorry for the previous errors, this one is tested ;)