3.05 Boolean Expressions HW
Boolean expressions Hw
3.05 Boolean Expressions Homework: SFI Backend Validator
Build a reusable validator for candidate SFI automotive records.
A valid record has
- a non-empty product name
- an accepted racing category (
Auto RacingorDrag Racing) - a non-empty spec number
- an effective date
- a spec number that does not already exist in the backend
Requirements
- Use test data with
existing_spec_numbers = ["1.1", "2.1"]andvalid_categories = ["Auto Racing", "Drag Racing"] - Test all three provided records (one valid, one bad category, one duplicate spec)
- Clearly print whether each record is accepted or rejected
- Explain the Boolean logic (
and,not, relational operators) - Keep the code self-contained so it runs without earlier lesson cells
Grading
Homework Validator: 0.40 of the 1.0 total (Popcorn Hacks 0.45 and MCQ 0.15 are graded separately).
Code Runner Challenge
Homework Hack - SFI Backend Validator
View IPYNB Source
%%python
# CODE_RUNNER: Homework Hack - SFI Backend Validator
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):
# Each rule becomes a small named Boolean so the final rule is easy to read.
# != compares values and returns True/False (not assignment like =).
has_product_name = record["product_name"].strip() != ""
has_spec_number = record["spec_number"].strip() != ""
has_effective_date = record["effective_date"].strip() != ""
# 'in' checks list membership and gives a Boolean
valid_category = record["category"] in valid_categories
# 'not' reverses a Boolean: a record is only new if its spec is NOT already stored
duplicate_spec = record["spec_number"] in existing_spec_numbers
# 'and' means EVERY requirement must be True for the record to be accepted
return (
has_product_name
and valid_category
and has_spec_number
and has_effective_date
and not duplicate_spec
)
for record in test_records:
accepted = validate_record(record)
print(record["product_name"], "(spec", record["spec_number"] + "):",
"ACCEPTED" if accepted else "REJECTED")
if not accepted:
# Explain which rule failed so the Boolean logic is visible
if record["category"] not in valid_categories:
print(" Reason: category is not an accepted racing category")
if record["spec_number"] in existing_spec_numbers:
print(" Reason: spec number already exists (duplicate)")
# Expected: record 1 ACCEPTED, record 2 REJECTED (bad category), record 3 REJECTED (duplicate)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...