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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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

Upload 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Guessing the output
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY