![]() |
Interview task - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Interview task (/thread-15854.html) |
Interview task - khazi - Feb-03-2019 Hello Folks, Recently i got a python assessment task from a company. Though the code was right but unfortunately i was not selected for the next round because i did not follow the python standards. I will post the code below please tell me how can i improve my code readability. #********************** This is the main program************************************ import sys import classModule as cm classObject = cm.assignmentTask() # object instantiation to the class assignmentTask() fileName = sys.argv[1] # reads file path passed as the arguments right after the find_subsequence.py subSeqLength = int(sys.argv[2]) # reads the maximum subsequence length passed as second argument classMethod = sys.argv[3] # reads the class method to further evaluate (values or differences) f_id = open(fileName,"r") # file object for reading a input file lst1 = f_id.read() # read content of the file till EOL lst1 = [int(val) for val in lst1.split()] # list type casting to 'int' type if(classMethod == "values"): # compare command line argument to invoke a class method classObject.values(lst1,subSeqLength) # method invoked for computing highest sum of absolute values for given subsequence elif(classMethod == "differences"): classObject.differences(lst1,subSeqLength) else: raise Exception("Argument passing error: Improper classMethod") # raise exception if a unknown method is passed #********************** This is the module program************************************ class assignmentTask(): def values(self, l1, n): self.l1 = l1 self.n = n d = dict() tmp_sum = [] for i in range(4,n+1): for j in range(len(l1)-i+1): l2 = l1[j:j+i] tmp_sum.append(sum(l2)) d[i] = max(tmp_sum) # maps maximum value for various subSequence(n) combinations tmp_sum = [] print("{}".format(max(d.values()))) def differences(self, l1, n): self.l1 = l1 self.n = n tmp_l2 = [] d = dict() for i in range(len(l1)-(n-1)): l2 = l1[i:i+n] for j in range(len(l2)-1): tmp_l2.append(abs(l2[j] - l2[j+1])) d[i] = sum(tmp_l2) # maps the sum of absolute difference for various subSequence(n) combinations del tmp_l2[:] print("{}".format(max(d.values()))) RE: Interview task - stullis - Feb-03-2019 Sorry about the interview. You'll do better next time. Supposing that they're referring to PEP 8 standards, here are the issues I see:
On a side note, I'm not understanding the purpose of assigning attributes in assignmentTask. Neither attribute gets used because all invocations are of the arguments passed in instead. Also, more descriptive names than "lst1" and "l1" may have benefited you. RE: Interview task - buran - Feb-03-2019 In addition to stullis's comments/recommendations:
This is non-exhaustive list, just some random thoughts while looking at your code. Please, don't take offense. |