|  | 
|  | 1 | +# Problem 1393: Capital Gain/Loss | 
|  | 2 | +# Difficulty: Medium | 
|  | 3 | + | 
|  | 4 | +# Table: Stocks | 
|  | 5 | +# +---------------+---------+ | 
|  | 6 | +# | Column Name   | Type    | | 
|  | 7 | +# +---------------+---------+ | 
|  | 8 | +# | stock_name    | varchar | | 
|  | 9 | +# | operation     | enum    | | 
|  | 10 | +# | operation_day | int     | | 
|  | 11 | +# | price         | int     | | 
|  | 12 | +# +---------------+---------+ | 
|  | 13 | +# (stock_name, operation_day) is the primary key (combination of columns with unique values) for this table. | 
|  | 14 | +# The operation column is an ENUM (category) of type ('Sell', 'Buy'). | 
|  | 15 | +# Each row of this table indicates that the stock which has stock_name had an operation on the day operation_day with the price. | 
|  | 16 | +# It is guaranteed that each 'Sell' operation for a stock has a corresponding 'Buy' operation in a previous day. | 
|  | 17 | +# It is also guaranteed that each 'Buy' operation for a stock has a corresponding 'Sell' operation in an upcoming day. | 
|  | 18 | + | 
|  | 19 | +# Problem Statement: | 
|  | 20 | +# Write a function to report the Capital gain/loss for each stock. | 
|  | 21 | +# The Capital gain/loss of a stock is the total gain or loss after buying and selling the stock one or many times. | 
|  | 22 | +# Return the result table in any order. | 
|  | 23 | + | 
|  | 24 | +# Solution 1 | 
|  | 25 | + | 
|  | 26 | +import pandas as pd | 
|  | 27 | +import numpy as np | 
|  | 28 | + | 
|  | 29 | +def capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame: | 
|  | 30 | +    # I create a new column 'signed_price' where Buy prices are made negative and Sell prices remain positive | 
|  | 31 | +    stocks['signed_price'] = np.where(stocks['operation'] == 'Buy', -stocks['price'], stocks['price']) | 
|  | 32 | +     | 
|  | 33 | +    # I group by 'stock_name' and sum the signed prices to compute total capital gain or loss | 
|  | 34 | +    result_df = stocks.groupby('stock_name', as_index=False).agg(capital_gain_loss=('signed_price', 'sum')) | 
|  | 35 | +     | 
|  | 36 | +    # I return the final result | 
|  | 37 | +    return result_df | 
|  | 38 | + | 
|  | 39 | +# Intuition: | 
|  | 40 | +# I need to compute the net profit or loss by treating Buy prices as negative and Sell prices as positive. | 
|  | 41 | +# Summing these adjusted prices for each stock will give the total capital gain or loss. | 
|  | 42 | + | 
|  | 43 | +# Explanation: | 
|  | 44 | +# I use `np.where` to create a 'signed_price' column where 'Buy' prices are multiplied by -1 and 'Sell' prices remain as is. | 
|  | 45 | +# Then, I group the DataFrame by 'stock_name' and sum the 'signed_price' values. | 
|  | 46 | +# This sum represents the net capital gain or loss for each stock. | 
|  | 47 | +# I finally return this summarized DataFrame. | 
|  | 48 | + | 
|  | 49 | +# Solution 2 | 
|  | 50 | + | 
|  | 51 | +import pandas as pd | 
|  | 52 | + | 
|  | 53 | +def capital_gainloss(stocks: pd.DataFrame) -> pd.DataFrame: | 
|  | 54 | +    # I create a new column 'signed_price' using apply to assign negative price for 'Buy' and positive for 'Sell' | 
|  | 55 | +    stocks['signed_price'] = stocks.apply(lambda row: -row['price'] if row['operation'] == 'Buy' else row['price'], axis=1) | 
|  | 56 | +     | 
|  | 57 | +    # I group by 'stock_name' and sum the signed prices to compute total capital gain or loss | 
|  | 58 | +    result_df = stocks.groupby('stock_name', as_index=False).agg(capital_gain_loss=('signed_price', 'sum')) | 
|  | 59 | +     | 
|  | 60 | +    # I return the final result | 
|  | 61 | +    return result_df | 
|  | 62 | + | 
|  | 63 | +# Intuition: | 
|  | 64 | +# Similar to Solution 1 — I need to assign a sign to each price based on the operation type. | 
|  | 65 | +# Summing these signed prices per stock will yield the total capital gain or loss. | 
|  | 66 | + | 
|  | 67 | +# Explanation: | 
|  | 68 | +# I use `apply` with `axis=1` to iterate row-wise and assign a negative price for 'Buy' operations and a positive price for 'Sell'. | 
|  | 69 | +# After creating the 'signed_price' column, I group the DataFrame by 'stock_name' and sum the 'signed_price' values. | 
|  | 70 | +# This sum represents the net capital gain or loss for each stock. | 
|  | 71 | +# The result is returned as a summarized DataFrame. | 
0 commit comments