Python Programming (Write a Function that partitions a list into best of 3) This
ID: 3588375 • Letter: P
Question
Python Programming (Write a Function that partitions a list into best of 3)
This function should work with all lengths of 3 or more. It should return only one value (The best or median of the 3 partitions)
I know division is needed and that we should throw away the decimal and round down when finding the indices from uneven lists.
Best of Three Pivot Consider the values at the leftmost, middle, and rightmost indice. Of those three values, pick the middle value (NOT middle index, middle value) example list: 18962421673955 8 5 6 7 8 910 11 12 13 14 15 16 pivot: three values 2, 1, 4 median value 2Explanation / Answer
def bestOf3(arr):
if len(arr) < 3:
return
temp = []
temp.append(arr[0])
temp.append(arr[-1])
mid = len(arr)//2
temp.append(arr[mid])
res = ", ".join([str(i) for i in temp])
print("three values " + res)
temp.sort()
print("median value " + str(temp[1]))
return temp[1]
l = [2,1, 8, 9, 6, 2, 4, 2, 1, 6, 7, 3, 9, 5, 5, 8, 4]
bestOf3(l)
# code link: https://paste.ee/p/WVBxe
sample exeuction
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.