Python 3.05: Boolean Expressions HW
Python 3.05 Boolean Expressions
This notebook covers all three Popcorn Hacks, the four-question MCQ, and the independent SFI backend validator homework.
Popcorn Hack 1: Check One SFI Part Record
The record is checked field by field so each backend requirement produces a visible Boolean result.
Code Runner Challenge
Popcorn Hack 1 - Check SFI Part Fields
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Check SFI Part Fields
# Printing each requirement separately makes it clear which Boolean rule passes or fails.
part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
allowed_categories = ["Auto Racing", "Drag Racing"]
# Each expression answers one backend validation question with True or False.
name_ok = part["product_name"].strip() != ""
category_ok = part["category"] in allowed_categories
spec_ok = part["spec_number"].strip() != ""
date_ok = part["effective_date"].strip() != ""
print("Product name present:", name_ok)
print("Accepted category:", category_ok)
print("Spec number present:", spec_ok)
print("Effective date present:", date_ok)
Popcorn Hack 2: Backend Create Rule
A record can be created only when all required fields are valid and the spec number is not already stored. The first test is rejected as a duplicate; the second changes the spec number and is accepted.
Code Runner Challenge
Popcorn Hack 2 - Backend Create Rule
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Backend Create Rule
# Combining the checks with and/not ensures creation succeeds only when every backend rule is satisfied.
allowed_categories = ["Auto Racing", "Drag Racing"]
stored_specs = ["1.1", "2.1"]
# Put the Boolean rule in a reusable function so multiple cases can be tested.
def create_allowed(part):
name_ok = part["product_name"].strip() != ""
category_ok = part["category"] in allowed_categories
spec_ok = part["spec_number"].strip() != ""
date_ok = part["effective_date"].strip() != ""
duplicate_spec = part["spec_number"] in stored_specs
return name_ok and category_ok and spec_ok and date_ok and not duplicate_spec
duplicate_part = {
"product_name": "Replacement Flywheels and Clutch Assemblies",
"category": "Auto Racing",
"spec_number": "1.1",
"effective_date": "Nov. 9, 2001"
}
new_part = duplicate_part.copy()
new_part["spec_number"] = "1.3"
print("Duplicate case accepted:", create_allowed(duplicate_part))
print("New spec case accepted:", create_allowed(new_part))
Popcorn Hack 3: SFI Search Filter
A record matches when the query equals either its product name or exact spec number, and the record must also belong to an accepted racing category.
Code Runner Challenge
Popcorn Hack 3 - SFI Search Filter
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - SFI Search Filter
# Separating query matching from category validation keeps the compound Boolean rule easier to read and test.
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"}
]
allowed_categories = ["Auto Racing", "Drag Racing"]
# Group the OR search rule, then require an accepted category with AND.
matches = []
for record in records:
query_matches = query == record["product_name"] or query == record["spec_number"]
accepted_category = record["category"] in allowed_categories
if query_matches and accepted_category:
matches.append(record)
print("Matches found:", len(matches))
for record in matches:
print(record["product_name"], "-", record["spec_number"])
MCQ 3.05
Answers: A, C, A, B
Score: 4/4
- A — True:
>=includes equality. - C —
and: both required fields must be valid. - A — True:
notreversesFalse. - B:
oraccepts either racing category.
Homework Hack: SFI Backend Validator
The validator checks every required field, accepted category membership, and duplicate-spec status, then clearly accepts or rejects each supplied test record.
Code Runner Challenge
3.05 Homework - SFI Backend Validator
View IPYNB Source
# CODE_RUNNER: 3.05 Homework - SFI Backend Validator
stored_specs = ["1.1", "2.1"]
allowed_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"}
]
# Return one Boolean that represents every backend requirement together.
def valid_sfi_record(record):
product_ok = record["product_name"].strip() != ""
category_ok = record["category"] in allowed_categories
spec_ok = record["spec_number"].strip() != ""
date_ok = record["effective_date"].strip() != ""
duplicate_spec = record["spec_number"] in stored_specs
return product_ok and category_ok and spec_ok and date_ok and not duplicate_spec
# Test valid, invalid-category, and duplicate cases.
for record in test_records:
if valid_sfi_record(record):
print("ACCEPT:", record["product_name"], "(" + record["spec_number"] + ")")
else:
print("REJECT:", record["product_name"], "(" + record["spec_number"] + ")")
Boolean Logic Explanation
and is used because every required field and rule must pass before a record can be accepted. not reverses the duplicate-spec check so a record is accepted only when its spec number is not already stored, while or is useful when either of multiple alternatives is allowed, such as matching a product name or a spec number.