Answer to Question #178224 in Python for Ling

Question #178224

A delicious sambal recipe made in Malaysia gains acclaim in Indonesia and the Philippines.


Write a Python program with features that could be extended from any ONE (1) of the business innovations mentioned in the article above. The program should consist of at least THREE (3) functions, ONE (1) repetition and TWO (2) selection structures. It should utilize at least TWO (2) list and ONE (1) text file to store the data and information. Use comments or docstrings to add justification on how the program relates to the selected business innovation.[60 marks]


1
Expert's answer
2021-04-05T00:34:21-0400
def heapify(arr, n, i):


    largest = i  # Initialize largest as root


    l = 2 * i + 1     # left = 2*i + 1


    r = 2 * i + 2     # right = 2*i + 2

 

    # See if left child of root exists and is


    # greater than root


    if l < n and arr[largest] < arr[l]:


        largest = l

 

    # See if right child of root exists and is


    # greater than root


    if r < n and arr[largest] < arr[r]:


        largest = r

 

    # Change root, if needed


    if largest != i:


        arr[i], arr[largest] = arr[largest], arr[i]  # swap

 

        # Heapify the root.


        heapify(arr, n, largest)

 
# The main function to sort an array of given size
 
 

def heapSort(arr):


    n = len(arr)

 

    # Build a maxheap.


    for i in range(n//2 - 1, -1, -1):


        heapify(arr, n, i)

 

    # One by one extract elements


    for i in range(n-1, 0, -1):


        arr[i], arr[0] = arr[0], arr[i]  # swap


        heapify(arr, i, 0)

 
 
# Driver code

arr = [12, 11, 13, 5, 6, 7]

heapSort(arr)

n = len(arr)


print("Sorted array is")


for i in range(n):


    print("%d" % arr[i]),

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS