def LengthLimitedLyndonWords(s,n):
    w = [-1]                            # set up for first increment
    while w:
        w[-1] += 1                      # increment the last non-z symbol
        yield w
        m = len(w)
        while len(w) < n:               # repeat word to fill exactly n syms
            w.append(w[-m])
        while w and w[-1] == s - 1:     # delete trailing z's
            w.pop()

for x in LengthLimitedLyndonWords(2, 3):
    print(x)
def LyndonWordsWithLength(s,n):
    if n == 0:
        yield []    # the empty word is a special case not handled by main alg
    for w in LengthLimitedLyndonWords(s,n):
        if len(w) == n:
            yield w

def LyndonWords(s):
    n = 0
    while True:
        for w in LyndonWordsWithLength(s,n):
            yield w
        n += 1

def DeBruijnSequence(s,n):   
    output = []
    for w in LengthLimitedLyndonWords(s,n):
        if n % len(w) == 0:
            output += w
    output.extend([0] * (s**n + n - 1 - len(output)))
    return output
print(DeBruijnSequence(2, 3))
      
    

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: