import random


def nbackseq(n, length, words):
    """Generate n-back balanced sequences

    Parameters
    ----------
    n : int
        How many characters (including the current one) to look
        back to assure no duplicates
    length : int
        The total length of the sequence to produce
    words : list
        A list of words to be used to generate sequences

    Returns
    -------
    list
        A list of solutions, where each solution is a list of words
        of length `length`

    """
    solutions = []

    solution = random.sample(words, length)
    # validate that the sequence is correct n-back sequence
    good = True
    for index in range(len(solution)):
        subseq = solution[index-(n-1): index]
        if len(set(subseq)) != n:
            good = False
            break
    if good:
        solutions.append(solution)

    return solutions