I have two classes - Item and AIShoppingCart (which has dictionary, for example {"milk" : Item(2, 2.28, 1)}). I have to find the most likable item from my cart using reduce and I am not allowed to use max(). Unfortunately, my program throws an error
<lambda>() takes 1 positional argument but 2 were given
class Item:
def __init__(self,quantity,price,likable):
self.quantity = quantity
self.price = price
self.likable = likable
class AIShoppingCart(ShoppingCart):
def __init__(self,items):
super().__init__(items)
def add_item(self,name,quantity,price):
if name in self.items:
self.items[name].likable+=1
self.items[name].quantity+=quantity
else:
items[name]=Item(quantity,price,1)
def findMostLikable(self):
return reduce(lambda x:self.items[x].likable>1, self.items)
How should I use reduce?