Python question Analysis: Computational Complexity (Q14-15) In this section, you are asked to analyse the worst-case computational time complexity of Python functions. Always identify the order of growth in the tightest and simplest possible form using big-O notation, e.g., write O(n) instead of O(2n+n/2). Question 14 (Complexity 1) State the overall worst-case time complexity of the below function and provide a short explanation of your answer. The function accepts an integer n as input. def mystery(n):     num = 1     count = 0     while num < n:         num = num*2         count += 1     return count   Question 15 (Complexity 2) State the overall worst-case time complexity of the below function and provide a short explanation of your answer. The function is supposed to return a list, res, containing the duplicated elements in the given list , lst, of size n.   Analyse the function in terms of the length of the list, n. def duplicate_values(lst):     res = []     for i in range(len(lst)):         if lst[i] in lst[i+1:]:             res.append(lst[i])     return res

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter4: Selection Structures
Section: Chapter Questions
Problem 14PP
icon
Related questions
Question

Python question

Analysis: Computational Complexity (Q14-15)

In this section, you are asked to analyse the worst-case computational time complexity of Python functions. Always identify the order of growth in the tightest and simplest possible form using big-O notation, e.g., write O(n) instead of O(2n+n/2).

Question 14 (Complexity 1)

State the overall worst-case time complexity of the below function and provide a short explanation of your answer. The function accepts an integer n as input.

def mystery(n):
    num = 1
    count = 0
    while num < n:
        num = num*2
        count += 1
    return count

 

Question 15 (Complexity 2)

State the overall worst-case time complexity of the below function and provide a short explanation of your answer. The function is supposed to return a list, res, containing the duplicated elements in the given list , lst, of size n.   Analyse the function in terms of the length of the list, n.

def duplicate_values(lst):
    res = []
    for i in range(len(lst)):
        if lst[i] in lst[i+1:]:
            res.append(lst[i])
    return res

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning