IT206: Design and Analysis of Algorithms
Running Head: ALGEBRA 2
Algebra 2
Student 1
INITIAL VALUES:
current_position = 0 low_price = 0 high_price = 0
STEP 1:
Set the low_price to the stock price at the current_position.
Set the high_price to the stock price at the current_position.
STEP 2:
Set the current_position to the subsequent position.
STEP 3:
Compare the low_price with the stock price at the current_position.
STEP 4:
If the low_price is less than the stock at the current_position then do the following.
Compare the high_price with the stock at the current_position.
If the high_price is less than the stock at the current_position,
set the high_price to the stock at the current_position.
Go to STEP 2.
If the low_price is greater than the stock at the current_position then do the following.
Set the low_price as the stock at the current_position.
Set the high_price to the stock at the current_position. [since the high_price must come after the low_price, this will “reset” the high_price by making it equal to the new low_price.]
Go to STEP 2.
EXAMPLE:
Here is an example of the values that are set through an iteration of the algorithm.
input = [143, 122, 121, 119, 147, 170, 172, 180, 170, 161]
current_position = 0 low_price = 143 high_price = 143
current_position = 1 low_price = 122 high_price = 122
current_position = 2 low_price = 121 high_price = 121
current_position = 3 low_price = 119 high_price = 119
current_position = 4 low_price = 119 high_price = 147
current_position = 5 low_price = 119 high_price = 170
current_position = 6 low_price = 119 high_price = 172
current_position = 7 low_price = 119 high_price = 180
current_position = 8 low_price = 119 high_price = 180
current_position = 9 low_price = 119 high_price = 180
Student 2
Solution:
The last 10 days stock market position as given below: - Day 1- $143, Day 2- $122, Day 3- $121, Day 4- $119, Day 5- $147, Day 6- $170, Day 7- $172, Day 8- $180, Day 9- $170, Day 10- $161
Given above Current stock value for last 10 days that is 1 to 10, let’s take the initial current values for the stocks as mentioned below by assuming zero to initial:
Current Stock Market Position = 0 High Stock Market Price = $0 Low Stock Market Price = $0
Algorithm
Input dollar values as per numbers of days {$143, $122, $121, $119, $147, $170, $172, $180, $170, and $161} 1. Let compare the High and Low Stock market price to the Current market position price as per each day.
Example: Current Stock Market Position = 0 High Stock Market Price = $0 Low Stock Market Price = $0
2. Let compare the Current Stock market position price to per day stock price.
Example: Current Stock Market Position = 1 High Stock Market Price = $122 Low Stock Market Price = $122
3. Let compare the High and Low Stock market price to per day stock position price.
Example: Current Stock Market Position = 2 High Stock Market Price = $121 Low Stock Market Price = $121
4. Let check the current stock market position to find the Low Stock market Price and High Stock market price to buy and sell with the following steps. a. If the current Stock market stock position is Low, then compare it with High Stock market stock price. b. If the current Stock market stock position is High, then compare it with Low Stock market stock price. c. Repeat the above the above two to per current stock market stock position to set the high and low market stock price.
Example: Current Stock Market Position = 3 High Stock Market Price = $119 Low Stock Market Price = $119
Current Stock Market Position = 4 High Stock Market Price = $147 Low Stock Market Price = $119
Current Stock Market Position = 5 High Stock Market Price = $170 Low Stock Market Price = $119
5. Hence the stock prices for the per day current market position keep on undoing the stock prices on a per day basis to find the actual buying stock and date in Low price before the selling stock date in High price depend on the current market stock position as shown in above step 2, 3 and 4 with examples.
Example: Current Stock Market Position = 6 High Stock Market Price = $172 Low Stock Market Price = $119
Current Stock Market Position = 7 High Stock Market Price = $180 Low Stock Market Price = $119
Current Stock Market Position = 8 High Stock Market Price = $180 Low Stock Market Price = $119
Current Stock Market Position = 9 High Stock Market Price = $180 Low Stock Market Price = $119
6. Therefore, the buying date lowest price is $119 and the selling Highest stock price after buying date is $180 as shown in the examples of per day Current Stock Market Position from 1 to 9.