Question

I need help for the order of growth for functions in Python 3. Q1: What is...

I need help for the order of growth for functions in Python 3.

Q1: What is the order of growth for the following functions?

Kinds of Growth

Here are some common orders of growth, ranked from no growth to fastest growth:

1. Θ(1) — constant time takes the same amount of time regardless of input size

2. Θ(log n) — logarithmic time

3. Θ(n) — linear time

4. Θ(n log n) — linearithmic time

5. Θ(n2 )

6. Θ(n3 ), etc. — polynomial time

7. Θ(2n), Θ(3n), etc. — exponential time (considered “intractable”; these are really, really horrible)

8. Something else not listed

You should create a parameter-free function called question1 and return a list with 10 numbers.  You should put the corresponding growth rate (1-8) for each of the problems listed below.

For example:

Answer: 3 (that is linear time)

Explanation (you do not have write it but you will in the exam, so make sure you understand your choice):

There is a single loop that runs n−5 times. Each time the loop runs it executes 1 instruction in the loop header and 1 instruction in the body of the loop. The total number of instructions is 2 ∗ (n − 5) + 1 (for the last loop check) = 2n − 9 = Θ(n)

If you need more examples, you can look here: https://www.comp.nus.edu.sg/~cs1020/tut/15s2/tut09ans/T9_ans.pdf

Please note:

1) They use big-O notation, which is the same as big-Theta for our purposes. (Big-O is an upper bound and Big-Theta is a tight bound, but in computer science both bounds are often used interchangeably.)

2) They use Java syntax in their loops. You can read it as:

for (int i = 0; i < n; i=i+1)

a) i is initialized to a 0.

b) if i is less than n, perform one iteration of the loop

c) increment i by 1, check if i is less than n. If it is, run the loop again etc; if i is greater than n, stop.

Equivalent loop in Python is: for i in range(0, n)

Question 1.1

def foo(n):
   i = 1
   sum = 0
   while i <= 20**20:
       sum = sum + i
       i = i * 2
   return sum

Question 1.2

def func(n):
   sum = 0
   for i in range(n):
            sum = sum + n
   for j in range(n * n * n):
            sum = sum + 10
   return sum

Question 1.3

