#C13591. Calculate Average Age
Calculate Average Age
Calculate Average Age
You are given a JSON string representing an array of user objects. Each object may contain an age field which should be an integer. Your task is to calculate the average age of all users with valid integer ages. Formally, if there are n users with valid ages (a_1, a_2, \dots, a_n), then the average is defined as
[
\text{average} = \frac{\sum_{i=1}^{n} a_i}{n}
]
If no valid age values are found or if the JSON input is malformed, output null
.
inputFormat
Input is provided via standard input (stdin) as a single JSON string. The JSON represents an array of objects. Each object may include an age
field, whose value should be an integer. For example:
[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
If the JSON is malformed or does not represent an array, consider it invalid and output null
.
outputFormat
Output the average age calculated from the valid integer age
fields. If at least one valid age is found, print the average (if it is an integer, print it without a decimal point; otherwise, print the floating-point value). If no valid ages are found or if the input is invalid, print null
.## sample
[{"name":"Alice", "age":30}, {"name":"Bob", "age":25}, {"name":"Charlie", "age":35}]
30