3.05 Boolean Expressions Homework

POPCORN HACK 1

Code Runner Challenge

POPCORN HACK 1

View IPYNB Source
# CODE_RUNNER: POPCORN HACK 1

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

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("Has product name:", has_product_name)
print("Accepted category:", valid_category)
print("Has spec number:", has_spec_number)
print("Has effective date:", has_effective_date)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

POPCORN HACK 2

Code Runner Challenge

POPCORN HACK 2

View IPYNB Source
# CODE_RUNNER: POPCORN HACK 2

valid_categories = ["Auto Racing", "Drag Racing"]
existing_spec_numbers = ["1.1", "2.1"]

part = {
    "product_name": "Replacement Flywheels and Clutch Assemblies",
    "category": "Auto Racing",
    "spec_number": "1.1",
    "effective_date": "Nov. 9, 2001"
}

has_required_fields = (
    part["product_name"] != ""
    and part["spec_number"] != ""
    and part["effective_date"] != ""
)
valid_category = part["category"] in valid_categories
duplicate_spec = part["spec_number"] in existing_spec_numbers
can_create_record = has_required_fields and valid_category and not duplicate_spec

print("Can create original record:", can_create_record)

part["spec_number"] = "1.3"
duplicate_spec = part["spec_number"] in existing_spec_numbers
can_create_record = has_required_fields and valid_category and not duplicate_spec

print("Can create changed record:", can_create_record)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

POPCORN HACK 3

Code Runner Challenge

POPCORN HACK 3

View IPYNB Source
# CODE_RUNNER: POPCORN HACK 3

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

for record in records:
    query_matches = query.lower() in record["product_name"].lower() or query == record["spec_number"]
    category_is_valid = record["category"] in valid_categories
    include_record = query_matches and category_is_valid

    if include_record:
        print(record["product_name"], "-", record["spec_number"])

print("Search complete")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Mixed Expression Prediction Practice

Code Runner Challenge

3.05 Mixed Boolean Prediction Practice

View IPYNB Source
# CODE_RUNNER: 3.05 Mixed Boolean Prediction Practice

has_product_name = True
valid_category = False
has_spec_number = True
duplicate_spec = False

# Predictions before running:
# 1. has_product_name and valid_category -> False
# 2. has_spec_number and not duplicate_spec -> True
# 3. valid_category or (has_spec_number and not duplicate_spec) -> True
# 4. not (valid_category or duplicate_spec) -> True

print(has_product_name and valid_category)
print(has_spec_number and not duplicate_spec)
print(valid_category or (has_spec_number and not duplicate_spec))
print(not (valid_category or duplicate_spec))
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ Check

HOMEWORK HACK

Code Runner Challenge

HOMEWORK HACK

View IPYNB Source
# CODE_RUNNER: HOMEWORK HACK

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"
    },
    {
        "product_name": "",
        "category": "Drag Racing",
        "spec_number": "4.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
    )

    return is_valid

for record in test_records:
    if validate_sfi_record(record):
        print("ACCEPT:", record["spec_number"], record["product_name"])
    else:
        print("REJECT:", record["spec_number"], record["product_name"])

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...