Skip to main content

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
deleted 3 characters in body
Source Link
fede s.
  • 111
  • 2

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 ;)

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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

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's easy to convert it to an iterative function, if a little less elegant IMO. It might be easier to see how this is different from itertools.product in the 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 ;)

deleted 3 characters in body
Source Link
fede s.
  • 111
  • 2

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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

added 4 characters in body
Source Link
fede s.
  • 111
  • 2

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 % s.lengthlen(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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

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 % s.length
  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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

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 to convert it to an iterative function anyway (left as an exercise for the reader ;).

added 2 characters in body
Source Link
fede s.
  • 111
  • 2
Loading
Source Link
fede s.
  • 111
  • 2
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.