3.05 Boolean Expressions HW
Homework submission for OCS Python 3.05 Booleans
Python 3.05 Boolean Expressions
All the compiled popcorn and homework hacks for boolean expressions are below. Click Run to run the code runners.
Popcorn Hack 1: Check One SFI Part Record
Code Runner Challenge
Popcorn Hack 1 Check One SFI Part Record
View IPYNB Source
%%python
# CODE_RUNNER: Popcorn Hack 1 Check One SFI Part Record
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 evaluate_create_rule(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
can_create_record = (
has_product_name
and valid_category
and has_spec_number
and has_effective_date
and not duplicate_spec
)
print(f"Testing Spec {record['spec_number']} ({record['product_name']}):")
print(" Duplicate Spec:", duplicate_spec)
print(" Can Create Record:", can_create_record)
if can_create_record:
print(" Decision: ACCEPT RECORD (Create in Database)")
else:
print(" Decision: REJECT RECORD (Do Not Create)")
print("--- Case 1: Starter Record (Duplicate Spec 1.1) ---")
evaluate_create_rule(part)
# Valid non-duplicate test case
new_valid_part = {
"product_name": "Bellhousing Safety Shield",
"category": "Drag Racing",
"spec_number": "1.5",
"effective_date": "Sep. 15, 2026"
}
print("\n--- Case 2: New Valid Record (Spec 1.5) ---")
evaluate_create_rule(new_valid_part)
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 Rules, 1 Reject 1 Accept
View IPYNB Source
%%python
# CODE_RUNNER: Popcorn Hack 2 Backend Rules, 1 Reject 1 Accept
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 evaluate_create_rule(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
can_create_record = (
has_product_name
and valid_category
and has_spec_number
and has_effective_date
and not duplicate_spec
)
print(f"Testing Spec {record['spec_number']} ({record['product_name']}):")
print(" Duplicate Spec:", duplicate_spec)
print(" Can Create Record:", can_create_record)
if can_create_record:
print(" Decision: ACCEPT RECORD (Create in Database)")
else:
print(" Decision: REJECT RECORD (Do Not Create)")
print("--- Case 1: Starter Record (Duplicate Spec 1.1) ---")
evaluate_create_rule(part)
# Valid non-duplicate test case
new_valid_part = {
"product_name": "Bellhousing Safety Shield",
"category": "Drag Racing",
"spec_number": "1.5",
"effective_date": "Sep. 15, 2026"
}
print("\n--- Case 2: New Valid Record (Spec 1.5) ---")
evaluate_create_rule(new_valid_part)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 3: Query Filters
Code Runner Challenge
Popcorn Hack 3 Query Filtering
View IPYNB Source
%%python
# CODE_RUNNER: Popcorn Hack 3 Query Filtering
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"
},
{
"product_name": "Street Flywheel Kit",
"category": "Street Car",
"spec_number": "3.1"
}
]
valid_categories = ["Auto Racing", "Drag Racing"]
def search_sfi_records(search_query, record_list):
print(f"=== Search Query: '{search_query}' ===")
matches_found = 0
for rec in record_list:
matches_name = search_query.lower() in rec["product_name"].lower()
matches_spec = rec["spec_number"] == search_query
matches_query = matches_name or matches_spec
accepted_category = rec["category"] in valid_categories
is_valid_match = matches_query and accepted_category
if is_valid_match:
matches_found += 1
print(f" MATCH: {rec['product_name']} | Category: {rec['category']} | Spec: {rec['spec_number']}")
else:
print(f" SKIP: {rec['product_name']} (matches_query={matches_query}, accepted_category={accepted_category})")
print(f"Total Valid Matches: {matches_found}\n")
# Run 1 with default query "1.2"
search_sfi_records(query, records)
# Run 2 with changed query "Flywheel"
search_sfi_records("Flywheel", records)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
In-class 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
%%python
# CODE_RUNNER: Homework Hack - SFI Backend Validator
# 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):
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
is_valid = (
has_product_name
and valid_category
and has_spec_number
and has_effective_date
and not duplicate_spec
)
print(f"Testing: {record['product_name']} (Spec {record['spec_number']})")
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)
print(" Duplicate spec:", duplicate_spec)
if is_valid:
print(" Decision: ACCEPT RECORD\n")
else:
print(" Decision: REJECT RECORD\n")
return is_valid
for i, record in enumerate(test_records, start=1):
print(f"--- Case {i} ---")
validate_sfi_record(record)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...