3.05 Boolean Expressions HW
3.05 Boolean Expressions: Homework Notebook
Lesson: CSP 3.05 Boolean Expressions (SFI Safety Tech Inspector) Contents: Popcorn Hacks 1-3 (with predictions + output), MCQ result, Homework Hack (SFI Backend Validator), College Board pseudocode check, submission notes.
Popcorn Hack 1: Check One SFI Part Record
Prediction (before running): The record has a non-empty product name, category "Auto Racing" (which is in the accepted list), spec number "1.1", and an effective date. Every individual check should be True, so the combined “record complete” result should also be True.
Code Runner Challenge
Popcorn Hack 1 - Check SFI Part Fields
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Check SFI Part Fields
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"]
# One small Boolean check per requirement
has_product_name = part["product_name"] != ""
valid_category = part["category"] in valid_categories # same idea as: == "Auto Racing" or == "Drag Racing"
has_spec_number = part["spec_number"] != ""
has_effective_date = part["effective_date"] != ""
print("Has product name: ", has_product_name)
print("Valid category: ", valid_category)
print("Has spec number: ", has_spec_number)
print("Has effective date:", has_effective_date)
# All four requirements must pass -> and
record_complete = has_product_name and valid_category and has_spec_number and has_effective_date
print("Record complete: ", record_complete)
# Change one input and compare: a Street Car category is not accepted
part["category"] = "Street Car"
print()
print("After changing category to 'Street Car':")
print("Valid category: ", part["category"] in valid_categories)
print("Record complete: ", part["product_name"] != "" and part["category"] in valid_categories
and part["spec_number"] != "" and part["effective_date"] != "")
What I learned: and needs every check to pass, so flipping a single input (the category) flips the whole result to False. in is a compact way to write the or chain category == "Auto Racing" or category == "Drag Racing".
Popcorn Hack 2: Backend Create Rule
Prediction (before running): The record’s spec number "1.1" is already in existing_spec_numbers, so not duplicate_spec is False and the create decision should be rejected. When I change the spec number to "1.3" (new), every check passes and it should be accepted.
Code Runner Challenge
Popcorn Hack 2 - Backend Create Rule
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Backend Create Rule
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 can_create(record):
has_product_name = record["product_name"] != ""
valid_category = record["category"] in valid_categories
has_spec_number = record["spec_number"] != ""
has_effective_date = record["effective_date"] != ""
duplicate_spec = record["spec_number"] in existing_spec_numbers
# all required fields valid AND the spec number is not already stored
return (has_product_name and valid_category and has_spec_number
and has_effective_date and not duplicate_spec)
# Case 1: rejected (duplicate spec number 1.1)
print("Case 1 (spec 1.1, already exists):", "CREATE" if can_create(part) else "REJECT")
# Case 2: accepted (new spec number)
part["spec_number"] = "1.3"
print("Case 2 (spec 1.3, brand new): ", "CREATE" if can_create(part) else "REJECT")
What I learned: not flips the duplicate flag, so a duplicate spec number makes the and chain False even when every other field is perfect.
Popcorn Hack 3: SFI Search Filter
Prediction (before running): Query "1.2" should match only the Multiple Disc Clutch Assemblies record (exact spec number match, Drag Racing is accepted). Query "flywheel" should match the two records whose product names contain “flywheel”. Query "3.9" should match nothing.
Code Runner Challenge
Popcorn Hack 3 - SFI Search Filter
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - SFI Search Filter
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(q, data):
results = []
for record in data:
name_match = q.lower() in record["product_name"].lower() # matches the product name
spec_match = q == record["spec_number"] # exact spec number
category_ok = record["category"] in valid_categories
# (name OR spec) AND an accepted category
if (name_match or spec_match) and category_ok:
results.append(record)
return results
for q in [query, "flywheel", "3.9"]:
found = search(q, records)
print(f"Query {q!r}: {len(found)} match(es)")
for r in found:
print(" ->", r["spec_number"], r["product_name"], f"[{r['category']}]")
What I learned: The parentheses matter. (name_match or spec_match) and category_ok means either kind of match is fine, but the category must always be valid. Without the parentheses, and would bind tighter than or and give wrong results.
In-class MCQ Check
| # | Question | My answer | Why |
|---|---|---|---|
| 1 | What does 7 >= 7 evaluate to? |
A (True) |
>= includes equality. |
| 2 | Name AND spec number required: which operator? | C (and) |
Both requirements must be true. |
| 3 | duplicate_spec is False; what is not duplicate_spec? |
A (True) |
not reverses the value. |
| 4 | Which expression accepts Auto Racing or Drag Racing? | B (category == 'Auto Racing' or category == 'Drag Racing') |
Only one comparison has to be true. |
Code Runner Challenge
Homework Hack - SFI Backend Validator
View IPYNB Source
MCQ 3.05: 4/4 | answers: A,C,A,B
Homework Hack: SFI Backend Validator
A reusable validator for candidate SFI automotive records. A record is valid only if it has a non-empty product name, an accepted racing category, a non-empty spec number, an effective date, and a spec number that does not already exist in the backend.
# 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, existing, categories):
"""Return (is_valid, reasons). is_valid is a Boolean; reasons lists every failed rule."""
has_product_name = record.get("product_name", "") != ""
valid_category = record.get("category", "") in categories
has_spec_number = record.get("spec_number", "") != ""
has_effective_date = record.get("effective_date", "") != ""
duplicate_spec = record.get("spec_number", "") in existing
# Every rule must pass (and) and the spec must not be a duplicate (not)
is_valid = (
has_product_name
and valid_category
and has_spec_number
and has_effective_date
and not duplicate_spec
)
reasons = []
if not has_product_name:
reasons.append("missing product name")
if not valid_category:
reasons.append("category not accepted")
if not has_spec_number:
reasons.append("missing spec number")
if not has_effective_date:
reasons.append("missing effective date")
if duplicate_spec:
reasons.append("duplicate spec number")
return is_valid, reasons
def run_validator(records):
for record in records:
ok, reasons = validate_record(record, existing_spec_numbers, valid_categories)
decision = "ACCEPT RECORD" if ok else "REJECT RECORD"
detail = "" if ok else " <- " + ", ".join(reasons)
print(f"{record['spec_number'] or '(none)':>6} | {record['product_name'] or '(none)':<34} | {decision}{detail}")
print("=== Supplied test records ===")
run_validator(test_records)
# Extra edge cases: empty strings and a record that breaks several rules at once
edge_cases = [
{"product_name": "", "category": "Auto Racing", "spec_number": "4.1", "effective_date": "Jan. 1, 2026"},
{"product_name": "Seat Belt Assembly", "category": "Drag Racing", "spec_number": "4.2", "effective_date": ""},
{"product_name": "Bad Record", "category": "Street Car", "spec_number": "2.1", "effective_date": ""},
]
print()
print("=== Edge cases ===")
run_validator(edge_cases)
Explanation of the Boolean logic
andjoins the four “required field” checks plus the duplicate check, because all requirements must be satisfied. OneFalsemakes the whole record invalid.not duplicate_specreverses the duplicate flag: a spec number that already exists isTrueforduplicate_spec, which must becomeFalsefor the record to pass.in valid_categoriesis the compact form ofcategory == "Auto Racing" or category == "Drag Racing": only one accepted category has to match.- Test results: record 1 (spec 1.2, Auto Racing) is accepted; record 2 fails because
"Street Car"is not an accepted category; record 3 fails because spec"1.1"already exists. The edge cases exercise the empty-string checks and multiple failures at once.
College Board Pseudocode Check
For the first test record (specNumber = "1.2", category = "Auto Racing"), tracing the exam-style pseudocode by hand:
hasProductName ← (productName ≠ "") // TRUE
validCategory ← (category = "Auto Racing") OR (category = "Drag Racing") // TRUE OR FALSE = TRUE
hasSpecNumber ← (specNumber ≠ "") // TRUE
hasEffectiveDate ← (effectiveDate ≠ "") // TRUE
duplicateSpec ← (specNumber = "1.1") OR (specNumber = "2.1") // FALSE OR FALSE = FALSE
isValid ← TRUE AND TRUE AND TRUE AND TRUE AND NOT FALSE // TRUE
IF (isValid) { DISPLAY("ACCEPT RECORD") }
Prediction: ACCEPT RECORD, which matches the Python validator’s output for the first record. Python uses == for comparison where College Board pseudocode uses =, but the decision is identical.
Submission Notes (for the Notes field)
Lesson: CSP 3.05 Boolean Expressions
Notebook: <published URL>
MCQ 3.05: 4/4 | answers: A,C,A,B
Popcorn Hacks 1-3 completed during class and copied to notebook: yes
Homework tested: 3 supplied records (accept / reject category / reject duplicate) + 3 edge cases (empty name, empty date, multiple failures); all matched expected output
Submit the Homework Hack code cell above (without Markdown fences) in the Code Submission form.