Question: Python 3 PLEASE FOLLOW INSTRUCTIONS #COMMENT STEPS PLEASE Problem Complete the function averagePartition() to take in an array, and partition it according to the Quick

Python 3

PLEASE FOLLOW INSTRUCTIONS

#COMMENT STEPS PLEASE

Python 3 PLEASE FOLLOW INSTRUCTIONS #COMMENT STEPS PLEASE Problem Complete the function

Problem

Complete the function averagePartition() to take in an array, and partition it according to the Quick Sort approach we studied in class by always picking the element closest to average as the pivot. I have already given you a helper function that returns the index of the element in the given array that is closest to the provided target.

Remember, Quick Sort partitions an array based on some pivot chosen from the elements in the array. Once we pick the pivot, we partition the array by placing all elements less than the pivot to the left of the pivot, and all elements greater than the pivot to the right of the pivot.

Hint: if you want to adhere to the approach of swapping elements shown in class, consider putting the pivot at the end of the array once you determine it.

Examples

 
 

averagePartition([24, 20, 8, 44, 6, 9, 5])

In the example above, 20 is the closest to the average (16.6) of the array, so your function should return [5, 8, 6, 9, 20, 44, 24] or any variation of that ordering where 20 appears in the 4th index, and everything to the left of 20 is smaller than 20, and everything to the right is greater than 20

Instructions from your teacher. Add new file 1- def averagePartition(array): Problem 3- def closestToTarget(array, target): closest_index = 0 5 for i in range(1, len(array)): 6 | if abs (array[i] - target)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!