0 out of 68 challenges solved

Group elements by applying a function to each element

Question:
Write a function called group_by that takes a function and a sequence as input. The function should group the elements of the sequence based on the result of applying the function to each element. It should return a dictionary where the keys are the unique results of the function and the values are lists of elements that produced the corresponding result.

Example:
Input: group_by(lambda x: x % 2, [1, 2, 3, 4, 5, 6])
Output: {1: [1, 3, 5], 0: [2, 4, 6]}

def group_by(func, seq):
    """
    Groups the elements of the sequence based on the result of applying the function to each element.

    Args:
    func: The function to apply.
    seq: The input sequence.

    Returns:
    dict: The dictionary with grouped elements.
    """
    # TODO: Implement the group_by function
    pass