#C14567. Average Salary per Department
Average Salary per Department
Average Salary per Department
You are given a CSV formatted text from standard input (stdin) containing employee records. The first line is a header: ID,Name,Department,Salary,Years of Employment
, and each subsequent line contains one record.
Your task is to compute the average salary for each department. The average for a department is computed by
$$
\text{average} = \frac{\sum_{i=1}^{n} s_i}{n}
$$
where \(s_i\) is a valid salary value for the department. Rows with missing or invalid salary or missing department fields should be ignored. In the case of an empty input (or when no valid records are found), output an empty JSON object ({}
).
inputFormat
The input is provided via standard input (stdin) as CSV content. The first line is the header: "ID,Name,Department,Salary,Years of Employment". Each following line is a record of an employee. Fields are separated by commas. There is no extra prompt or number of lines provided.
outputFormat
Print a JSON object to standard output (stdout) where the keys are department names and the values are the computed average salaries (as floats). For example: {"Engineering": 77500.0, "Marketing": 52500.0, "Sales": 60000.0}## sample
ID,Name,Department,Salary,Years of Employment
1,John Doe,Engineering,75000,5
2,Jane Smith,Marketing,55000,3
3,Bob Johnson,Engineering,80000,2
4,Alice White,Sales,60000,4
5,Chris Green,Marketing,50000,6
{"Engineering": 77500.0, "Marketing": 52500.0, "Sales": 60000.0}