When a share of common stock of some company is sold, the capital gain (or, sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFO protocol—the shares sold are the ones that have been held the longest (indeed, this is the default method built into several personal finance software packages). For example, suppose we: buy 100 shares at $20 each, buy 20 shares at $24 each, buy 200 shares at $36 each, sell 150 shares at $30 each, in this order. Then applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. The capital gain in this case would therefore be 100 * 10 + 20 * 6 + 30 * (−6), or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at y each” or “sell x share(s) at y each” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol to identify shares. INPUT#1: # defining the input sequence. This can also be taken from user using input() function. transactions = ["buy 100 shares at $20 each", "buy 20 shares at $24 each", "buy 200 shares at $36 each", "sell 150 shares at $30 each"] OUTPUT#1: $940 INPUT#2: transactions = ["buy 10 shares at $20 each", "buy 20 shares at $34 each", "sell 25 shares at $40 each", "buy 100 shares at $50 each", "sell 85 shares at $45 each"] OUTPUT#2: $-55 Upload
use python
When a share of common stock of some company is sold, the capital gain (or, sometimes, loss) is the difference between the share’s selling price and the price originally paid to buy it. This rule is easy to understand for a single share, but if we sell multiple shares of stock bought over a long period of time, then we must identify the shares actually being sold. A standard accounting principle for identifying which shares of a stock were sold in such a case is to use a FIFO protocol—the shares sold are the ones that have been held the longest (indeed, this is the default method built into several personal finance software packages). For example, suppose we:
- buy 100 shares at $20 each,
- buy 20 shares at $24 each,
- buy 200 shares at $36 each,
- sell 150 shares at $30 each, in this order.
Then applying the FIFO protocol means that of the 150 shares sold, 100 were bought on day 1, 20 were bought on day 2, and 30 were bought on day 3. The capital gain in this case would therefore be 100 * 10 + 20 * 6 + 30 * (−6), or $940. Write a program that takes as input a sequence of transactions of the form “buy x share(s) at y each” or “sell x share(s) at y each” assuming that the transactions occur on consecutive days and the values x and y are integers. Given this input sequence, the output should be the total capital gain (or loss) for the entire sequence, using the FIFO protocol to identify shares.
INPUT#1:
# defining the input sequence. This can also be taken from user using input() function.transactions = ["buy 100 shares at $20 each",
"buy 20 shares at $24 each",
"buy 200 shares at $36 each",
"sell 150 shares at $30 each"]
OUTPUT#1:
$940
INPUT#2:
transactions = ["buy 10 shares at $20 each","buy 20 shares at $34 each",
"sell 25 shares at $40 each",
"buy 100 shares at $50 each",
"sell 85 shares at $45 each"]
OUTPUT#2:
$-55
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)