Data Aggregation with Python
Aggregation is a fundamental operation in data analysis, involving combining multiple data points to derive a single, representative value. This challenge asks you to implement a flexible aggregation function in Python that can perform various calculations (sum, average, min, max, custom functions) on a list of numerical data. Successfully completing this challenge will demonstrate your understanding of functional programming concepts and data manipulation in Python.
Problem Description
You are tasked with creating a function called aggregate_data that takes a list of numbers and an aggregation function as input. The function should apply the provided aggregation function to the list and return the result. The aggregation function can be one of the built-in functions (sum, min, max, average) or a custom function provided by the user.
Key Requirements:
- The
aggregate_datafunction must accept a list of numbers (data) and an aggregation function (func) as arguments. - The
funcargument can be one of the following strings: "sum", "min", "max", "average", or a callable (e.g., a lambda function or a regular function). - If
funcis "sum", the function should return the sum of all numbers in thedatalist. - If
funcis "min", the function should return the minimum value in thedatalist. - If
funcis "max", the function should return the maximum value in thedatalist. - If
funcis "average", the function should return the average (mean) of the numbers in thedatalist. - If
funcis a callable, the function should apply this callable to thedatalist. - The function should handle the case where the input list is empty. If the list is empty, it should return 0 for "sum", "min", and "max", and
Nonefor "average" and custom functions. - The function should raise a
TypeErrorif the inputdatais not a list or if the elements of the list are not numbers. - The function should raise a
ValueErrorif the aggregation functionfuncis not one of the supported options ("sum", "min", "max", "average") and is not a callable.
Expected Behavior:
The function should correctly calculate the aggregation based on the provided function and input data. It should also handle edge cases and invalid inputs gracefully, raising appropriate exceptions.
Examples
Example 1:
Input: data = [1, 2, 3, 4, 5], func = "sum"
Output: 15
Explanation: The sum of the numbers in the list is 1 + 2 + 3 + 4 + 5 = 15.
Example 2:
Input: data = [5, 2, 8, 1, 9], func = "min"
Output: 1
Explanation: The minimum value in the list is 1.
Example 3:
Input: data = [10, 20, 30], func = lambda x: sum(x) / len(x)
Output: 20.0
Explanation: The lambda function calculates the average of the list, which is (10 + 20 + 30) / 3 = 20.0.
Example 4:
Input: data = [], func = "average"
Output: None
Explanation: The list is empty, so the average is undefined and returns None.
Example 5:
Input: data = [1, 2, "a"], func = "sum"
Output:
TypeError: Input list must contain only numbers.
Explanation: The list contains a non-numeric element, raising a TypeError.
Constraints
- The input list
datawill contain only numbers (integers or floats) or be empty. - The length of the input list
datawill be between 0 and 1000. - The aggregation function
funcwill be either a string ("sum", "min", "max", "average") or a callable. - Performance: The function should complete within a reasonable time (e.g., less than 1 second) for the given constraints.
Notes
Consider using a dictionary to map the string representations of aggregation functions to their corresponding built-in functions. This can make the code more readable and maintainable. Think about how to handle potential TypeError and ValueError exceptions effectively. Remember to handle the empty list case appropriately for each aggregation function.