Unit 4 Session 1 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
Q: What is the structure of the input?
"name"
key and a "criteria"
key containing a list of strings.Q: What is the output?
Q: What should the function return if no brands meet the criterion?
Q: Are there any constraints on the input, such as the presence of the "criteria"
key in each dictionary?
"name"
key and a "criteria"
key with corresponding values.Plan the solution with appropriate visualizations and pseudocode.
General Idea: Iterate through the list of brands, checking each brand's criteria to see if it includes the specified criterion. If it does, add the brand's name to the list of sustainable brands.
1) Initialize an empty list called `sustainable_brands`.
2) For each `brand` in `brands`:
a) If the `criterion` is in the `brand["criteria"]`, append the `brand["name"]` to `sustainable_brands`.
3) Return the `sustainable_brands` list.
**⚠️ Common Mistakes**
- Forgetting to handle cases where the list of brands is empty.
- Not correctly checking the presence of the criterion in the criteria list.
def filter_sustainable_brands(brands, criterion):
sustainable_brands = []
for brand in brands:
if criterion in brand["criteria"]:
sustainable_brands.append(brand["name"])
return sustainable_brands