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

1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
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.
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List...
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List ADT using the following specification. MergeLists(SortedType list1, SortedType list2, SortedType& result) Function: Merge two sorted lists into a third sorted list. Preconditions: list1 and list2 have been initialized and are sorted by key using function ComparedTo. list1 and list2 do not have any keys in common. Postconditions: result is a sorted list that contains all of the items from list1 and list2. c. Write...
Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
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]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT