Let T(n) be the running time of function do_something. Find the equation of T(n) and find the complexity of T(n) using big-O notation.
def do_something(L):
s = 0
for x in L:
s = s + x
print(s)
for y in L:
s = s + y
print(s,x,y)
for z in L:
s = s + z
s = s * 5
return s
def do_something(L):
s = 0
for x in L:
s = s + x
print(s)
x loop runs O(Length(L))
for y in L:
s = s + y
print(s,x,y)
y Loop runs(O(Length(L))
for z in L:
s = s + z
s = s * 5
z loop runs O(Length(L))
return s
here L may be a list or tuple or array or etc...
every time "for loop" runs Length(L) times....here x,y, z are independent loop.so time complexity is T(n)=O(Length(L))+O(Length(L))+O(Length(L))+c
c for addition,multiplication,assignment ,ect
T(n)=O(Length(L))
Get Answers For Free
Most questions answered within 1 hours.