-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxprice.py
More file actions
54 lines (47 loc) · 1.99 KB
/
maxprice.py
File metadata and controls
54 lines (47 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import sys
import csv
class MaxCast(object):
inputstrings = sys.argv
final_dict = {'company': [], 'month': [], 'year': [], 'price' : []}
def argsparse(self):
if(len(sys.argv)) <= 1:
raise Exception("Argument Error: Too few Arguments")
else:
self.filename = self.inputstrings[1]
try:
self.csvf = open(self.filename, 'rb') # opens the csv file
except IOError:
raise IOError("File not found, please check the filename")
self.reader = csv.reader(self.csvf)
def read_data(self):
year = []
month = []
share_price = []
for ind, item in enumerate(self.reader):
i = 0
if ind == 0:
self.company_name = item[2:]
for i in range(len(self.company_name)):
share_price.append([])
else:
year.append(item[0])
month.append(item[1])
for price in item[2:]:
share_price[i].append(price.strip())
i = i + 1
j = 0
for price in share_price:
if price:
max_val = max(price)
ind = price.index(max_val)
self.final_dict['company'].append(self.company_name[j])
self.final_dict['month'].append(month[ind])
self.final_dict['year'].append(year[ind])
self.final_dict['price'].append(max_val)
j = j + 1
return [(self.final_dict['company'][i], self.final_dict['month'][i], self.final_dict['year'][i], self.final_dict['price'][i]) for i in range(len(self.final_dict['company']))]
if __name__ == "__main__":
max_cast = MaxCast()
max_cast.argsparse()
max_cast.read_data()
print [(max_cast.final_dict['company'][i], max_cast.final_dict['month'][i], max_cast.final_dict['year'][i], max_cast.final_dict['price'][i]) for i in range(len(max_cast.final_dict['company']))]