This is a python code question. The output is supposed to be the two different l
ID: 669289 • Letter: T
Question
This is a python code question. The output is supposed to be the two different lists (which they are) and the big-O complexity of the code. How would I incorporate that need (the big-O complexity) into the code below to satisfy that objective.
#This function will reverse the list within the parameter
def reverseList(tools):
return tools[::-1]
#This is the original list
tools_list = ['hammer' , 'screwdriver' , 'drill' , 'wrench']
#This will be the reversed list
tools_listrev = reverseList(tools_list)
#We will then print out both lists
print(tools_list)
print(tools_listrev)
Explanation / Answer
#This function will reverse the list within the parameter
def reverseList(tools):
return tools[::-1]
#This is the original list
tools_list = ['hammer' , 'screwdriver' , 'drill' , 'wrench']
#This will be the reversed list
tools_listrev = reverseList(tools_list)
#We will then print out both lists
print(tools_list)
print(tools_listrev)
'''
reversing the list means iterate over the list from the back and append each in new list and return the that list;
This process is O(n) where n is the number of items in the List;
to find this we should write above function in form of loop
'''
def reverseList(tools):
i = 1;
l = []
length = len(tools);
for j in range(length):
l.append(tools[length-(1+j)])
i += 1
print("Length is "+str(length)+" and i is "+str(i))
return l
# i will be equal to Length of L which shows that is O(n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.