Identify Recyclable and Low-Fat Products
This challenge involves filtering a list of products based on two criteria: whether they are recyclable and whether they are low in fat. This is a common task in data management, particularly in inventory systems or consumer product databases, where specific product attributes need to be identified for targeted actions or analysis.
Problem Description
You are given a dataset representing various products, each with a unique identifier, a name, and two boolean attributes: is_recyclable and is_low_fat. Your task is to identify and return the products that satisfy both conditions: they must be recyclable AND low in fat.
Key Requirements
- Filter products based on the
is_recyclableattribute being true. - Filter products based on the
is_low_fatattribute being true. - Return only those products that meet both criteria.
Expected Behavior
The function or program should accept a collection of product objects (or equivalent data structure) and return a new collection containing only the products that are both recyclable and low-fat.
Edge Cases
- An empty input collection of products.
- Products where
is_recyclableis true butis_low_fatis false. - Products where
is_recyclableis false butis_low_fatis true. - Products where both
is_recyclableandis_low_fatare false.
Examples
Example 1:
Input:
[
{ "product_id": 1, "name": "Milk", "is_recyclable": false, "is_low_fat": true },
{ "product_id": 2, "name": "Cereal", "is_recyclable": true, "is_low_fat": true },
{ "product_id": 3, "name": "Juice", "is_recyclable": true, "is_low_fat": false },
{ "product_id": 4, "name": "Butter", "is_recyclable": false, "is_low_fat": false }
]
Output:
[
{ "product_id": 2, "name": "Cereal", "is_recyclable": true, "is_low_fat": true }
]
Explanation:
Product with product_id 2 ("Cereal") is the only one where both "is_recyclable" and "is_low_fat" are true.
Example 2:
Input:
[
{ "product_id": 10, "name": "Cheese", "is_recyclable": false, "is_low_fat": true },
{ "product_id": 11, "name": "Yogurt", "is_recyclable": false, "is_low_fat": true },
{ "product_id": 12, "name": "Water Bottle", "is_recyclable": true, "is_low_fat": false }
]
Output:
[]
Explanation:
No product in this list satisfies both the recyclable and low-fat criteria.
Example 3: (Edge Case: Empty Input)
Input:
[]
Output:
[]
Explanation:
An empty input collection should result in an empty output collection.
Constraints
- The input will be a collection of product objects.
- Each product object will have the following properties:
product_id: An integer.name: A string.is_recyclable: A boolean.is_low_fat: A boolean.
- The number of products in the input collection will be between 0 and 1000, inclusive.
- The length of product names will be between 1 and 50 characters, inclusive.
Notes
- The order of products in the output collection does not matter.
- You will need to iterate through the input collection and check the
is_recyclableandis_low_fatattributes for each product. - Consider how you will build the new collection of filtered products.