Similar Problems
Similar Problems not available
Recyclable And Low Fat Products - Leetcode Solution
LeetCode: Recyclable And Low Fat Products Leetcode Solution
Difficulty: Easy
Topics: database
Problem Statement:
You need to design a system for a grocery store to manage their inventory of products. The system should allow the store to track recyclable and low-fat products.
The following information should be tracked for each product:
- Name (string)
- Category (string)
- Price (float)
- Quantity (integer)
- Is Recyclable (boolean)
- Is Low Fat (boolean)
Your system should support the following operations:
- Add a new product
- Remove an existing product
- Update the quantity of a product
- Update whether a product is recyclable or low fat
- Get the average price of all products in a certain category
- Get the total quantity of all products
- Get the total quantity of recyclable and/or low fat products
Solution:
To solve this problem, we can design a class Product that will hold all the necessary information about each product. We can then use a list data structure to store all the products in the store's inventory. The list will allow us to easily add, remove, and update products as well as calculate the necessary statistics.
Here is the class definition for the Product class:
class Product: def init(self, name, category, price, quantity, is_recyclable=False, is_low_fat=False): self.name = name self.category = category self.price = price self.quantity = quantity self.is_recyclable = is_recyclable self.is_low_fat = is_low_fat
We initialize the Product class with all the necessary properties, including default values of False for is_recyclable and is_low_fat.
Next, we can create a Store class that will manage the inventory of products:
class Store: def init(self): self.inventory = []
def add_product(self, product):
self.inventory.append(product)
def remove_product(self, product):
self.inventory.remove(product)
def update_quantity(self, product, quantity):
product.quantity = quantity
def update_recyclable(self, product, is_recyclable):
product.is_recyclable = is_recyclable
def update_low_fat(self, product, is_low_fat):
product.is_low_fat = is_low_fat
def get_avg_price(self, category):
prices = [product.price for product in self.inventory if product.category == category]
if not prices:
return 0
return sum(prices) / len(prices)
def get_total_quantity(self):
return sum([product.quantity for product in self.inventory])
def get_total_recyclable(self):
return sum([product.quantity for product in self.inventory if product.is_recyclable])
def get_total_low_fat(self):
return sum([product.quantity for product in self.inventory if product.is_low_fat])
The Store class has methods to add, remove, and update products, as well as methods to calculate the average price of products in a category, the total quantity of all products, and the total quantity of recyclable and/or low fat products.
Here is an example usage of the Store class:
store = Store()
Add products to inventory
product1 = Product("Apple", "Fruit", 0.99, 10, True, True) store.add_product(product1)
product2 = Product("Bread", "Bakery", 2.99, 5, False, True) store.add_product(product2)
product3 = Product("Milk", "Dairy", 3.49, 2, True, False) store.add_product(product3)
Update quantity of a product
store.update_quantity(product1, 15)
Update whether a product is recyclable or low fat
store.update_recyclable(product2, True) store.update_low_fat(product3, True)
Calculate average price of all products in a category
print(store.get_avg_price("Fruit")) # Output: 0.99
Calculate total quantity of all products
print(store.get_total_quantity()) # Output: 22
Calculate total quantity of recyclable and/or low fat products
print(store.get_total_recyclable()) # Output: 25 print(store.get_total_low_fat()) # Output: 17
Overall, our solution provides a simple way to manage the inventory of a grocery store, track recyclable and low fat products, and calculate important statistics.
Recyclable And Low Fat Products Solution Code
1