Popcorn Hack 1 - Check SFI Part Fields

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

# Boolean checks for each requirement
has_product_name = part["product_name"] != ""
valid_category = part["category"] in valid_categories
has_spec_number = part["spec_number"] != ""
has_effective_date = part["effective_date"] != ""

# Print Boolean state of each requirement
print("Has product name:", has_product_name)
print("Is valid category:", valid_category)
print("Has spec number:", has_spec_number)
print("Has effective date:", has_effective_date)

# Overall record completeness check
is_complete = has_product_name and valid_category and has_spec_number and has_effective_date
print("Record is complete:", is_complete)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Backend Create Rule

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

# Boolean checks for validity and uniqueness
has_required_fields = part["product_name"] != "" and part["spec_number"] != "" and part["effective_date"] != ""
is_valid_category = part["category"] in valid_categories
is_duplicate = part["spec_number"] in existing_spec_numbers

# Final backend create decision
can_create_record = has_required_fields and is_valid_category and not is_duplicate

print("Has required fields:", has_required_fields)
print("Is valid category:", is_valid_category)
print("Is spec duplicate:", is_duplicate)
print("Backend decision - Create Record:", can_create_record)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: SFI Search Filter

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

matching_records = []

for record in records:
    # Match query by exact spec number OR product name search
    matches_query = (query == record["spec_number"]) or (query.lower() in record["product_name"].lower())
    
    # Must belong to an accepted category
    is_valid_category = record["category"] in valid_categories
    
    # Combined filter condition using AND
    if matches_query and is_valid_category:
        matching_records.append(record)

print(f"Search results for query '{query}':")
for r in matching_records:
    print(f"- {r['product_name']} (Spec: {r['spec_number']}, Category: {r['category']})")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ Answers

  • Question 1: A (True)
  • Question 2: C (And)
  • Question 3: A (True)
  • Question 4: B

MCQ 3.05: 4/4 (100%)

Homework Hack: SFI Backend Validator

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_sfi_record(record, valid_cats, existing_specs):
    """
    Validates an SFI automotive record using Boolean logic.
    A record is valid IF:
    1. Product name is non-empty
    2. Category is in valid_categories
    3. Spec number is non-empty
    4. Effective date is non-empty
    5. Spec number is NOT already in existing_specs
    """
    has_product_name = bool(record.get("product_name", "").strip())
    is_valid_category = record.get("category") in valid_cats
    has_spec_number = bool(record.get("spec_number", "").strip())
    has_effective_date = bool(record.get("effective_date", "").strip())
    is_not_duplicate = record.get("spec_number") not in existing_specs

    # Combined Boolean evaluation
    is_valid = (
        has_product_name 
        and is_valid_category 
        and has_spec_number 
        and has_effective_date 
        and is_not_duplicate
    )
    
    # Detailed diagnostic output
    reasons = []
    if not has_product_name: reasons.append("Missing product name")
    if not is_valid_category: reasons.append(f"Invalid category '{record.get('category')}'")
    if not has_spec_number: reasons.append("Missing spec number")
    if not has_effective_date: reasons.append("Missing effective date")
    if not is_not_duplicate: reasons.append(f"Duplicate spec number '{record.get('spec_number')}'")
    
    return is_valid, reasons

# Run validation across test dataset
print("=== SFI BACKEND RECORD VALIDATOR RESULTS ===")
for idx, record in enumerate(test_records, 1):
    valid, errors = validate_sfi_record(record, valid_categories, existing_spec_numbers)
    status = "ACCEPTED" if valid else "REJECTED"
    print(f"\nRecord {idx}: '{record['product_name']}'")
    print(f"Status: {status}")
    if not valid:
        print(f"Rejection Reasons: {', '.join(errors)}")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Lesson: CSP 3.05 Boolean Expressions Notebook: https://Rashig-1804.github.io/portfolio/python/boolean-hw MCQ 3.05: 4/4 (100%) Popcorn Hacks 1-3 completed and copied to notebook: yes Homework tested: Evaluated 3 test records — Record 1 ACCEPTED (Valid 1.2 spec), Record 2 REJECTED (Invalid ‘Street Car’ category), Record 3 REJECTED (Duplicate ‘1.1’ spec).