Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
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):
XXXXXXXXXXself.num_items = num_items
def __lt__(self, other):
XXXXXXXXXXif self.num_items <= 15 and other.num_items > 15:
XXXXXXXXXXreturn True
XXXXXXXXXXelse:
XXXXXXXXXXreturn False
def __gt__(self, other):
XXXXXXXXXXif self.num_items > 15 and other.num_items <= 15:
XXXXXXXXXXreturn True
XXXXXXXXXXelse:
XXXXXXXXXXreturn False
def __eq__(self, other):
XXXXXXXXXXif self.num_items <= 15 and other.num_items <= 15:
XXXXXXXXXXreturn True
XXXXXXXXXXelse:
XXXXXXXXXXreturn 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
 
emember - 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):
XXXXXXXXXXself.coefficient = coefficient
XXXXXXXXXXself.exponent = exponent
XXXXXXXXXXself.next = None
def __eq__(self, other):
XXXXXXXXXXreturn (self.coefficient == other.coefficient and
XXXXXXXXXXself.exponent == other.exponent)
def __ne__(self, other):
XXXXXXXXXXreturn not self.__eq__(other)
class Polynomial:
def __init__(self, coefficient=0, exponent=0):
XXXXXXXXXXself._first_node = TermNode(coefficient, exponent)
def __add__(self, other):
XXXXXXXXXXresult = Polynomial()
XXXXXXXXXXresult_node = result._first_node
XXXXXXXXXXnode1 = self._first_node
XXXXXXXXXXnode2 = other._first_node
XXXXXXXXXXwhile node1 is not None or node2 is not None:
XXXXXXXXXXif node1 is None:
XXXXXXXXXXresult_node.next = node2

eak
XXXXXXXXXXelif node2 is None:
XXXXXXXXXXresult_node.next = node1

eak
XXXXXXXXXXelif node1.exponent == node2.exponent:
XXXXXXXXXXnew_coeff = node1.coefficient + node2.coefficient
XXXXXXXXXXif new_coeff != 0:
XXXXXXXXXXresult_node.next = TermNode(new_coeff, node1.exponent)
XXXXXXXXXXresult_node = result_node.next
XXXXXXXXXXnode1 = node1.next
XXXXXXXXXXnode2 = node2.next
XXXXXXXXXXelif node1.exponent > node2.exponent:
XXXXXXXXXXresult_node.next = TermNode(node1.coefficient, node1.exponent)
XXXXXXXXXXresult_node = result_node.next
XXXXXXXXXXnode1 = node1.next
XXXXXXXXXXelse:
XXXXXXXXXXresult_node.next = TermNode(node2.coefficient, node2.exponent)
XXXXXXXXXXresult_node = result_node.next
XXXXXXXXXXnode2 = node2.next
XXXXXXXXXXreturn result
def __mul__(self, other):
XXXXXXXXXXresult = Polynomial()
XXXXXXXXXXfor node1 in self:
XXXXXXXXXXfor node2 in other:
XXXXXXXXXXnew_coeff = node1.coefficient * node2.coefficient
XXXXXXXXXXnew_exp = node1.exponent + node2.exponent
XXXXXXXXXXtemp = Polynomial(new_coeff, new_exp)
XXXXXXXXXXresult += temp
XXXXXXXXXXreturn result
def differentiate(self):
XXXXXXXXXXresult = Polynomial()
XXXXXXXXXXnode = self._first_node.next
XXXXXXXXXXwhile node is not None:
XXXXXXXXXXif node.exponent != 0:
XXXXXXXXXXnew_coeff = node.coefficient * node.exponent
XXXXXXXXXXnew_exp = node.exponent - 1
XXXXXXXXXXresult._add_term(new_coeff, new_exp)
XXXXXXXXXXnode = node.next
XXXXXXXXXXreturn result
def integrate(self):
XXXXXXXXXXresult = Polynomial(0, 1)
XXXXXXXXXXnode = self._first_node
XXXXXXXXXXwhile node is not None:
XXXXXXXXXXnew_coeff = node.coefficient / (node.exponent + 1)
XXXXXXXXXXnew_exp = node.exponent + 1
XXXXXXXXXXresult._add_term(new_coeff, new_exp)
XXXXXXXXXXnode = node.next
XXXXXXXXXXreturn result
def __str__(self):
XXXXXXXXXXresult = ''
XXXXXXXXXXnode = self._first_node.next
XXXXXXXXXXwhile node is not None:
XXXXXXXXXXcoeff = round(node.coefficient, 2)
XXXXXXXXXXif coeff == 0:
XXXXXXXXXXnode = node.next
XXXXXXXXXXcontinue
XXXXXXXXXXif coeff > 0 and len(result) > 0:
XXXXXXXXXXresult += '+'
XXXXXXXXXXif abs(coeff) != 1 or node.exponent == 0:
XXXXXXXXXXresult += str(coeff)
XXXXXXXXXXif node.exponent > 0:
XXXXXXXXXXresult += 'x'
XXXXXXXXXXif node.exponent > 1:
XXXXXXXXXXresult += '^' + str(node.exponent)
XXXXXXXXXXnode = node.next
XXXXXXXXXXreturn result if len(result) > 0 else '0'
def __eq__(self, other
Answered 1 days After Mar 18, 2023

Solution

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