Priority QueueWrite a class for Cart have it implement __lt__ and __gt__ and __eq__ functionsa cart is less than if it's number of items is 15 or less and the other cart has more than 15 items if...

1 answer below »
In python


Priority Queue Write a class for Cart have it implement __lt__ and __gt__ and __eq__ functions a cart is less than if it's number of items is 15 or less and the other cart has more than 15 items if both carts have less than 15 items they are equal Generate random carts and put them into a less than priority queue import random import heapq class Cart: def __init__(self, num_items): self.num_items = num_items def __lt__(self, other): if self.num_items <= 15="" and="" other.num_items=""> 15: return True else: return False def __gt__(self, other): if self.num_items > 15 and other.num_items <= 15:="" return="" true="" else:="" return="" false="" def="" __eq__(self,="" other):="" if="" self.num_items=""><= 15="" and="" other.num_items=""><= 15:="" return="" true="" else:="" return="" false="" #="" generate="" some="" random="" carts="" carts="[Cart(random.randint(1," 30))="" for="" _="" in="" range(10)]="" #="" put="" them="" into="" a="" less="" than="" priority="" queue="" heapq.heapify(carts)="" linked="" objects="" your="" task="" is="" to="" write="" the="" implementation="" for="" a="" class="" of="" polynomial="" operations.="" your="" will="" write="" the="" code="" for:="" addition,="" multiplication,="" differentiation="" and="" integration="" of="" polynomials.="" the="" polynomials="" will="" be="" links="" of="" termnodes.="" class="" termnode=""  exponent="" :="" int=""  coefficient="" :="" float=""  next="" :="" termnode=""  __eq__(other:="" termnode)="" :="" bool=""  __ne__(other:="" termnode)="" :="" bool =""  ="" class="" polynomial=""  _first_node="" :="" termnode ="" __init__(="" coefficient,="" exponent="" ) =""  __add__="" (polynomial)="" :="" polynomial =""  __mul__(polynomial)="" :="" polynomial=""  differentiate()="" :="" polynomial=""  integrate()="" :="" polynomial="" #(with="" 0="" as="" the="" constant) ="" __str__="" :="" string="" #="" in="" descending="" exponential="" order="" -="" clean="" up="" anything="" x^0 ="" -="" coefficient="" to="" 2="" decimal="" places ="" __eq__(other:="" polynomial="" )="" :="" bool="" __ne__(other:="" polynomial="" )="" :="" bool=""  ="" remember="" -="" the="" originals="" don't="" change,="" you="" create="" a="" new="" polynomial="" result=""  ="" unit="" tests=""  ="" examples:="" poly2="Polynomial(2,3)" #="" makes="" the="" polynomial="" 2.00x^3 ="" poly3="Polynomial(3,4)" #="" makes="" the="" polynomial="" 3.00x^4 ="" poly1="poly2" +="" poly3;="" #="" makes="" poly1="3.00x^4" +="" 2.00x^3 ="" print(poly1)="" #="" prints="" out="" 3.0x^4="" +="" 2.00x^3 ="" poly3="poly2*poly1" #="" sets="" poly3="" to="" 6.00x^7+4.00x^6 ="" poly4="poly3.differentiate()" #="" sets="" poly4="" to="" 42.00x^6+24.00x^5 ="" poly5="poly1.integrate()" #="" sets="" poly5="" to="" .60x^5+.50x^4 ="" class="" termnode:="" def="" __init__(self,="" coefficient,="" exponent):="" self.coefficient="coefficient" self.exponent="exponent" self.next="None" def="" __eq__(self,="" other):="" return="" (self.coefficient="=" other.coefficient="" and="" self.exponent="=" other.exponent)="" def="" __ne__(self,="" other):="" return="" not="" self.__eq__(other)="" class="" polynomial:="" def="" __init__(self,="" coefficient="0," exponent="0):" self._first_node="TermNode(coefficient," exponent)="" def="" __add__(self,="" other):="" result="Polynomial()" result_node="result._first_node" node1="self._first_node" node2="other._first_node" while="" node1="" is="" not="" none="" or="" node2="" is="" not="" none:="" if="" node1="" is="" none:="" result_node.next="node2" break="" elif="" node2="" is="" none:="" result_node.next="node1" break="" elif="" node1.exponent="=" node2.exponent:="" new_coeff="node1.coefficient" +="" node2.coefficient="" if="" new_coeff="" !="0:" result_node.next="TermNode(new_coeff," node1.exponent)="" result_node="result_node.next" node1="node1.next" node2="node2.next" elif="" node1.exponent=""> node2.exponent: result_node.next = TermNode(node1.coefficient, node1.exponent) result_node = result_node.next node1 = node1.next else: result_node.next = TermNode(node2.coefficient, node2.exponent) result_node = result_node.next node2 = node2.next return result def __mul__(self, other): result = Polynomial() for node1 in self: for node2 in other: new_coeff = node1.coefficient * node2.coefficient new_exp = node1.exponent + node2.exponent temp = Polynomial(new_coeff, new_exp) result += temp return result def differentiate(self): result = Polynomial() node = self._first_node.next while node is not None: if node.exponent != 0: new_coeff = node.coefficient * node.exponent new_exp = node.exponent - 1 result._add_term(new_coeff, new_exp) node = node.next return result def integrate(self): result = Polynomial(0, 1) node = self._first_node while node is not None: new_coeff = node.coefficient / (node.exponent + 1) new_exp = node.exponent + 1 result._add_term(new_coeff, new_exp) node = node.next return result def __str__(self): result = '' node = self._first_node.next while node is not None: coeff = round(node.coefficient, 2) if coeff == 0: node = node.next continue if coeff > 0 and len(result) > 0: result += '+' if abs(coeff) != 1 or node.exponent == 0: result += str(coeff) if node.exponent > 0: result += 'x' if node.exponent > 1: result += '^' + str(node.exponent) node = node.next return result if len(result) > 0 else '0' def __eq__(self, other
Answered 1 days AfterMar 18, 2023

Answer To: Priority QueueWrite a class for Cart have it implement __lt__ and __gt__ and __eq__ functionsa...

Vikas answered on Mar 20 2023
35 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here