Question

In: Computer Science

Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...

Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well.

Here is the code

#Q2
# to denote ayymptotic time complexity use the following notation
# O(l) O(m) O(a) O(b)
# e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)
#1
def merge():
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = [15, 16, 17, 18]
lm = []
for l_i in l:
lm.append(l_i)
for m_i in m:
lm.append(m_i)
print("lm:{}".format(lm))
#2
def merge_a_lot():
l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = [15, 16, 17, 18]
lm = []
for l_i in l:
lm.append(l_i)
for m_i in m:
lm.append(m_i)
print("lm:{}".format(lm))
#3
def go_through():
l = 10
lm = []
while l>0:
lm.append(l)
l=int(l/2)
print("lm:{}".format(lm))
#4
def go_through_two():
l = 10
m = 5
lm = []
while l > 0:
lm.append(l)
l = int(l/2)
m1=1
while m1*m1 <= m:
print("in")
lm.append(m1)
m1 +=1
print("lm:{}".format(lm))
#5
def go_through_cross_two():
l = 10
m = 5
lm = []
while l*l > 1:
lm.append(l)
l -= 1
m1 = 1
while m1*m1 <= m:
lm.append(m1)
m1 += 1
print("lm:{}".format(lm))
#6
def times():
a=100
b=5
sum=b
count=0
while sum<=a:
sum+=b
count+=1
print("count:{}".format(count))

Solutions

Expert Solution

#1 The very first loop runs for the number of times elements in l and then similarly for m. When two loops runs one after another, their time complexity is added So,

Time complexity = O(l+m)

#2 In this part, the second loop is inside the first loop, So, the second loop will run m times in every iteration of l. When two loops runs one inside other, their time complexity is multiplied. So,

Time complexity = O(l*m)

#3 In this part, the value of l is halved each time in the loop. So, it will take log2l iterations to stop. So,

Time Complexity = O(log2l)

#4 In this part, there are 2 different loops, the first loop runs for log2l times while 2nd loop will run for sqrt(m) times. So,\

Time complexity = O(log2l + sqrt(m))

#5 In this part, the second loop is inside the first loop so, time will be multiplied. So,

Time complexity = (log2l * sqrt(m)

#6 This loop will run until the multiple of b becomes equal to a. So,

Time Complexity = O(a/b)


Related Solutions

Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to...
Find the Asymptotic time complexity for each of the functions in the following: #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l) #1 def merge(): l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] m = [15, 16, 17, 18] lm = [] for l_i in l: lm.append(l_i) for m_i in m:...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it....
Hi! Can someone summarize what the reaction article is saying? I am having trouble understanding it. The purpose of this experiment was to investigate the catalytic performance of solid acid catalyst developed from modification of coal combustion fly ash to synthesize methyl 4-aminobenzoate (MAB) by Fischer esterification between 4-aminobenzoic acid and methanol. Upon acid modification of fly ash, surface acidity of solid acid catalyst (SAC) is greatly enhanced as a result of increased surface area, augmented silica content, and fabricated...
Hi! I am having a difficult time thinking of a topic for my research paper if...
Hi! I am having a difficult time thinking of a topic for my research paper if anyone is able to help out that would be appreciated! One challenge is defining a specific topic. For example, the topic "Federal Reserve" is far too broad. Here are considerations and suggestions for you in defining your research topic: 1. Start by review the major topics of the course. See how the textbook is organized, and flip through the various chapters to identify potential...
I am having trouble understanding what value to use for each function. The question is: "Tom...
I am having trouble understanding what value to use for each function. The question is: "Tom plans to save $88 a month, starting today, for 22 years. Dean plans to save $88 a month for 22 years, starting one month from today. Both Tom and Dean expect to earn an average return o 5.27 percent APR on thier savings and both will make the same number of deposits. At the end of the 22 years, how much more (in $)...
I am working through this solution in rstudio and am having trouble fitting this table into...
I am working through this solution in rstudio and am having trouble fitting this table into a linear regression analysis. an answer with corrosponding r code used would be greatly appreciated A study was conducted to determine whether the final grade of a student in an introductory psychology course is linearly related to his or her performance on the verbal ability test administered before college entrance. The verbal scores and final grades for all 1010 students in the class are...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
I am having the most trouble with 1d: 1. a. Prove that if f : A...
I am having the most trouble with 1d: 1. a. Prove that if f : A → B, g : B → C, and g ◦f : A → C is a 1-to-1 surjection, then f is 1-to-1 and g is a surjection. Proof. b. Prove that if f : A → B, g : B → C, g ◦ f : A → C is a 1-to-1 surjection, and g is 1-to-1, then f is a surjection. Proof. c....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT