Question

Question-6 (10 pts): Study the following Python function and discuss its time complexity, make sure to...

Question-6 (10 pts): Study the following Python function and discuss its time complexity, make sure to account for time-cost of calling built-in python methods.

def fun(nums: List[int]):

    number_of_elements = len(nums)

    for index in range(number_of_elements):

        value = nums[index]

        n = nums.count(value)

        print('{v} appeared {n} times'.format(value, n))

Homework Answers

Answer #1

Please find the answer for the above given question .

ANSWER :

The time complexity of above given function is O(n^2) [Big O of n square].

The time complexity of len keyword is O(1) [Big O of one].

Here in the given function for loop will run for number of elements that were there in the list that is passed to fun. So if we consider there are n elemets in the list that is passed to fun. So time complexity will be O(n) again.

But inside for loop , we have one keyword which is a count . For that also we have a O(n) time complexity .

So totally it will be O(n^2) [Big O of n square].

Thanks..

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
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT