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...
-
Robin inherits 1,000 shares of Wal-Mart stock from her aunt in 2016. According to the information received from the executor of her aunt's estate, Robin's adjusted basis for the stock is $55,000....
-
A machine is supposed to fill soft drink cans to 12 ounces. It appears that although the average amount in the cans is about 12 ounces (based on sample means), there is a great deal of variability in...
-
\(\{293,7,298,19,229\}\) Find the greatest common divisor of the given set of numbers.
-
Data-Check is considering two capital structures. The key information is shown in the following table. Assume a 40% tax rate. a. Calculate two EBIT-EPS coordinates for each of the structures by...
-
Required: Prepare a statement of changes in equity for the year ended Dec. 31, 2021. PERFECTNA Company reported the following amounts in the shareholders' equity section of its December 31, 2011...
-
A periodic signal x[n] of fundamental period N can be represented by its Fourier series If you consider this a representation of x[n] (a) Is x 1 [n] = x[n N 0 ] for any value of N 0 periodic? If so...
-
A company estimates that total overhead will be $272,500. Of the overhead, $92,500 consists of machine-related costs and the balance is direct labor-related costs. They expect to work 15,000 labor...
-
ANALYZE THE RELEVANT ENVIRONMENT that you could face in your chosen career/industry and 4. Carefully describe your PERSONAL ETHICAL STATEMENT (see AMA Code) that will guide your decision making as a...
-
If the total obligation is $950 and borrower income is $4,500, the total obligation ratio is?
-
Mr. Morani doesn't fall under the provisions of A112(2), or A112(3), and is eligible to apply for PRRA. Mr. Morani has no previous refugee or PRRA claims, so he could present all the personal and...
-
Suppose that Bob sends N back-to-back packets (numbered 1 to N) to Alice over two links (shown below) with the link between Bob's computer and the router having a transmission rate of R bits/sec...
-
A certain account pays 3.12% interest, compounded weekly. Audrey wishes to invest money into this account today so that she will be able to withdraw $100 every week for the next year. a) How much...
-
The market value of a company's equity is $160 million and the market value of a company's debt is $80 million. The before-tax cost of debt is 8.5%. The stock has a beta of 1.1. The U.S. Treasury...
-
Organizations are increasing their use of personality tests to screen job applicants. What are some of the advantages and disadvantages of this approach? What can managers do to avoid some of the...
-
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...
-
Numerically solve the nonlinear pendulum problem using the EulerCromer Method for a pendulum with length \(L=0.5 \mathrm{~m}\) using initial angles of \(\theta_{0}=10^{\circ}\), and...
-
Use i) Euler's Method and ii) the Midpoint Method to determine the given value of \(y\) for the following problems: a. \(\frac{d y}{d x}=2 y, y(0)=2\). Find \(y(1)\) with \(h=0.1\). b. \(\frac{d y}{d...
-
A certain model of the motion of a light plastic ball tossed into the air is given by \[m x^{\prime \prime}+c x^{\prime}+m g=0, \quad x(0)=0, \quad x^{\prime}(0)=v_{0}\] Here \(m\) is the mass of the...
Study smarter with the SolutionInn App