Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MRJob in Python In this assignment, you are going to compute the best three stoc

ID: 3796938 • Letter: M

Question

MRJob in Python

In this assignment, you are going to compute the best three stocks listed in the Dow Jones index based on the first two quarters in 2011. The data look like the following where the columns are quarter, stock_label, date, opening_price, day_high, day_low, closing_price, and traded_volume. The stock performance should be based on the closing price. The percentage of gain is computed as price-at-the-last-date - price-at-the-first-date/price-at-the-first-date * 100 To test the code, name your program Statistics.py and run the following command NestedGreaterGreater python Statistics.py dow_jones_index.csv > output.txt The expected results should look like the following where the columns are stock_label, percentage_of_gain, and average_traded_volume ('IBM', '11.59%', '24739184.44') ('KRFT', '10.93%', '44816171.24') ('MCD', '10.04%', '32151700.28') In this assignment, you may need more than just one mapper and reducer.

Explanation / Answer

import sys

file = sys.argv[1]

stocks = {}
with open(file) as fh:
for line in fh:
stock = line.split(",")
symbol = stock[1]
closing_price = float(stock[-2])
volume = float(stock[-1])
if symbol in stocks:
stocks[symbol]["price"].append(closing_price)
stocks[symbol]["volume"].append(volume)
else:
pv_dict = {}
pv_dict["price"] = [closing_price]
pv_dict["volume"] = [volume]
stocks[symbol] = pv_dict

stock_perf = {}
gain_list = []
for key in stocks:
price_list = stocks[key]["price"]
volume_list = stocks[key]["volume"]
average_volume = reduce(lambda x, y: x + y, volume_list) / len(volume_list)
pv_dict = {}
gain = ((price_list[-1] - price_list[0])/price_list[0])*100
gain_list.append(gain)
pv_dict["gain"] = gain
pv_dict["volume"] = average_volume
  
stock_perf[key] = pv_dict

gain_list.sort()
gain_list.reverse()
gain_list = gain_list[:3]

for g in gain_list:
for key in stock_perf:
if stock_perf[key]["gain"] == g:
print "('" + key + "','" + str(g) + "%','" + str(stock_perf[key]["volume"]) + "')"
break

# pastebin link: http://pastebin.com/gQbi9HGS

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote