Write a Python class PQueue that implements a priority queue using an array list of PQElement...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a Python class PQueue that implements a priority queue using an array list of PQElement 's. In particular, you need to implement 5 functions: _init_: for creating an empty priority queue • size: for returning the size of the priority queue • enq: for enqueueing a new PQ-element in the priority queue • deg for dequeueing from the priority queue the PQ-element with the greatest priority _str_: for printing the elements of the priority queue into a string, in order of decreasing priority Your function for dequeueing should have complexity (1). Test each of the functions on examples of your own making. For example, running: 1s = PQueue () A = [("D",7), ("S",5), ("A",0), ("G",4), ("0",8), ("P",3), ("A",-4), ("S", 1), ("S",-1), ("T",2), ("G",-2)] for x in A: ls.enq (PQElement(x[0],x[1])) print (1s) print(1s.deq(), ls) should give this printout: [('0', 8), ('D', 7), ('5', 5), ('G', 4), ('P', 3), ('T', 2), ('5', 1), ('A', 0), ('S', -1), ('G', -2), ('A', -4)] ('0', 8) [('D', 7), ('5', 5), ('G', 4), ('P', 3), ('T', 2), ('s', 1), ('A', 0), ('s', -1), ('G', -2), ('A', -4)] Write a Python class PQueue that implements a priority queue using an array list of PQElement 's. In particular, you need to implement 5 functions: _init_: for creating an empty priority queue • size: for returning the size of the priority queue • enq: for enqueueing a new PQ-element in the priority queue • deg for dequeueing from the priority queue the PQ-element with the greatest priority _str_: for printing the elements of the priority queue into a string, in order of decreasing priority Your function for dequeueing should have complexity (1). Test each of the functions on examples of your own making. For example, running: 1s = PQueue () A = [("D",7), ("S",5), ("A",0), ("G",4), ("0",8), ("P",3), ("A",-4), ("S", 1), ("S",-1), ("T",2), ("G",-2)] for x in A: ls.enq (PQElement(x[0],x[1])) print (1s) print(1s.deq(), ls) should give this printout: [('0', 8), ('D', 7), ('5', 5), ('G', 4), ('P', 3), ('T', 2), ('5', 1), ('A', 0), ('S', -1), ('G', -2), ('A', -4)] ('0', 8) [('D', 7), ('5', 5), ('G', 4), ('P', 3), ('T', 2), ('s', 1), ('A', 0), ('s', -1), ('G', -2), ('A', -4)]
Expert Answer:
Answer rating: 100% (QA)
class PQElement def initself element priority selfelement el... View the full answer
Related Book For
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these algorithms questions
-
Pederson Company reported the following: Manufacturing costs $2,000,000 50,000 47,000 units sold for $75 Units manufactured Units sold per unit Beginning inventory O units What is the amount of...
-
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...
-
You use the following data to calculate your stock value. Assume you use the historical geometric average growth rate (rounded to 2 decimals) for initial growth rate for 4 years. Then you believe it...
-
The first line of the sharp series of atomic cesium is a doublet with wavelengths 1358.8 and 1469.5 nm. Find the frequency intervals (in rad/s units) between the components of the sequent lines of...
-
A particle travels in a straight line so that, t seconds after passing through a fixed-point O, its speed, v ms -1 , is given by v = 2 cos(t/2) 1. a. Find the value of t when the particle first...
-
The scaling analysis for the mass-transfer boundary-layer thickness is parallel to that for heat transfer analysis. Use the same method to show that \(\Delta\) is proportional to \(S c^{-1 / 3}\) for...
-
On January 1, 2008, Ameen Company purchased a building for $36 million. Ameen uses straight-line depreciation for financial statement reporting and MACRS for income tax reporting. At December 31,...
-
What is the difference in revenue and expense recognition between the cash basis of accounting and the accrual basis of accounting?
-
Annie invested $5,000 in a segregated fund in a non-registered account. At that time, the basis of each unit was worth $10. At the end of the year, the fund made an allocation of $300 to Annie's...
-
In 2021, Remsco has taxable income of $66,000 and a charitable contribution limit modified taxable income of $29,400. Remsco donated cash of $8,040 to a qualified public charity. What is Remsco's...
-
Presented below is information related to Alexis and Ryans, Attorneys at Law Instructions Prepare the 2019 retained earnings statement for Alexis and Ryans, Attorneys at Law. Retained earnings,...
-
Two students are discussing the use of a trial balance. They wonder whether the following errors, each considered separately, would prevent the trial balance from balancing. What would you tell them?...
-
The statement of cash flows classifies each transaction as an operating activity, an investing activity, or a financing activity. Operating activities are the types of activities the company performs...
-
Jarmuz Management Services began business on January 1, 2018, with a capital investment of $90,000. The company manages condominiums for owners (service revenue) and rents space in its own office...
-
(a) The output response depends on present and past inputs. Hence, it is dynamic. (b) The output does not depend on the future input. Hence, it is causal. (c) The output due to the delayed input is...
-
230, 30, 23 20x420, x520. Z=21423 Canonical form maximise S 24-3x5 2x1+x=-2 2110 (0) , X is unrestricted in sign.
-
Consider the advantages and disadvantages of extending property rights so that everyone would have the right to prevent people imposing any costs on them whatsoever (or charging them to do so).
-
Write a program that displays two eyes in an oval, as shown in Figure 31.49c. Exercise31 06 (c)
-
Write a program that converts a red-black tree to a 2-4 tree.
-
Use the Complex class introduced in Programming Exercise 13.17 to develop the ComplexMatrix class for performing matrix operations involving complex numbers. The ComplexMatrix class should extend the...
-
What is the change in velocity of \((a)\) cart 1 (b) cart 2 in Figure 4.6? (c) What do you notice about your two answers? Figure 4.6 Velocity-versus-time graph for two identical carts before and...
-
(a) Are the accelerations of the motions shown in Figure 4.1 constant? (b) For which surface is the acceleration largest in magnitude? Figure 4.1 Velocity-versus-time graph for a wooden block sliding...
-
The \(x\) component of the final velocity of the standard cart in Figure 4.8 is positive. Can you make it negative by adjusting this cart's initial speed while still keeping the half cart initially...
Study smarter with the SolutionInn App