Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Interview task
#1
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())))
Reply


Messages In This Thread
Interview task - by khazi - Feb-03-2019, 04:02 PM
RE: Interview task - by stullis - Feb-03-2019, 06:11 PM
RE: Interview task - by buran - Feb-03-2019, 07:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  count certain task in task manager[solved] kucingkembar 2 1,140 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  Schedule a task and render/ use the result of the task in any given time klllmmm 2 2,113 May-04-2021, 10:17 AM
Last Post: klllmmm
  How to create a task/import a task(task scheduler) using python Tyrel 7 3,764 Feb-11-2021, 11:45 AM
Last Post: Tyrel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020