Question

Let T(n) be the running time of function do_something. Find the equation of T(n) and find...

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

Homework Answers

Answer #1

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))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
An algorithm has a running time like the following: T(n) = T(n-1) + an + b;...
An algorithm has a running time like the following: T(n) = T(n-1) + an + b; for n> 0 and a, b: the constant T(0) = 0 Determine the complexity of T(n)
Let z(s,t) z(s,t) be a differentiable function of two variable s and t with continuous first...
Let z(s,t) z(s,t) be a differentiable function of two variable s and t with continuous first partial derivatives. Assume further that s=s(x,y), t=t(x,y) are differentiable functions of variables x and y . (a) find the general chain rule chain rule for (∂z/∂x)y . Your subscripts will be marked. (b) Let equations F(x,y,s,t)=y+x^2+cos(t)−sin(s)+1=0, G(x,y,s,t)=x+y^2−st−2=0, implicitly define s , and t as functions of x and y . Compute ∂s/∂x)y ∂t/∂x)y at the point P(x,y,s,t)=(1,−1,π,0). (c) Let now z(s,t)=s^2+t. By using the...
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4...
Given the recurrence equation below, find running time T(n). T(n) = 8T(n/2) + n3 log n4 T(n) = 2T(n1/2) + log n
What is the time complexity of the following segment of code assuming that the function temp...
What is the time complexity of the following segment of code assuming that the function temp takes time O(n)? ... ... x = n y = n while y > 1:      print(x)      temp(x)      y = y / 2      x = x – 1 temp(x) … …
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 =...
1.) the following code fragments give running time analysis (Big Oh). Explain your answer: sum2 = 0; sum5 = 0; for(i=1; i<=n/2; i++) { sum2 = sum + 2; } for(j=1; j<=n*n; j++) { sum5 = sum + 5; } i think it is O(n^2) since big oh finds the order of worst case which is the the second loop being n^2 complexity. but im not sure. 2.) Give an efficient algorithm along with running time analysis to find the...
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code...
   1)T(n) = 27 T (n/3) + n3 2)Calculate the running time for the following code fragment. Algorithm test int counter, i, j; counter := 0; for (i:= 1; i < n; i*=2) { for (j:= 0; j < i; j++) { counter := counter +1 } } 3)Let f(n) = 2lg 8n + 3 be a function.
Python question: Write a function that takes a number and an integer (X,N) in a time...
Python question: Write a function that takes a number and an integer (X,N) in a time complexity of O(log(N)). for an example: calling power(2, 2) would return 4
Consider the function F(x, y, z) =x2/2− y3/3 + z6/6 − 1. (a) Find the gradient...
Consider the function F(x, y, z) =x2/2− y3/3 + z6/6 − 1. (a) Find the gradient vector ∇F. (b) Find a scalar equation and a vector parametric form for the tangent plane to the surface F(x, y, z) = 0 at the point (1, −1, 1). (c) Let x = s + t, y = st and z = et^2 . Use the multivariable chain rule to find ∂F/∂s . Write your answer in terms of s and t.
Find Big-O Notation 1. def upper_triangle(A, N): # Assume A is a square matrix (stack of...
Find Big-O Notation 1. def upper_triangle(A, N): # Assume A is a square matrix (stack of lists) for i in range(N): A[i][:i] = [0 for _ in range(i)] return A 2. def running_avg(vec, N): sum = 0 avg_list = [] for i in range(N): sum += vec[i] avg = sum / (i + 1) avg_list.append(avg) return avg_list 3. def similarity(vec1, vec2, N): # Assume vec1 and vec2 have length of N s1 = 0 s2 = 0 for i in...