Question: Using Python and Dynamic Programming algorithm How do I find the Max return on investment using $1,000,000 to invest Skip the first 2 rows which

Using Python and Dynamic Programming algorithm

How do I find the Max return on investment using $1,000,000 to invest

Skip the first 2 rows which is the header row and United States row

The answer should be:

49455.556598018164

['"Nevada"', '"Colorado"', '"Tennessee"', '"Michigan"']

Dataset:

InvestmentName InvestmentCost ReturnOnInvestment
United States 226800 0.03015913
California 547700 0.048065
Texas 196100 0.04092943
New York 300400 0.01323088
Florida 233200 0.04162567
Illinois 181100 0.005052
Pennsylvania 173100 0.0136185
Ohio 139100 0.02190591
Michigan 152000 0.04296877
Georgia 188500 0.02801046
North Carolina 185000 0.02277269
New Jersey 327400 0.00550842
Virginia 258800 0.01400381
Washington 387100 0.03917281
Massachusetts 407900 0.03332137
Indiana 144500 0.02654102
Arizona 253300 0.0406834
Tennessee 167300 0.03421969
Missouri 161100 0.01927588
Maryland 289900 0.00955569
Wisconsin 187800 0.02063872
Minnesota 236700 0.03262538
Colorado 380200 0.05724463
Alabama 131200 0.02117276
South Carolina 167000 0.02233585
Louisiana 145900 0.01485257
Kentucky 145400 0.0247173
Oregon 345800 0.04171159
Oklahoma 124000 0.02603059
Connecticut 243700 -0.0013441
Iowa 143200 0.025706
Mississippi 127600 0.01476834
Arkansas 126600 0.02427704
Kansas 140300 0.01893809
Utah 341600 0.03971773
Nevada 293500 0.05258923
New Mexico 195000 0.00910665
West Virginia 96500 0.01716238
Nebraska 165600 0.0342072
Idaho 266400 0.04333478
Hawaii 614100 0.03404135
Maine 232600 0.03022418
New Hampshire 277200 0.02450347
Rhode Island 281700 0.02070541
Montana 235600 0.02603622
Delaware 236600 0.00675307
South Dakota 190100 0.03011093
Alaska 312800 0.01274184
North Dakota 206700 0.04344248
Vermont 202500 0.01095289
District of Columbia 566900 0.04706701
Wyoming 226300 0.02174417

This is what I got but I got the wrong answer. Please help.

Using Python and Dynamic Programming algorithm How do I find the Max

return on investment using $1,000,000 to invest Skip the first 2 rows

1 2 3 import pandas as pd def loadInvestments(file_name) -> list: data = pd.read_csv(file_name)[1:].reset_index(drop=True) data = pd.DataFrame({ 'InvestmentName':data['RegionName'], 'Investment Cost':data['Zhvi'), 'EstimatedReturnOnInvestment':data['10Year']}) 4 5 6 7 return data.values.tolist() 8 9 10 def optimizeInvestments(A, amount): 11 12 # table to income investment_made = [None for i in range(len(A))) 13 14 15 for i in range(len(A)): investment_made[i] = A[i][1] * A[i][2] 16 17 18 investments = None 19 20 roi = 0 21 22 23 24 25 26 27 28 for i in range(len(A)): invested = amount invested-=A[i][1] investment = 0 iny = [A[i][0]] for j in range(i+1, len(A)): if invested >= 0: invested-=A[j][1] investment+investment_made[j] inv.append(A[j][0]) if investment > roi: roi = investment investments = inv 29 30 31 32 33 34 35 36 return(roi, investments) 37 38 39 40 print(optimizeInvestments(loadInvestments('State_Zhvi_Summary_All Homes.csv'), 1000000)) (41865.455897, ['South Dakota', 'Alaska', 'North Dakota', 'Vermont', 'District of Columbia']) 1 2 3 import pandas as pd def loadInvestments(file_name) -> list: data = pd.read_csv(file_name)[1:].reset_index(drop=True) data = pd.DataFrame({ 'InvestmentName':data['RegionName'], 'Investment Cost':data['Zhvi'), 'EstimatedReturnOnInvestment':data['10Year']}) 4 5 6 7 return data.values.tolist() 8 9 10 def optimizeInvestments(A, amount): 11 12 # table to income investment_made = [None for i in range(len(A))) 13 14 15 for i in range(len(A)): investment_made[i] = A[i][1] * A[i][2] 16 17 18 investments = None 19 20 roi = 0 21 22 23 24 25 26 27 28 for i in range(len(A)): invested = amount invested-=A[i][1] investment = 0 iny = [A[i][0]] for j in range(i+1, len(A)): if invested >= 0: invested-=A[j][1] investment+investment_made[j] inv.append(A[j][0]) if investment > roi: roi = investment investments = inv 29 30 31 32 33 34 35 36 return(roi, investments) 37 38 39 40 print(optimizeInvestments(loadInvestments('State_Zhvi_Summary_All Homes.csv'), 1000000)) (41865.455897, ['South Dakota', 'Alaska', 'North Dakota', 'Vermont', 'District of Columbia'])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!