#C14063. Simulate Stock Market Transactions

    ID: 43671 Type: Default 1000ms 256MiB

Simulate Stock Market Transactions

Simulate Stock Market Transactions

You are given a series of stock market transactions. Each transaction is represented by four values: the action (either buy or sell), the stock name, the stock price, and the quantity.

Your task is to process these transactions sequentially in order to obtain the final portfolio. The following rules apply:

  • Every transaction must have exactly four fields. If any transaction does not, output an error message: Each transaction must be a tuple of the form (action, stock_name, stock_price, quantity).
  • The action must be either buy or sell. If an unknown action is encountered, output an error message: Unknown transaction type: \(action\) where \(action\) is replaced by the invalid action.
  • For a buy transaction, add the quantity to the stock in the portfolio.
  • For a sell transaction, subtract the quantity. If there are not enough shares to sell, output an error message: Not enough shares to sell for stock: \(stock_name\).
  • Stocks that end up with 0 shares should be removed from the portfolio.

The final portfolio should be printed as a JSON dictionary (object) where keys are stock names and values are the number of shares.

Note: Input is read from standard input (stdin) and output must be printed to standard output (stdout). In case of an error, print the error message.

inputFormat

The input consists of multiple lines. The first line contains an integer \(N\) representing the number of transactions. Each of the following \(N\) lines contains a transaction with four tokens separated by spaces:

action stock_name stock_price quantity

For example: buy AAPL 150 10

outputFormat

If all transactions are processed successfully, output the final portfolio as a JSON-style dictionary with no spaces, for example:

{"AAPL":15,"GOOGL":3}

If an error occurs during processing, print the error message exactly as specified.

## sample
3
buy AAPL 150 10
buy AAPL 155 5
buy GOOGL 1200 3
{"AAPL":15,"GOOGL":3}