Question

In: Computer Science

Task1: Ternary Partition (4 Marks) Write a function ternary partition(lst) that partitions an unsorted list into...

Task1: Ternary Partition

Write a function ternary partition(lst) that partitions an unsorted list into three sections: smaller than pivot, equal to pivot, larger than pivot.

Input: an unsorted list containing one or more integers.

Output: a pair of integers, where the first integer is the index of the final position of the pivot, and the second integer is the index of the first element that is larger than the pivot. The pivot should be the element in the 0th position in the original list. To receive full marks the original list must be partitioned; however, the list is not returned.

Examples

a) Let lst1=[3]. Calling ternary partition(lst1) returns (0,1), as after calling function lst1=[3], thus the pivot section starts at 0 and ends at 1 (exclusive).

Solutions

Expert Solution

SOLUTION a:

##define a function for ternary partiton
##i am doing it in the same basis as quick sort
def ternaryPartition(a):
pivotIndex=0 ##store the pivotIndex with the intial index
pivot=a[0] ##store the pivot
  
for i in range(len(a)):
if(a[i]<pivot):
## if an item is less than pivot swap the details and increase the index   
pivotIndex=pivotIndex+1
##swap the current index value with the pivotIndex
a[i],a[pivotIndex]=a[pivotIndex],a[i]
## Here we are not swapping the pivot value with the lesser value since it will not check if the previous values are greater
##we are doing swap with the lesser so if the value is greater then it will in the pivotIndex and the pivot will be in the
## zero index itself
  
## we need to swap the pivot index with the zero index so that the data is in the
a[pivotIndex],a[0]=a[0],a[pivotIndex]
print(a)
pivotGreater=pivotIndex+1
##if there are some elements that are equal to the pivot then the index of the
##elements that are greater than pivot will change to check that we are using this
for i in range(pivotIndex,len(a)):
if(a[i]==pivot):
##if the elemets are equal to pivot we need to continue
continue
else:
## this for checking for the index of the element that are greater than the pivot
pivotGreater=i
break
##partion List like this

lessThanPivot=a[0:pivotIndex]
equalPivot=a[pivotIndex:pivotGreater]
greaterThanPivot=a[pivotGreater:]
print("List less than Pivot",lessThanPivot)
print("Equal List",equalPivot)
print("Greater than pivot list ",greaterThanPivot)
##returning the pivot index and index of the element where the data is greater than pivot
##we are returning an map here
return (pivotIndex,pivotGreater)
  


li=list(map(int,input("Enter list").split(" ")))
##reading input and then casting it to int
##since map returns an map so we are convrting it to list

##calling the function
print("Returning data from ternary function",ternaryPartition(li))
     
  
EXPLANATION:

I have written the explanation on each step of the
I have printed the list by partitioning

partion list is written with three names in the program and not returned

SOURCE CODE:


  

OUTPUT:


Related Solutions

Evaluate and write an algorithm to find the largest item in an unsorted singly linked list...
Evaluate and write an algorithm to find the largest item in an unsorted singly linked list with cells containing integers
Thermodynamics and Statistical Mechanics problem: Write the formula for the partition function Z of an electron...
Thermodynamics and Statistical Mechanics problem: Write the formula for the partition function Z of an electron in a hydrogen atom. If the sum is finite, to what value does it converge? If the sum is infinite, prove it
E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98,...
E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31 48 69 94 Programming rules: You must adhere to the same rules when using the built-in Python list as you did in the ArrayList lab. You may not use any other data structures (this includes the built-in Python list). class ArrayList: def __init__(self): self.data =...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write pseudocode (3 Marks) and program structure (4 Marks) for the problem given below; In a...
Write pseudocode and program structure (4 Marks) for the problem given below; In a college, students are awarded a pass grade if their total mark is between 50-59, credit grade if the mark is between 60-69, distinction for marks between 70-79. High distinction if the mark is above or equal to 80 and fail if the mark is below 50.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT