from typing import List
from typing import Callable


def add_to_partition(elem: str, partitions: List[List[str]],
                     is_equiv: Callable[[str, str], bool]):
    """Helper function to add an element to an appropriate equivalence class

    Adds the element to an existing class if one is available or creates a
    new class by adding a partition if necessary.

    Args:
        elem: The element to add
        partitions: The list of equivalence classes to add ``elem`` to
        is_equiv: A function that accepts two elements of lst and returns
            whether those elements should be in the same equivalence class.
            For proper functioning, should implement an equivalence relation.

    Returns: The equivalence classes provided but with ``elem`` added.

    """
    for partition in partitions:
        if is_equiv(elem, partition[0]):
            partition.append(elem)
            return partitions
    partitions.append([elem])
    return partitions