#C14764. Compute Average from Nested Number List
Compute Average from Nested Number List
Compute Average from Nested Number List
You are given a nested list of numbers in JSON format. Your task is to compute the average of all the numbers in the list. The list may be nested arbitrarily. You must use recursion to both validate the list and flatten it.
Every element must be either an integer or a floating-point number. If any element is not a number, your program should output an error message in the following format:
Invalid item X: All elements must be int or float
If the list is empty, the average is defined as 0.
The average is computed by the formula: $$\text{average} = \frac{\sum_{i=1}^{n} a_i}{n}$$ where \(a_i\) represents the valid numbers extracted from the list.
inputFormat
A single line containing a nested list in JSON format. For example: [1, [2, 3], 4]
outputFormat
Output the computed average to stdout. If an invalid element is found, output the error message exactly as specified.## sample
[1, 2, 3]
2.0