def func(n):
   j = 1
   sum = 100
   while j <= n:
           for i in range(1, n//2):
                   sum = sum + i*j
            j = j + 1
   return sum

Question 1.4

def func(n):
       sum = 111
       for i in range(1, n+2):
               for j in range(i):
                   sum = i + j
       return sum

Question 1.5

def func(n):
       i = 1
       sum = 0
       while i <= n:
               sum = sum * i
               i = i * 5
       return sum

Question 1.6

def mod_10(n):
       if n % 10 == 0:
               return 10
       else:
               return 1 + mod_10(n-1)

Question 1.7

Calculate the complexity for foo(bar(n)). To see pattern I'd suggest you to take different inputs for n and write down the recursion tree.

def bar(n):
       if n%2 == 1:
               return n + 1
       return n


def foo(n):
       if n < 1:
           return 2
       if n % 2 == 0:
           return foo(n-1) + foo(n-2)
       else:
           return 1 + foo(n-2)

Question 1.8

def func(alist):
       for num in range(len(alist)-1,0,-1):
               for i in range(num):
                       if alist[i] > alist[i+1]:
                           alist[i], alist[i+1] = alist[i+1], alist[i]

Question 1.9

def func(n):
   for i in range(0,n):
            for j in range (i+1, i, -1):
               for k in range(n, j, -1):
                   print("print")

Question 1.10

Calculate the complexity for second(n).

def first(n):
       for i in range(0, n):
           print("one")

def second(n):
       for i in range(0, n):
           first(n)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note : O(c*n) = O(n + k) = O(n) where c, k are constants.

O(c1*n) * O(c2*n^2) *O(1) = O(n^3).

Add a comment
Know the answer?
Add Answer to:
I need help for the order of growth for functions in Python 3. Q1: What is...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the...

    Python Merge Sort Adjust the following code so that you can create random lists of numbers of lengths 10, 15, and 20. You will run the merge sort 10 times for each length. Record the run time for the length and then calculate the average time to sort. Finally, compare the average run time for each length to the average time for the Merge Sort. -------------------------------------------- Initial python code: import random import time def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid...

  • 8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n...

    8.         R-4.8 Order the following functions by asymptotic growth rate. 4nlogn + 2n 2^10    2^logn 3n + 100logn      4n     2^n n^2 + 10n        n^3       nlogn 9.         R-4.9 Give a big-Oh characterization, in terms of n, of the running time of the example 1 method shown in Code Fragment 4.12. 10.       R-4.10 Give a big-Oh characterization, in terms of n, of the running time of the example 2 method shown in Code Fragment 4.12. 11.       R-4.11 Give a big-Oh characterization, in...

  • Python 3 5. (16 points) Determine the big-O running time of each of the following functions:...

    Python 3 5. (16 points) Determine the big-O running time of each of the following functions: def pi (a) for i in range(len (a)): print (a[i]) for i in range(len(a)): print (ali]) def p2(a): for i in rangeClen(a)): for j in a: print (ati].j) def p3(a): for i in a: for j in a: print (i,j) def p4(a): for i in range(len(a)): pi(a) def p5(a): for i in range(len(a)): p3 (a) def p6(a): for i in range(len(a)): p5(a) def p7...

  • I need to write a program based on two finished programs (listm.py and setunion.py) to implement...

    I need to write a program based on two finished programs (listm.py and setunion.py) to implement the intersection operation between two sets of list. The program should not use the Python built-in list and set related functions and operations. I'm sorry about the program format is wrong(no "TAB" space), here is the same question I posted 2 days ago, but the question title is incorrect, there have complete images of my 3 Python program. The link is: https://www.chegg.com/homework-help/questions-and-answers/need-write-program-based-two-finished-programs-listmpy-setunionpy-implement-intersection-o-q44800953?trackid=uNY0ilRc Thank you!...

  • in my c++ class i need help with these question please Question 1. Indicate whether the...

    in my c++ class i need help with these question please Question 1. Indicate whether the first function of each of the following pairs has a smaller, same, or larger order of growth (to within a constant multiple) than the second function. Use the correct notation to indicate the order of growth (f(n) ∈O(g(n)), Ω(g(n)), or Θ(g(n)) as applicable). Prove your statement using limits. (a) (lnn)2 and lnn2 (b) 42n+1 and 42n Question 2. Use the formal definitions of O,...

  • Python 3 Question: All I need is for someone to edit the program with comments that...

    Python 3 Question: All I need is for someone to edit the program with comments that explains what the program is doing (for the entire program) def main(): developerInfo() #i left this part out retail = Retail_Item() test = Cash_Register() test.data(retail.get_item_number(), retail.get_description(), retail.get_unit_in_inventory(), retail.get_price()) answer = 'n' while answer == 'n' or answer == 'N': test.Menu() print() print() Number = int(input("Enter the menu number of the item " "you would like to purchase: ")) if Number == 8: test.show_items() else:...

  • What are the outputs for each question PYTHON Question 6 bools = [True, 0>-1, not not...

    What are the outputs for each question PYTHON Question 6 bools = [True, 0>-1, not not True, 2%2 == 0, True and False] index = 0 while bools[index]: index += 1 print(index) a) 0 b) 1 c) 2 d) 3 e) none of the above Question 7 for i in range(2): for j in range(1): print(i, j, end = " ") a) no output b) 0 0 1 0 2 0 c) 0 0 0 1 1 0 1 1...

  • 1 question) Arrange the following in the order of their growth rates, from least to greatest:...

    1 question) Arrange the following in the order of their growth rates, from least to greatest: (5 pts) n3                     n2         nn        lg n     n!       n lg n              2n                     n 2 question)Show that 3n3 + n2 is big-Oh of n3. You can use either the definition of big-Oh (formal) or the limit approach. Show your work! (5 pts.) 3 question)Show that 6n2 + 20n is big-Oh of n3, but not big-Omega of n3. You can use either the definition of big-Omega...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT