Question

Consider the following Python program, where x is assumed to be a list of integers. def...

Consider the following Python program, where x is assumed to be a list of integers.

def mystery_func(x):

n = len(x)

for i in range(n - 1):

for j in range(i + 1, n):

if x[j] - x[i] < j - i: return False

return True

3a. In plain English, describe what it accomplishes (i.e., the relationship between input and output, not how the code works).

3b. Analyze the running time and express it with big O.

3c. Rewrite this function so it accomplishes the same thing but is more efficient, and state the running time in a comment.

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

Answer:

3a) input is array and output is false or true on the basis of given comparison

Here two loops are used to travel array, i indexed array is outermost and j indexed array is innermost

if condition is checking whether "difference between value at jth location and value at ith location" is less than "difference between index value j and index value i". if the above mentioned condition right/true then it return false value otherwise it return true value.

3b) It has two nested for loop hence its time complexity is O(n2)

3c) Lets consider array x is already sorted in ascending order then no need of j indexed array. because condition is always true. only above mentioned case can reduce its running time.

def mystery_func(x):

n = len(x)

for i in range(n - 1):

if x[i+1] - x[i] < 1: return False

return True

Here running time is O(n)

Add a comment
Know the answer?
Add Answer to:
Consider the following Python program, where x is assumed to be a list of integers. def...
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 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...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • Consider the following Python function: def find_max (L): max = 0 for x in L: if...

    Consider the following Python function: def find_max (L): max = 0 for x in L: if x > max: max = x return max Suppose list L has n elements. In asymptotic notation, determine the best case running time as function of n In asymptotic notation, determine the worst case running time as function of n Now, assume L is sorted. Give an algorithm that takes asymptotically less time than the above algorithm, but performs the same function. Prove that...

  • I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result =...

    I need to rewrite this code without using lambda function using python. def three_x_y_at_one(x): result = (3 * x *1) return result three_x_y_at_one(3) # 9 zero_to_four = list(range(0, 5)) def y_values_for_at_one(x_values): return list(map(lambda x : three_x_y_at_one(x), x_values)) -------> this is the function that I need to rewrite without using a lambda function . y_values_for_at_one(zero_to_four) # [0, 3, 6, 9, 12] Thanks

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • 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 ),...

  • Determine the Big-O value for the following functions. The valid Big-O options are: 'O(1)' 'O(N)' 'O(LOG2(N))'...

    Determine the Big-O value for the following functions. The valid Big-O options are: 'O(1)' 'O(N)' 'O(LOG2(N))' 'O(N * LOG2(N))' 'O(N^2)' 'O(inf)' # This is Big-O of infinity def function_one(list_): for pass_ in range(len(list_) - 1, 0, -1): for i in range(pass_): if list_[i] > list_[i+1]: temp = list_[i] list_[i] = list_[i+1] list_[i+1] = temp def function_two(list_): if len(list_) > 1: midpoint_index = len(list_) // 2 left_half = list_[:midpoint_index] right_half = list_[midpoint_index:] function_two(left_half) function_two(right_half) left_half_pos = 0 right_half_pos = 0 new_position...

  • PYTHON 3: An n x n matrix forms a magic square if the following conditions are...

    PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...

  • Consider the following problem: Input: a list of n-1 integers and these integers are in the...

    Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...

  • 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