#C12807. Moving Average Analysis from CSV Data
Moving Average Analysis from CSV Data
Moving Average Analysis from CSV Data
You are given a CSV dataset and are required to perform a moving average analysis on a specific numerical column. The CSV input is provided via standard input (stdin) and has the following format:
- The first line contains the target column name and the window size, separated by a space.
- The second line is the CSV header. It contains one or more comma-separated column names.
- All subsequent lines are CSV rows.
Your task is to:
- Identify the index of the target column using the header.
- For each row, read the value in that column. If the value is missing (an empty field), replace it with the most recent non-missing value from previous rows. It is guaranteed that the first row in the data has a valid (non-missing) value for the target column.
- Calculate the simple moving average for the target column with the specified window size. For any row where a full window of data is not available, output "NA" (without quotes).
- For rows where the moving average can be computed, print the average rounded to exactly two decimal places.
Each output should be printed on a new line to standard output (stdout).
Note: The CSV may contain additional columns that should be ignored. Only process the column specified in the first input line.
inputFormat
The input is read from standard input (stdin) and has the following format:
- Line 1: A string (target column name) and an integer (window size) separated by a space.
- Line 2: The CSV header containing comma-separated column names.
- Line 3 and onwards: CSV data rows corresponding to the header. Each row contains comma-separated values. Missing values in the target column are represented by an empty field.
outputFormat
Output to standard output (stdout) exactly one line per CSV data row. For each row:
- Print "NA" (without quotes) if there are fewer than the required number of entries to compute the moving average.
- Otherwise, print the computed moving average of the target column for the window ending at that row, formatted to exactly two decimal places.
Price 3
Date,Price
2020-01-01,10
2020-01-02,20
2020-01-03,
2020-01-04,40
NA
NA
16.67
26.67
</p>