import itertools


def _find_cols(cat_vars_ohe, length, categorical_columns):
    """
    Convert the input one-hot encoding dictionary to a
    dictionary that can be used for ordinal to one-hot conversion.
    In addition returns a list of categorical columns including ones
    defined in input and

    Args:
        cat_vars_ohe (dict): Keys are the first column index for each one-hot encoded
            categorical variable and values are the number of categories per categorical
            variable.
        length (int): Total number of columns
        categorical_columns (list of int): list of the index of the categorical (non-one
            hot encoded) columns.
    Returns:
        tuple:
            dict:
                Keys are the new column indexes and values are the values of the
                non-one hot encoded categorical variables (if a list) or the old column
                index.
            list:
                list of bool values showing if the column is categorical
    """
    if categorical_columns is None:
        categorical_columns = []

    list2d = [list(range(k, k + v)) for k, v in cat_vars_ohe.items()]
    merged = list(itertools.chain(*list2d))

    i, j, k = 0, 0, 0
    col_dict = {}
    categorical = []

    while i < length:
        if i not in merged:
            col_dict[j] = i
            if i in categorical_columns:
                categorical.append(True)
            else:
                categorical.append(False)
            i += 1
            j += 1
        else:
            col_dict[j] = list2d[k]

            i += len(list2d[k])
            j += 1
            k += 1
            categorical.append(True)
    return col_dict, categorical