Feb-03-2019, 04:02 PM
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.
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())))