CSP 3.05 Boolean Expressions

Completed all three Popcorn Hacks, the MCQ, and the SFI Backend Validator homework.

Popcorn Hack 1: Check One SFI Part Record

Prediction: all four checks are True. Changing the category to "Street Car" makes only the category check False.

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"]

# Each comparison gives True or False.
print("Product name present:", part["product_name"] != "")
print("Racing category:", part["category"] in valid_categories)
print("Spec number present:", part["spec_number"] != "")
print("Effective date present:", part["effective_date"] != "")

# Changed input: only the category check flips.
part["category"] = "Street Car"
print("Street Car is a racing category:", part["category"] in valid_categories)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Backend Create Rule

Prediction: spec 1.1 is already stored, so it is rejected. Spec 1.5 is new, so it is created.

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"]

for spec in ["1.1", "1.5"]:
    part["spec_number"] = spec
    fields_ok = (part["product_name"] != "" and part["category"] in valid_categories
                 and part["spec_number"] != "" and part["effective_date"] != "")
    duplicate = part["spec_number"] in existing_spec_numbers

    if fields_ok and not duplicate:
        print("Spec", spec, "-> CREATE")
    else:
        print("Spec", spec, "-> REJECT")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: SFI Search Filter

Prediction: "1.2" matches one record by spec number, "flywheel" matches two by name, and "9.9" matches 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"]

# or: match by name OR spec. and: category must also be accepted.
for q in [query, "flywheel", "9.9"]:
    matches = []
    for record in records:
        name_match = q.lower() in record["product_name"].lower()
        spec_match = record["spec_number"] == q
        if (name_match or spec_match) and record["category"] in valid_categories:
            matches.append(record["product_name"])
    print("Query", q, "->", matches)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ 3.05

Answers: A, C, A, B
Score: 4/4

  1. A — True: >= includes equality.
  2. C — and: both required fields must be valid.
  3. A — True: not reverses False.
  4. B: or accepts either racing category.
**MCQ 3.05: 4/4 answers: A,C,A,B**

Homework Hack: SFI Backend Validator

validate_record() checks each requirement and accepts a record only if all of them pass and the spec isn’t a duplicate.

Code Runner Challenge

Homework Hack - SFI Backend Validator

View IPYNB Source
# 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):
    has_name = record["product_name"].strip() != ""
    good_category = record["category"] in valid_categories
    has_spec = record["spec_number"].strip() != ""
    has_date = record["effective_date"].strip() != ""
    duplicate = record["spec_number"] in existing_spec_numbers
    return has_name and good_category and has_spec and has_date and not duplicate

for record in test_records:
    if validate_record(record):
        print("ACCEPTED:", record["product_name"])
    else:
        print("REJECTED:", record["product_name"])
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Boolean Logic Explanation

and is used because every requirement has to be True for a record to be accepted, so one failed check rejects it. not flips the duplicate check, so a record only passes when its spec is not already stored. Results: the first record is accepted, the second is rejected for its Street Car category, and the third is rejected as a duplicate of spec 1.1.