3.05 Boolean Expressions HW
Popcorn Hack 1
Prediction: The code will check all four requirements for the record. Since all fields are filled with data and the category is ‘Auto Racing’, all Boolean checks should output 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"]
# Build and test your solution below.
has_product = part["product_name"] != ""
valid_cat = part["category"] in valid_categories
has_spec = part["spec_number"] != ""
has_date = part["effective_date"] != ""
print("Has product name:", has_product)
print("Has valid category:", valid_cat)
print("Has spec number:", has_spec)
print("Has effective date:", has_date)
Actual output (Valid Case):
Has product name: TrueHas valid category: TrueHas spec number: TrueHas effective date: True
Changed Input Test: I changed the category in the dictionary to “Street Car” to test an invalid case.
Actual output (Invalid Case):
Has product name: TrueHas valid category: FalseHas spec number: TrueHas effective date: True
Popcorn Hack 2
Prediction: The first run should output False because the spec_number “1.1” is already in the existing_spec_numbers list (it’s a duplicate). When I change the spec number to a new one, it should output True.
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"]
# Build and test your solution below.
has_required = part["product_name"] != "" and part["spec_number"] != "" and part["effective_date"] != ""
valid_cat = part["category"] in valid_categories
duplicate_spec = part["spec_number"] in existing_spec_numbers
create_record = has_required and valid_cat and not duplicate_spec
print("Create record (1.1):", create_record)
# Test the opposite case by changing the spec number
part["spec_number"] = "1.3"
duplicate_spec_test2 = part["spec_number"] in existing_spec_numbers
create_record_test2 = has_required and valid_cat and not duplicate_spec_test2
print("Create record (1.3):", create_record_test2)
Actual output:
Create record (1.1): FalseCreate record (1.3): True
Explanation: The first run correctly rejected the creation because duplicate_spec was True, and the rule requires not duplicate_spec. Changing the spec to 1.3 made it unique, so all conditions became True.
Popcorn Hack 3
Prediction: The code will search for “1.2”. The first record (1.1) will be False. The second record (1.2, Drag Racing) will be True because it matches the query and has a valid category. The third record (2.1) will be False.
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"]
# Build and test your solution below.
for record in records:
match_query = (record["product_name"] == query) or (record["spec_number"] == query)
valid_cat = record["category"] in valid_categories
is_match = match_query and valid_cat
print("Match for spec", record["spec_number"], ":", is_match)
Actual output:
Match for spec 1.1 : FalseMatch for spec 1.2 : TrueMatch for spec 2.1 : False
Explanation: The filter successfully isolated the exact match for “1.2” while ensuring the category was also accepted.
MCQ Check
MCQ 3.05: 4/4 | answers: A,C,A,B
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"
}
]
# Build and test your validator below.
def validate_record(record, existing_specs, categories):
has_name = record["product_name"] != ""
valid_cat = record["category"] in categories
has_spec = record["spec_number"] != ""
has_date = record["effective_date"] != ""
duplicate_spec = record["spec_number"] in existing_specs
# Every requirement must pass (and), and the spec must NOT be a duplicate (not)
return has_name and valid_cat and has_spec and has_date and not duplicate_spec
for record in test_records:
if validate_record(record, existing_spec_numbers, valid_categories):
print("ACCEPT RECORD:", record["product_name"])
else:
print("REJECT RECORD:", record["product_name"])
Homework output:
ACCEPT RECORD: Multiple Disc Clutch AssembliesREJECT RECORD: Replacement FlywheelsREJECT RECORD: Existing Flywheel Record
Homework tested: The reusable validator correctly iterated through the 3 cases.
- Accepted because it meets all criteria and spec 1.2 is new.
- Rejected because “Street Car” is not in valid_categories.
- Rejected because spec “1.1” is already in existing_spec_numbers (duplicate).
Boolean logic: validate_record() builds one small Boolean per rule, then combines them with and, because all requirements must pass. A single False (like valid_cat for “Street Car”) makes the whole result False. The duplicate rule uses not duplicate_spec, so a spec already in existing_spec_numbers flips to False and rejects the record. Because it’s a procedure with parameters, the same validator can be reused on any record or spec list.