Portfolio Valuation, Part 1
Problem statement
An investment account starts with initialBalance in cash and no shares. transactions lists its trades in the order they happened. Each entry is "symbol,type,price,quantity,day":
symbolis the stock, andtypeisbuyorsell.priceis the price per share for this trade, andquantityis the number of shares.dayis the trading day. The list is already in day order, so apply the trades one by one in list order.
A buy pays price * quantity out of cash and adds quantity shares of the stock. A sell removes quantity shares and adds price * quantity to cash.
currentPrices gives today's price of some stocks, each as "symbol,price". After all trades, find:
- the cash balance;
- the value of each stock still held, which is the number of shares held times its current price;
- the total account value, which is the cash plus the values of all stocks held.
Return a list of strings. The first is "cash:<cash>" and the second is "total:<total>". After them comes one "<symbol>:<value>" for each stock with more than zero shares, in increasing string order of the symbol. A stock whose shares were all sold is not listed. All amounts are whole numbers.
Function
valuePortfolio(initialBalance: int, transactions: String[], currentPrices: String[]) → String[]Examples
Example 1
initialBalance = 200000transactions = ["AAPL,buy,120,20,1","AAPL,sell,130,10,2"]currentPrices = ["AAPL,200","GOOG,150"]return = ["cash:198900","total:200900","AAPL:2000"]- Buying 20 shares at 120 costs
2400, so cash goes from200000to197600. - Selling 10 shares at 130 brings in
1300, so cash ends at198900and 10 AAPL shares are left. - At the current price 200 they are worth
2000. GOOG is not held, so its price is not used. - The total is
198900 + 2000 = 200900.
Example 2
initialBalance = 10000transactions = ["MSFT,buy,50,40,3","GOOG,buy,100,30,3","MSFT,sell,60,40,4","AMZN,buy,20,100,5","GOOG,sell,90,10,6"]currentPrices = ["AMZN,25","GOOG,110","MSFT,70"]return = ["cash:6300","total:11000","AMZN:2500","GOOG:2200"]- Cash:
10000 - 2000 - 3000 + 2400 - 2000 + 900 = 6300. - All 40 MSFT shares were sold, so MSFT is not listed.
- AMZN: 100 shares at 25 are worth
2500. GOOG: 20 shares at 110 are worth2200. - The total is
6300 + 2500 + 2200 = 11000, and the holdings are listed in symbol order: AMZN, then GOOG.
Example 3
initialBalance = 500transactions = []currentPrices = ["AAPL,10"]return = ["cash:500","total:500"]There are no trades, so the cash stays 500, nothing is held and the total is 500.
Constraints
0 <= initialBalance <= 10^90 <= transactions.length <= 10001 <= currentPrices.length <= 100- Each
transactions[i]is"symbol,type,price,quantity,day", wheresymbolhas 1 to 5 uppercase English letters,typeis"buy"or"sell",1 <= price <= 10^4,1 <= quantity <= 10^4and1 <= day <= 365, with numbers written in plain decimal. - The days in
transactionsnever decrease. - A buy never costs more than the cash held at that moment, and a sell never sells more shares of a symbol than are held at that moment.
- Each
currentPrices[j]is"symbol,price"with the same symbol format and1 <= price <= 10^4. The symbols incurrentPricesare distinct, and every symbol still held after all trades appears there.