3.05 Boolean Expressions HW
Code Runner Challenge
popcorn 1
View IPYNB Source
# CODE_RUNNER: popcorn 1
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
# Build and test your solution
def is_valid_category(part_data, allowed_categories):
# Check if the category key exists and is in the allowed list
return part_data.get("category") in allowed_categories
# Run the test
result = is_valid_category(part, valid_categories)
print(f"Category is valid: {result}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
popcorn 2
View IPYNB Source
# CODE_RUNNER: popcorn 2
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
valid_categories = ["Auto Racing", "Drag Racing"]
existing_spec_numbers = ["1.1", "2.1"]
def validate_part(part_data, allowed_categories, valid_specs):
errors = []
# Validate category
if part_data.get("category") not in allowed_categories:
errors.append(f"Invalid category: '{part_data.get('category')}'")
# Validate spec number
if part_data.get("spec_number") not in valid_specs:
errors.append(f"Invalid spec number: '{part_data.get('spec_number')}'")
return {
"is_valid": len(errors) == 0,
"errors": errors
}
# Run and test the solution
result = validate_part(part, valid_categories, existing_spec_numbers)
print(result)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
popcorn 3
View IPYNB Source
# CODE_RUNNER: popcorn 3
query = "1.2"
records = [
{
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1"
},
{
"product_name": "Multiple Disc Clutch Assemblies",
"category": "Drag Racing",
"spec_number": "1.2"
},
{
"product_name": "Racing Flywheel Record",
"category": "Auto Racing",
"spec_number": "2.1"
}
]
valid_categories = ["Auto Racing", "Drag Racing"]
def search_records(query_str, record_list):
# Filter records where the spec_number matches the query string
matching_records = [
record for record in record_list
if record.get("spec_number") == query_str and record.get("category") in valid_categories
]
return matching_records
# Run and test the solution
results = search_records(query, records)
print("Matching Records:")
for match in results:
print(match)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
MCQ
answers: D,B,D,C
Code Runner Challenge
hw
View IPYNB Source
# CODE_RUNNER: hw
existing_spec_numbers = ["1.1", "2.1"]
valid_categories = ["Auto Racing", "Drag Racing"]
test_records = [
{
"product_name": "Multiple Disc Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.2",
"effective_date": "Feb. 9, 2006"
},
{
"product_name": "Replacement Flywheels",
"category": "Street Car",
"spec_number": "3.1",
"effective_date": "Jan. 1, 2026"
},
{
"product_name": "Existing Flywheel Record",
"category": "Drag Racing",
"spec_number": "1.1",
"effective_date": "Jan. 1, 2026"
}
]
def validate_record(record, valid_cats, existing_specs):
errors = []
# 1. Non-empty product name
if not record.get("product_name", "").strip():
errors.append("Product name is empty or missing.")
# 2. Accepted racing category
category = record.get("category")
if category not in valid_cats:
errors.append(f"Invalid category: '{category}'")
# 3 & 5. Non-empty spec number AND does not already exist in backend
spec_number = record.get("spec_number")
if not spec_number or not str(spec_number).strip():
errors.append("Spec number is empty or missing.")
elif spec_number in existing_specs:
errors.append(f"Spec number '{spec_number}' already exists in the backend.")
# 4. Effective date present
if not record.get("effective_date", "").strip():
errors.append("Effective date is empty or missing.")
# Return validation status (True if no errors) and error list
return len(errors) == 0, errors
# --- Runner & Test Execution ---
print("=== SFI Backend Validation Results ===\n")
for i, record in enumerate(test_records, 1):
is_accepted, error_list = validate_record(record, valid_categories, existing_spec_numbers)
status = "ACCEPTED"
print(f"Record {i}: {record.get('product_name', 'Unknown Product')}")
if is_accepted:
print(f"Result: {status}\n")
else:
print(f"Result: REJECTED")
print("Reasons:")
for error in error_list:
print(f" - {error}")
print()
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...