import random


def create_subsets(stimuli, num_stimuli_per_trial, overlap=2):
    """
    Given a list of words, group the words into 8 groups of 6, randomly.
    :param num_stimuli_per_trial: (int) defines the length of the sliding window to use when making groups
    :param overlap (int) defines how many stimuli appear together in two trials
    :param stimuli: (List) Words (stimulus names) of length 36
    :return: subsets: (List of lists) a list of 6 lists of length 6
    """
    num_stimuli = len(stimuli)
    if not (num_stimuli % 6 == 0):
        raise Exception('Number of stimuli needs to be valid: 1 + a multiple of 6 (19, 25, 31, 37, 49)')
    i = 0
    subsets = []
    while i < len(stimuli) - num_stimuli_per_trial:
        subset = stimuli[i:i + num_stimuli_per_trial]
        random.shuffle(subset)  # shuffling so that stimuli are grouped randomly into trials
        subsets.append(tuple(subset))
        i += num_stimuli_per_trial - overlap
    last = stimuli[i:] + stimuli[:overlap]
    random.shuffle(last)  # shuffling so that the last trial contains stimuli in a random order
    subsets.append(tuple(last))
    return subsets