During the hiring process, a company gave me this test:
Q) Consider Share prices for a N number of companies given for each month since year 1990 in a CSV file. Format of the file is as below with first line as header.
Year,Month,Company A, Company B,Company C, .............Company N 1990, Jan, 10, 15, 20, , ..........,50 1990, Feb, 10, 15, 20, , ..........,50 . . . . 2013, Sep, 50, 10, 15............500
a) List for each Company year and month in which the share price was highest.
b) Submit a unit test with sample data to support your solution.
They wanted me to not use any third party libraries so I did this:
import csv
csv_file = open('demo.csv', "rb")
reader = csv.reader(csv_file)
master_dic = {}
final_list= []
rownum = 0
for row in reader:
if rownum == 0:
header = row
else:
for i in range(2,len(header)):
"""
# Here it will create a dictionary which till have a structure like this
{'Company': {'1990': {'Mar': 18.0, 'Feb': 19.0, 'Aug': 19.0},
'1991': {'Mar': 10.0, 'Feb': 21.0, 'Aug': 21.0, 'Sep': 23.0, 'May': 26.0}}}
"""
if header[i] in master_dic:
if row[0] in master_dic[header[i]]:
master_dic[header[i]][row[0]][row[1]] =float(row[i])
else:
master_dic[header[i]][row[0]] ={}
master_dic[header[i]][row[0]][row[1]] =float(row[i])
else:
master_dic[header[i]] = {}
master_dic[header[i]][row[0]] = {}
master_dic[header[i]][row[0]][row[1]] =float(row[i])
rownum += 1
# Here we will Iterate over the master_dic dictionary and find out the highest price
# of the shares in every company in all the months of all the years.
for company,items in master_dic.iteritems():
for year,items_1 in items.iteritems():
maxima = 0
maxima_month = ''
temp_list = []
for months,share in items_1.iteritems():
if share > maxima:
maxima = share
maxima_month = months
temp_list = [company,year,maxima_month,maxima]
continue
if share == maxima: # sometimes company may have same high value for more than one month in a year.
if len(temp_list) >0:
temp_list = [company,year,temp_list[2]+', '+months,maxima]
final_list.append(temp_list) # appending the value of temperory list to the final list that would be displayed at the end of the program.
print final_list
csv_file.close()
demo.csv:
year month Company A Company B Company C Company D 1990 Jan 10 15 20 18 1990 Feb 11 14 21 21 1990 Mar 13 8 23 10 1990 April 12 22 19 9 1990 May 15 12 18 26 1990 June 18 13 13 19 1990 July 12 14 15 20 1990 Aug 12 14 16 21 1990 Sep 13 8 23 23 1990 Oct 12 22 19 19 1990 Nov 15 12 18 14 1990 Dec 18 13 13 16 1991 Jan 15 12 18 26 1991 Feb 18 13 13 19 1991 Mar 12 14 15 18 1991 April 12 14 16 21 1991 May 11 16 13 10 1991 June 14 17 11 9 1991 July 23 13 12 26 1991 Aug 23 21 10 19 1991 Sep 22 22 9 20 1991 Oct 24 20 42 19 1991 Nov 12 14 15 18 1991 Dec 15 12 18 13 1992 Jan 21 14 16 15 1992 Feb 10 13 26 16 1992 Mar 9 11 19 23 1992 April 23 12 18 19 1992 May 12 10 21 18 1992 June 17 9 10 13 1992 July 15 42 9 18 1992 Aug 16 9 26 13 1992 Sep 15 26 19 15 1992 Oct 18 19 20 16 1992 Nov 19 18 21 13 1992 Dec 20 21 23 11 1993 Jan 21 10 19 12 1993 Feb 13 9 14 18 1993 Mar 21 14 16 21 1993 April 10 13 26 10 1993 May 9 11 19 9 1993 June 23 12 18 26 1993 July 12 10 21 19 1993 Aug 17 9 10 20 1993 Sep 15 42 9 21 1993 Oct 16 9 26 23 1993 Nov 15 26 19 19 1993 Dec 18 19 20 14
I got this response back from them:
Wrong output,
No DS implementation
Inline code style - No use of best practices
Could you educate me on what I did wrong? What should I have done, and how to learn best practices? I thought I was right.
I really want to learn best practices, and understand why I was rejected, so I can do better on my next interview.