For the next question, we look again at priority queues. A priority queue is a queue...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
For the next question, we look again at priority queues. A priority queue is a queue in which each element has a priority, and where dequeueing always returns the item with the greatest priority in the queue We start by defining a class of priority queue elements (PQ-elements for short): class PQElement: def _init_(self, v, p): self.val = v self.priority p So, a PQ-element is a pair consisting of a value (which can be anything, e.g. an integer, a string, an array, etc.) and a priority (which is an integer). In Lab 5 we also implemented the_str_ function to be able to print PQ-elements. Question 7 Write a Python class PQueue that implements a priority queue using a heap of PQElement 's. In particular, you need o implement 5 functions: one for creating an empty priority queue • one for returning the size of the priority queue • one for enqueueing a new PQ-element in the priority queue • one for dequeueing from the priority queue the PQ-element with the greatest priority • one that prints the elements of the priority queue into a string (call this one _str_) Test each of the functions on examples of your own making. For example, running: pq = PQueue () A = [(1,9), (3,7), (13,-3), (0, 10), (4,6), (5,5), (6,4), (2,8), (7,3), (9, 1), (14,-4), (10, 0), (11,-1), (8,2), (12,-2)] for x in A: pq.enq (PQElement(x [0],x[1])) print (pq) print (pq.deq(),pq) should give this printout: [(0,10), (1,9), (2,8), (3,7), (4,6), (5,5), (6,4),(7,3), (8,2), (9, 1), (10,0), (11,-1), (12,-2), (13,-3), (14,-4)] (0,10) [(1,9), (2,8), (3,7),(4,6), (5,5), (6,4), (7,3), (8,2), (9, 1), (10,0), (11,-1), (12,-2), (13,-3), (14,-4) 1. Note: the print function should print the queue elements in descending priority order, without changing the queue. One idea is to use the function toSorted Array from Question 4. Activate Wi Go to Settings t For the next question, we look again at priority queues. A priority queue is a queue in which each element has a priority, and where dequeueing always returns the item with the greatest priority in the queue We start by defining a class of priority queue elements (PQ-elements for short): class PQElement: def _init_(self, v, p): self.val = v self.priority p So, a PQ-element is a pair consisting of a value (which can be anything, e.g. an integer, a string, an array, etc.) and a priority (which is an integer). In Lab 5 we also implemented the_str_ function to be able to print PQ-elements. Question 7 Write a Python class PQueue that implements a priority queue using a heap of PQElement 's. In particular, you need o implement 5 functions: one for creating an empty priority queue • one for returning the size of the priority queue • one for enqueueing a new PQ-element in the priority queue • one for dequeueing from the priority queue the PQ-element with the greatest priority • one that prints the elements of the priority queue into a string (call this one _str_) Test each of the functions on examples of your own making. For example, running: pq = PQueue () A = [(1,9), (3,7), (13,-3), (0, 10), (4,6), (5,5), (6,4), (2,8), (7,3), (9, 1), (14,-4), (10, 0), (11,-1), (8,2), (12,-2)] for x in A: pq.enq (PQElement(x [0],x[1])) print (pq) print (pq.deq(),pq) should give this printout: [(0,10), (1,9), (2,8), (3,7), (4,6), (5,5), (6,4),(7,3), (8,2), (9, 1), (10,0), (11,-1), (12,-2), (13,-3), (14,-4)] (0,10) [(1,9), (2,8), (3,7),(4,6), (5,5), (6,4), (7,3), (8,2), (9, 1), (10,0), (11,-1), (12,-2), (13,-3), (14,-4) 1. Note: the print function should print the queue elements in descending priority order, without changing the queue. One idea is to use the function toSorted Array from Question 4. Activate Wi Go to Settings t
Expert Answer:
Answer rating: 100% (QA)
an implementation of a priority queue PQueue using a heap of PQElement s in Python class PQElement def initself v p selfval v selfpriority p def strse... View the full answer
Related Book For
Smith and Roberson Business Law
ISBN: 978-0538473637
15th Edition
Authors: Richard A. Mann, Barry S. Roberts
Posted Date:
Students also viewed these algorithms questions
-
Differential Analysis for Machine Replacement Ridgeway Digital Components Company assembles circuit boards by using a manually operated machine to insert electronic components. The original cost of...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
A self-organizing list is a linked list of n elements, in which each element has a unique key. When we search for an element in the list, we are given a key, and we want to find an element with that...
-
A firm has total debt of $6,000,000 and stockholder's equity is $4,000,000. The firm wants to calculate equity-to- total asset ratio in order to make decision about further raise of capital. What is...
-
A microscope with an objective of focal length 8.00 mm and an eyepiece of focal length 7.50 cm is used to project an image on a screen 2.00 m from the eyepiece. Let the image distance of the...
-
The tensile strength of portland cement is being studied. Four different mixing techniques can be used economically. The following data have been collected: (a) Test the hypothesis that mixing...
-
Conduct the following analyses for the Internet usage data given in Applied Problem 11-18 in Chapter 11. a. Find the simple correlations between the following sets of variables: Internet usage and...
-
The controller of Vestin Co. believes that the yearly allowance for doubtful accounts for Vestin Co. should be 2% of net credit sales. The president of Vestin Co., nervous that the stockholders might...
-
Required Information [The following Information applies to the questions displayed below.] Simon Company's year-end balance sheets follow. At December 31 Assets Current Year 1 Year Ago 2 Years Ago...
-
Wedding Planners Limited (WP), owned by Anne and Francois Tremblay, provides wedding planning and related services. WP owns a building (the Pavilion) that has been custom-made for hosting weddings....
-
What is the best way to view a spectrum on paper? O 1.A graph of intensity vs. peak energy of the light emitted by a given source can be plotted on p O 2. A graph of velocity vs. frequency of the...
-
The article states that a cheetah can have a home range > 3,000 km 2 . Calculate the side of a square required in miles to provide this area.
-
When the researchers asked participants to fill out a questionnaire, 10% of exposed cases and 20% of unexposed cases could not be located. What type of bias is this? In what direction? Show your work...
-
If the target food cost percent is 33%, what would the selling price of macaroni and cheese be if the food cost was $1.25 per portion?
-
Susie takes out a car loan for $10,040 for a term of 5 years at 12% interest compounded monthly. Complete parts (a) through (g). (a) Find Susie's monthly payment. Her monthly payment is $ . (Round to...
-
H has just received a payment of $13,250 from a trust fund. Payments from the trust fund are expected to increase by 2.1% per year for the next four years, by 3.25% for the following seven years, and...
-
The value of the surface integral [(x*y+ yz + x) ds. where Sis the surface of the sphere x + y + 2 = 1 S is 2 3 4t 15 0 47 5
-
San Carlos Bank and Trust Company uses a credit-scoring system to evaluate most consumer loans that amount to more than $2,500. The key factors used in its scoring system are found at the conclusion...
-
Paul Bunyan is the owner of noncumulative 8 percent preferred stock in the Broadview Corporation, which had no earnings or profits in 2009. In 2010, the corporation had large profits and a surplus...
-
Every contract, combination in the form of a trust or otherwise, or, conspiracy, in restraint of trade is made illegal by 1 of the Sherman Act, [citation]. The question whether an arrangement is a...
-
Civil Code 1719, subdivision (a) provides in part that any person who draws a check that is dishonored due to insufficient funds shall be liable to the payee for the amount owing upon the check and...
-
Based on the photographs in Figure 26.13, in which segment(s) is the Antp gene normally expressed? Figure 26.13: (a) Normal fly (b) Antennapedia mutant
-
The bush baby, a small African mammal, is a remarkable jumper. Although only about 8 inches long, it can jump, from a standing start, straight up to a height of over 7 feet! Use the particle model to...
-
Your friend Travis claims to have set the new world speed record for riding a unicycle. His top speed, he says, was 55 m/s. Do you believe him? Explain.
Study smarter with the SolutionInn App