ENGG 1330 Computer Programming I (21-22 Semester 2)
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
ENGG 1330 Computer Programming I
(21-22 Semester 2)
Assignment 1
Introduction
(This picture is referenced from “freepik” with free license for personal and commercial use through the following link
https://www.freepik.com/free-psd/3d-illustration-man-flying-catch-golden-coin-business-graph- rendering_21517039.htm#query=cartoon%20stock&position=2&from_view=keyword)
Peter wants to perform data analytic on the stock market. For each stock, there are several metrics to describe its prices, e.g.
• open: the opening price for a stock when the market opens
• close: the closing price for a stock when the market closes
• high: the highest price of a stock during a trading day
• low: the lowest price of a stock during a trading day.
Here, we use the close price only throughout Assignment I and assume that stocks are bought and sold at the close price. Buying a stock followed by selling the stock is referred to as a transaction.
Level 1 (20%)
When investing a stock, the metric “average return” can be used to measure its profitability. The “average return” is defined as follows:
$%#
= / ,
!&#
where
• n is the number of trading days we are analyzing, and n ³ 2
• ! is the close price of a stock at trading day i
For example, the following are the stock close prices from trading day 1 to day 4.
Trading day |
1 |
2 |
3 |
4 |
Close price |
10 |
20 |
40 |
80 |
The average return for this stock is 100%. The result is calculated through the following:
= 3 + + 8 = 100%
We could get the average return by summing daily return and then dividing the sum by the number of transactions which is 3 in this example.
Knowing that you are excellent in programming, Peter asks you a favor to write a computer program to calculate the average return for a stock.
Input:
• A list of close prices in integer for a stock, separated by a space, terminated by a carriage return (linefeed) character.
Output:
• Average return of the stock, in integral percentage format, terminated by a carriage return (linefeed) character.
Examples:
Case |
Sample input |
Sample output |
1 |
10 20 40 |
100% |
2 |
100 90 80 70 |
-11% |
Level 2 (20%)
Peter wants to analyze the maximum profit through buying and selling stock at most once. Peter turns to your help.
Input:
• A list of close prices in floating point number for a stock, separated by a space, terminated by a carriage return (linefeed) character.
Output:
• The maximum profit, in two decimal places, terminated by a carriage return (linefeed) character.
Examples:
Case |
Sample input |
Sample output |
1 |
1.0 2.0 4.0 6.0 |
Maximum profit: 5.00 |
2 |
10.0 9.0 8.0 7.0 |
Maximum profit: 0.00 |
Explanation:
• Case 1: The maximum profit can be obtained by buying the stock at Day 1 and selling it at Day 4 and so the maximum profit is 6 – 1 = 5.
• Case 2: The maximum profit can be obtained by holding the cash without doing any transaction, then the maximum profit is 0.
Level 3 (30%)
Peter wants to further analyze the maximum profit through buying and selling stock for multiple times and has to comply with the following rules. Peter turns to your help again.
Rules:
Rule 1. A stock cannot be bought and sold on the same day.
Rule 2. A stock must be sold before it can be bought again.
Rule 3. If there is more than one way to do a transaction with the same profit, we choose the
one that allows us to hold the cash for a longer time.
Rule 4. If there is more than one sequence of transactions to obtain the same maximum profit,
we choose the one with the least number of transactions. In real life, every transaction has a cost, and we would like to reduce. In case Rule 3 conflicts with Rule 4, Rule 4 overrides Rule 3.
Input:
• A list of close prices in floating point number for a stock, separated by a space, terminated by a carriage return (linefeed) character.
Output:
• The day and the action in two columns with column width of 10 characters. If no action is needed, output ‘None’ in all the columns.
• The maximum profit, in two decimal places, terminated by a carriage return (linefeed) character.
Examples:
Case |
Sample input |
Sample output |
|
1 |
1.0 2.0 6.0 3.0 7.0 |
Day 1 3 4 5 |
Action Buy Sell Buy Sell |
Maximum profit: 9.00 |
|||
2 |
9.0 8.0 6.0 2.0 3.0 3.0 7.0 |
Day Action 4 Buy 7 Sell Maximum profit: 5.00 |
|
3 |
4.0 3.0 2.0 1.0 |
Day Action None None Maximum profit: 0.00 |
|
4 |
1.0 3.0 3.0 6.0 |
Day Action 1 Buy 4 Sell Maximum profit: 5.00 |
|
5 |
1.0 3.0 3.0 3.0 |
Day Action 1 Buy 2 Sell Maximum profit: 2.00 |
|
6 |
3.0 3.0 3.0 |
Day |
Action |
|
|
None None Maximum profit: |
Explanation:
• Case 1: The maximum profit can be obtained by the following two transactions. Day 1: buy the stock
Day 3: sell the stock
Day 4: buy the stock
Day 5: sell the stock.
Maximum profit = (6- 1) + (7-3) = 9
• Case 2: The maximum profit can be obtained by the following one transaction. Day 4: buy the stock
Day 7: sell the stock
Maximum profit = 7-2 = 5
• Case 3: The maximum profit can be obtained by not doing any transaction.
• Case 4: There are two sequences of transactions that can obtain the same maximum profit : Day 1: buy the stock
Day 4: sell the stock
Or
Day 1: buy the stock
Day 2: sell the stock
Day 3: buy the stock
Day 4: sell the stock.
In this case, we choose the first sequence with fewer number of transactions.
• Case 5: The maximum profit can be obtained by buying at Day 1 and selling at either Day 2,
Day 3, or Day 4.
In this case, we choose the earliest selling date, which is Day 2, allowing us to hold the cash for a longer time. The same also applies to buying. If two buying dates have the same price, we will choose the later one so as to hold cash in our hand for a longer time.
• Case 6: The maximum profit can be obtained by not doing any transaction.
Level 4 (30%)
With your help, Peter gains insight into stock-trading. Instead of trading only one share, Peter wants to know the effect of trading multiple times with multiple shares. He believes your programming could help him again. In addition to Rule 1 to 4 for Level 3, we also need to comply with the following Rule 5.
New rule:
Rule 5. The number of shares can be bought is limited by amount of cash in hand. For example,
if you have cash of 10 and the stock price is 3, you could only buy 3 shares for 9 with 1 left in hand. There is no fraction for a share.
Input :
• A list of close prices in floating point number for a stock, separated by a space, terminated by a carriage return (linefeed) character.
• A floating point number representing the cash in hand at the beginning, terminated by a carriage return (linefeed) character.
Output:
• The day, the action and the number of shares in three columns with column width of 10 characters. If no action is needed, output ‘None’ in all the columns.
• The maximum cash in hand at the end, in two decimal places, terminated by a carriage return (linefeed) character.
Examples:
Case |
Sample input |
Sample output |
||
1 |
1.0 3.0 1.0 8.0 10.0 |
Day 1 2 3 4 |
Action Buy Sell Buy Sell |
Quantity 10 10 30 30 |
Maximum cash in hand: 240.00 |
||||
2 |
9.0 8.0 7.0 6.0 20.0 |
Quantity None Maximum cash in hand: 20.00 |
Explanation:
• Case 1: The maximum cash in hand can be obtained by the following transactions.
Day 1: buy 10 shares of the stock, cash in hand = 10 – 1*10 = 0
Day 2: sell 10 shares of the stock, cash in hand = 3 * 10 = 30
Day 3: buy 30 shares of the stock, cash in hand = 30 – 1 * 30 = 0
Day 4: Sell 30 shares of the stock, cash in hand = 8 * 30 = 240
• Case 2: The maximum cash in hand can be obtained by not doing any transaction.
Bonus
• If you are the first student who can find a sequence of transactions, which is different from the one in the test case but can obtain the same maximum profit in Level 3 or the same maximum cash in hand in Level 4, you can get a bonus of 2 marks for each correct sequence.
• To get the bonus, you need to send your sequence to Mr. Mao Huade by email at ma[email protected] for verification against the rules.
2022-03-22