Do this in python only, import the modules below first before doing anything Sorter.py: import random import enum from abc import ABC, abstractmethod from priorityqueue.HeapPriorityQueue import HeapPriorityQueue class SortingALgorithm(enum.Enum):     PR_Queue = 1     SortedPriorityQueue = 2     UnSortedPriorityQueue =3 class Sorter(ABC):     @abstractmethod     def sort(self) -> list:         """in strategy pattern naming, this is method execute"""         pass class PR_queue_sorter(Sorter):     """ Heap sort Sorting according to the algorithm on slide #10 using a heap implementation     of priority queue"""     def __init__(self):         self._q = HeapPriorityQueue()     def sort(self, seq:list):         i = 0         while len(seq) > 0:             self._q.add(seq[0], None)             del seq[0]         while not self._q.is_empty():             k,v =self._q.remove_min()             seq.append(k)         return seq class Context:     def __init__(self, sorting_algo:SortingALgorithm):         if sorting_algo == SortingALgorithm.PR_Queue:             self._sorter = PR_queue_sorter()     def strategy(self):         """    The Context maintains a reference to one of the Strategy objects. The                Context does not know the concrete class of a strategy. It should work                with all strategies via the Strategy interface.                In our example: Strategy interface: Sorter                Strategy objects: PR_Queue,                """         return self._strategy     def strategy(self, sorter: Sorter) -> None:         """         Usually, the Context allows replacing a Strategy object at runtime.         """         self._sorter = sorter     def sort(self, seq:list):         return self._sorter.sort(seq)  # delegation if __name__ == "__main__":     l1 = [random.randint(0, 100) for i in range(5)]     print(l1)     c = Context(SortingALgorithm.PR_Queue)     l1 = c.sort(l1)     print(l1)     #you can use the same strategy more than once     l2 = [random.randint(0, 100) for i in range(10)]     print(l2)     c = Context(SortingALgorithm.PR_Queue)     l2 = c.sort(l2)     print(l2)

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
We will extend the solution we developed in Sorter.py to support two more sorting strategies, mergeSort and quickSort. 
 
Plugging these two algorithms should not require any changes to the Sorter class public interface. To sort, the client should pass a parameter from the enum to select what algorithm to use, and then call sort with a given list. 

 

Do this in python only, import the modules below first before doing anything


Sorter.py:


import random
import enum
from abc import ABC, abstractmethod

from priorityqueue.HeapPriorityQueue import HeapPriorityQueue

class SortingALgorithm(enum.Enum):
    PR_Queue = 1
    SortedPriorityQueue = 2
    UnSortedPriorityQueue =3

class Sorter(ABC):

    @abstractmethod
    def sort(self) -> list:
        """in strategy pattern naming, this is method execute"""
        pass


class PR_queue_sorter(Sorter):
    """ Heap sort Sorting according to the algorithm on slide #10 using a heap implementation
    of priority queue"""

    def __init__(self):
        self._q = HeapPriorityQueue()

    def sort(self, seq:list):
        i = 0
        while len(seq) > 0:
            self._q.add(seq[0], None)
            del seq[0]
        while not self._q.is_empty():
            k,v =self._q.remove_min()
            seq.append(k)
        return seq


class Context:
    def __init__(self, sorting_algo:SortingALgorithm):
        if sorting_algo == SortingALgorithm.PR_Queue:
            self._sorter = PR_queue_sorter()

    def strategy(self):
        """    The Context maintains a reference to one of the Strategy objects. The
               Context does not know the concrete class of a strategy. It should work
               with all strategies via the Strategy interface.
               In our example: Strategy interface: Sorter
               Strategy objects: PR_Queue,

               """
        return self._strategy

    def strategy(self, sorter: Sorter) -> None:
        """
        Usually, the Context allows replacing a Strategy object at runtime.
        """

        self._sorter = sorter

    def sort(self, seq:list):
        return self._sorter.sort(seq)  # delegation

if __name__ == "__main__":

    l1 = [random.randint(0, 100) for i in range(5)]
    print(l1)
    c = Context(SortingALgorithm.PR_Queue)
    l1 = c.sort(l1)
    print(l1)

    #you can use the same strategy more than once
    l2 = [random.randint(0, 100) for i in range(10)]
    print(l2)
    c = Context(SortingALgorithm.PR_Queue)
    l2 = c.sort(l2)

    print(l2)

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning