On this page
Quality Control
TIP101 Unit 2 Session 1 (Click for link to problem statements)
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- What are the inputs?
- A dictionary
product_scoresthat maps each product ID to its quality score, and a numberthreshold.
- A dictionary
- What should the function return?
- A new dictionary that maps every product ID in
product_scoresto either"pass"or"fail".
- A new dictionary that maps every product ID in
- What should my code do if a product’s score is exactly equal to the threshold?
- Mark it
"pass". Only a score below the threshold fails, as the plan and the>= thresholdcheck below show.
- Mark it
- What if
product_scoresis empty?- Return an empty dictionary.
HAPPY CASE
Input: product_scores = {"P101": 92, "P102": 68, "P103": 75}, threshold = 75
Output: {"P101": "pass", "P102": "fail", "P103": "pass"}
Explanation: P102 scores 68, which is below 75, so it fails. P103 scores exactly 75, which meets the threshold, so it passes.
EDGE CASE
Input: product_scores = {}, threshold = 50
Output: {}
Explanation: There are no products to categorize, so the result is an empty dictionary.
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Loop through each product, and add it to a new dictionary with “pass” or “fail” depending on its score.
1) Categorized products dict start empty
2) For each product, check score
a) If below threshold, add product to categorized and map to "fail"
b) Otherwise, add product to categorized and map to "pass"
3) Return the categorized products
I-mplement
def quality_control(product_scores, threshold):
categorized_products = {} # Initialize an empty dictionary
for product_id in product_scores:
if product_scores[product_id] >= threshold:
categorized_products[product_id] = "pass"
else:
categorized_products[product_id] = "fail"
return categorized_products