Question
Hello, I am struggling with recursion. Python Here is the assignment: This is what I have and what I am getting: I need to find
Hello,
I am struggling with recursion.
Python
Here is the assignment:
This is what I have and what I am getting:
I need to find out when was the best time to buy and the best time to sell was and for what stock. I only need 3 columns which are date, symbol, and close.
I store the best date to buy in bestStock[0], the best date to sell in bestStock[1], the company that holds this in bestStock[3], and the maxProfit in bestStock[4].
I run loops through data and compare if the company is the same, then subtract the two closing costs to get the profit. If the profit is greater than bestStock[4], then I
update the whole bestStock list with the 2 dates, the company, and the maxProfit.
I am sure this will work without recursion but it will take forever. Please help me solve this using Divide and Conquer in an efficient way.
Thank you
Copyable code:
import pandas as pd
import math
def openFile(file):
return pd.read_csv(file)
def cleanData(df):
del df['open']
del df['low']
del df['high']
del df['volume']
return df
df = cleanData(openFile('prices-split-adjusted.csv'))
def DAC(data):
if len(data) > 1:
mid = len(data)//2
left = data[:mid]
right = data[mid:]
DAC(left)
DAC(right)
bestStock = [None, None, 'Company', -math.inf]
for i in range(len(data)):
for j in range(i+1,len(data)):
if data.iloc[i,1] == data.iloc[j,1]:
maxProfit = data.iloc[j,2] - data.iloc[i,2]
if maxProfit > bestStock[3]:
bestStock[0] = left.iloc[i,0]
bestStock[1] = left.iloc[j,0]
bestStock[2] = left.iloc[i,1]
bestStock[3] = maxProfit
if len(data)
return bestStock
In this assignment, you will be implementing a version of the Max Stock Profit problem discussed in the videos/PowerPoint slides. You will be given actual NYSE data from Find the best stock that you could have purchased during the time frame covered by the data about six years). You will also determine the specific buy and sell dates for this stock that maximizes your profit. Start with the data itself. You will use two CSV files. The first is prices-split-adjusted.csv. It contains data for every stock on every day. Each line has the following information: date, symbol, open, close, low, high, volume For example, a sample line from Apple follows: 2010-01-05, AAPL, 30.6571,30.6257,30.4642,30.7985,1504.0 The closing price for the date 2010-01-05 was 30.6257. This is the information we will need for the Max Stock Profit problem. You should write functions to read in this file and find all lines with a particular ticker symbol (i.e., AAPL). Note that the DAC implementation of the Max Stock Profit problem requires changes in price, not actual price, so you will have to manipulate the data so it matches what the implementation requires. Also note that the file contains a header line you will want to read past once you open the file. The other file we will use is securities.csv. It contains, among other things, all the ticker symbols and names of NYSE companies. For example "AAPL", "Apple Inc.","reports", "Information Technology", "Computer Hardware", "Cupertino, California", "1982-11-30","0000320193" I would suggest you get the Max Stock Profit code working (below) before reading this file. You can simply hard-code a symbol like AAPL until you are ready for this step. You will use this file to read in every company and its associated ticker symbol. You then run the Max Stock Profit algorithm on each symbol and keep track of the one with the best profit, printing this information out at the end of the run. Note that this might take a few minutes to compute. Also note that there may be a few symbols that have no price data, in which case you can simply skip those. You should display the company name rather than the ticker symbol. For example Best stock to buy: "Priceline.com Inc" on 2010-06-09 and sell on 2016-11-07 with profit of 1402.9399999999996 To actually figure out the max stock profit, you should implement the DAC version of the algorithm. There is some sample code in the video/PowerPoint slides to guide you. However, you will need to add to it to figure out the best buy and sell date for the optimal profit it calculates. I would start with hard-coding a ticker symbol, such as AAPL. If everything goes well you should get results similar to the ones that follow: AAPL Profit: 105.5710 buy on day: 22 (2010-02-04) and sell on day: 1291 (2015-02-20) 1 import pandas as pd import math 2 3 4 def openFile(file): return pd.read_csv(file) 5 6 7 8 9 def cleanData(df): del df['open'] del df['low'] del df['high'] del df['volume'] return df 10 11 12 13 14 df = cleanData(openFile('prices-split-adjusted.csv')) 15 16 17 18 def DAC(data): if len(data) > 1: mid = len(data)//2 left = data[:mid] right = data[mid:] print(len(data)) 19 20 21 22 23 DAC(left) DAC(right) 24 25 26 bestStock = [None, None, 'Company', -math.inf] 27 28 29 30 31 32 for i in range(len(data)): for j in range(i+1, len(data)): if data.iloc[i,1] == data.iloc[j,1]: maxProfit = data.iloc[i,2] - data.iloc[j,2] if maxProfit > bestStock[3]: bestStock[0] = left.iloc[i,0] bestStock[1] = left.iloc[j,o] bestStock[2] = left.iloc[i,1] bestStock[3] = maxProfit 33 34 35 36 37 38 if len(data) 1: mid = len(data)//2 left = data[:mid] right = data[mid:] print(len(data)) 19 20 21 22 23 DAC(left) DAC(right) 24 25 26 bestStock = [None, None, 'Company', -math.inf] 27 28 29 30 31 32 for i in range(len(data)): for j in range(i+1, len(data)): if data.iloc[i,1] == data.iloc[j,1]: maxProfit = data.iloc[i,2] - data.iloc[j,2] if maxProfit > bestStock[3]: bestStock[0] = left.iloc[i,0] bestStock[1] = left.iloc[j,o] bestStock[2] = left.iloc[i,1] bestStock[3] = maxProfit 33 34 35 36 37 38 if len(data)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started