#C304. Yearly Stock Return Analysis

    ID: 46423 Type: Default 1000ms 256MiB

Yearly Stock Return Analysis

Yearly Stock Return Analysis

In this problem, you are given historical stock market data in CSV format. Your task is to compute the yearly return for each specified stock ticker. The yearly return for a given year is defined as [ R = \frac{C_{last} - C_{first}}{C_{first}} \times 100, ] where (C_{first}) and (C_{last}) are the closing prices on the first and last trading days of that year (based on the chronological order of dates). Note that the dates are provided in the format YYYY-MM-DD, and you can assume that lexicographical order of the dates coincides with their chronological order. Rows with invalid numeric data must be skipped. If there is only one valid record in a particular year, the return is 0.00%.

The input is provided via standard input and the output via standard output. Write a program that processes the input as described and prints the computed yearly returns for each ticker.

inputFormat

The input is read from standard input and follows the format below:

  1. The first line contains one or more stock ticker symbols separated by spaces. For example: AAPL GOOGL.
  2. The second line contains an integer M representing the total number of lines of CSV data that follow (including the header line).
  3. The next M lines represent the CSV data. The first of these lines is the header with the columns: date,company_ticker,open,close. Each subsequent line provides a record with:
    • date: in YYYY-MM-DD format
    • company_ticker: the stock ticker symbol
    • open: the opening price
    • close: the closing price

outputFormat

The output should be printed to standard output. For each ticker (in the order they appear in the first line of input), output a block with the following format:

Ticker: {TICKER}
{YEAR1}: {RETURN1}%
{YEAR2}: {RETURN2}%
...

Each ticker block is separated by an empty line. The yearly returns should be computed as described and printed with exactly two digits after the decimal point. The years should be listed in increasing order.

## sample
AAPL
5
date,company_ticker,open,close
2022-01-03,AAPL,175.25,176.22
2022-12-30,AAPL,179.61,182.01
2021-01-04,AAPL,133.52,132.69
2021-12-31,AAPL,177.57,179.42
Ticker: AAPL

2021: 35.19% 2022: 3.29%

</p>