(s,n)
Generate nonempty Lyndon words of length <= n over an s-symbol alphabet.
    The words are generated in lexicographic order, using an algorithm from
    J.-P. Duval, Theor. Comput. Sci. 1988, doi:10.1016/0304-3975(88)90113-2.
    As shown by Berstel and Pocchiola, it takes constant average time
    per generated word.
Generate nonempty Lyndon words of length <= n over an s-symbol alphabet.
    The words are generated in lexicographic order, using an algorithm from
    J.-P. Duval, Theor. Comput. Sci. 1988, doi:10.1016/0304-3975(88)90113-2.
    As shown by Berstel and Pocchiola, it takes constant average time
    per generated word.
Generate nonempty Lyndon words of length < = n over an s - symbol alphabet . The words are generated in lexicographic order using an algorithm from J . - P . Duval Theor . Comput . Sci . 1988 doi : 10 . 1016 / 0304 - 3975 ( 88 ) 90113 - 2 . As shown by Berstel and Pocchiola it takes constant average time per generated word .
def LengthLimitedLyndonWords(s,n):
    """Generate nonempty Lyndon words of length <= n over an s-symbol alphabet.
    The words are generated in lexicographic order, using an algorithm from
    J.-P. Duval, Theor. Comput. Sci. 1988, doi:10.1016/0304-3975(88)90113-2.
    As shown by Berstel and Pocchiola, it takes constant average time
    per generated word."""
    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 len(w) > 0 and w[-1] == s - 1:     # delete trailing z's
            w.pop()
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 len ( w ) > 0 and w [ - 1 ] == s - 1 : # delete trailing z's w . pop ( )