import random


def spliter(data_dict, ratio=[6, 1, 1], shuffle=True):
    """split dict dataset into train, valid and tests set

    Args:
        data_dict (dict): dataset in dict
        ratio (list): list of ratio for train, valid and tests split
        shuffle (bool): shuffle or not
    
    """
    if len(ratio) != 3:
        raise ValueError(f'ratio must include three int numbers')
    train = {'x': list(), 'y': list()}
    valid = {'x': list(), 'y': list()}
    tests = {'x': list(), 'y': list()}
    for _, [samples, labels] in data_dict.items():
        samples_lens = len(samples)
        train_ratio = round(samples_lens * (ratio[0] / sum(ratio)))
        tests_ratio = round(samples_lens * (ratio[2] / sum(ratio)))
        valid_ratio = samples_lens - train_ratio - tests_ratio
        data = list(zip(samples, labels))
        if shuffle:
            random.shuffle(data)
        x, y = zip(*data)
        train['x'].extend(x[:train_ratio])
        train['y'].extend(y[:train_ratio])
        valid['x'].extend(x[train_ratio:train_ratio + valid_ratio])
        valid['y'].extend(y[train_ratio:train_ratio + valid_ratio])
        tests['x'].extend(x[-tests_ratio:])
        tests['y'].extend(y[-tests_ratio:])
    return train, valid, tests