3.15 Random Values HW
3.15 Random Values — Homework (SFI Backend QA Test Garage)
Lesson: https://pages.opencodingsociety.com/python/random
Theme: QA engineer stress-testing an SFI automotive safety-spec backend using random.randint(a, b) (inclusive) and random.choice(sequence).
College Board connection: RANDOM(a, b) → random.randint(a, b), both inclusive. RANDOM(1, 4) possible outputs: 1, 2, 3, 4.
Popcorn Hack 1: Check One SFI Part Record
Task (from https://pages.opencodingsociety.com/python/boolean/2027): An SFI automotive record is complete when it has a product name, a racing category, a spec number, and an effective date. For this challenge, Auto Racing and Drag Racing are valid racing categories. Report the Boolean state of each requirement for the record below.
Starter:
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"]
Prediction: First run prints all True — name is non-empty, Auto Racing is in the valid list, spec and date are non-empty. Changing category to Street Car flips only valid_category to False.
Reasoning rule: Comparisons create Booleans (!=, in, ==). in valid_categories is the Python form of (category = "Auto Racing") OR (category = "Drag Racing").
Code Runner Challenge
Popcorn Hack 1 — Check One SFI Part Record. Run the code, then change the category to Street Car and run again to see valid_category flip to False.
Output recorded: Has product name: True, Valid category: True, Has spec number: True, Has effective date: True; changed-input run Street Car -> valid: False.
Explanation: All four checks are True for the starter record, so it is complete. valid_category uses in ["Auto Racing", "Drag Racing"], matching the lesson’s OR rule — either accepted value passes, anything else (Street Car) is False. This is the field-level Boolean check the Homework validator reuses with and / not.
Popcorn Hack 2: Backend Create Rule
Task (from https://pages.opencodingsociety.com/python/boolean/2027): The backend receives the same style of SFI part record. A new database record should only be created when all required fields are valid and its spec number is not already stored. Build the final create decision and test it with at least one valid case and one rejected case.
Prediction: Starter record (spec 1.1, already in ["1.1", "2.1"]) prints REJECT even though its fields are valid, because NOT duplicate fails. A second record with new spec 1.2 prints CREATE.
Reasoning rule: All requirements must pass -> and. Reject a duplicate -> not duplicate_spec. Tech Talk 2 demo: has_product_name and has_spec_number, or for either-option, not to reverse.
Code Runner Challenge
Popcorn Hack 2 — Backend Create Rule. Run both cases: spec 1.1 should REJECT (duplicate) and spec 1.2 should CREATE.
Output recorded: Starter (spec 1.1) can_create: False -> REJECT; New part (spec 1.2) can_create: True -> CREATE.
Explanation: Starter fails only on duplicate_spec = True, so ... and (not True) = False. The 1.2 record passes all five checks, so and-chain is True. Both branches of the if/else were exercised, as the instructions require (one valid, one rejected).
Popcorn Hack 3: SFI Search Filter
Task (from https://pages.opencodingsociety.com/python/boolean/2027): An SFI backend search can match a record through either its product name or its exact spec number. Search results should only include records from accepted racing categories. Use the provided records and query to return only valid matches. Logic must still work when the query or record data changes.
Predictions: Query "1.2" matches exactly one record (Multiple Disc Clutch Assemblies, Drag Racing). Query "Flywheel" matches by name but only in accepted categories. Changing the query changes the result without changing the code.
Reasoning rule: Either match passes -> or for (query in name) OR (query == spec). Category gate -> and valid_category. Combines to (name_match or spec_match) and valid_category.
Code Runner Challenge
Popcorn Hack 3 — SFI Search Filter. Run the code, then change the query to "Flywheel" and run again to see the filter adapt.
Output recorded: Query "1.2" -> 1 match (Multiple Disc Clutch Assemblies | Drag Racing | 1.2); Query "Flywheel" -> 2 matches (both in accepted categories).
Explanation: (name_match or spec_match) and valid_category is the whole filter. "1.2" hits only via spec_match; "Flywheel" hits via name_match. A Street Car record with the same name would still be excluded by the and valid_category gate, which is why the category check must stay inside the condition.
In-class MCQ Check
I’ve added the text asked for me to add in the description. Here it is just incase:
MCQ 3.05: 4/4 | answers: A,C,A,B
Homework Hack (independent): SFI Backend Validator
Starter code (use as-is, build below it): existing_spec_numbers, valid_categories, test_records with 3 candidate records.
Reference: https://pages.opencodingsociety.com/python/boolean/2027 — Boolean expressions (==, !=, in, and, or, not).
Valid record rule (all must pass):
- non-empty
product_nameAND categoryinvalid_categories(Auto RacingORDrag Racing) AND- non-empty
spec_numberAND - non-empty
effective_dateAND - NOT duplicate (
spec_numbernot inexisting_spec_numbers)
Random Values connection (3.15): after the deterministic check of all 3 starter records, use random.choice(test_records) for 5 randomized QA draws and random.randint(1, 4) to show inclusive RANDOM(a,b) range. Same validator, random test driver.
Code Runner Challenge
Homework Hack — SFI Backend Validator. Run the deterministic 3-record check and 5 randomized QA draws using random.choice and random.randint.
Homework validation tests
Seeded repeatability check + Boolean-logic assertions + randint range check. Expected: record 1.2 ACCEPTs, 3.1 REJECTs (bad category), 1.1 REJECTs (duplicate).
Code Runner Challenge
Homework Validation — Boolean assertions and random-values checks. This runner is self-contained: the validator setup is included so it runs on its own.
College Board pseudocode trace
Same rule as https://pages.opencodingsociety.com/python/boolean/2027 Section 3:
hasProductName <- (productName != "")
validCategory <- (category = "Auto Racing") OR (category = "Drag Racing")
hasSpecNumber <- (specNumber != "")
hasEffectiveDate <- (effectiveDate != "")
duplicateSpec <- (specNumber = "1.1") OR (specNumber = "2.1")
isValid <- hasProductName AND validCategory AND hasSpecNumber AND hasEffectiveDate AND NOT duplicateSpec
Trace: 1.2 / Auto Racing / new -> T AND T AND T AND T AND NOT F = T -> ACCEPT. 3.1 / Street Car -> validCategory = F -> F -> REJECT. 1.1 / Drag Racing / duplicate -> NOT T = F -> REJECT. Python random.randint(0,2) indexes test_records, random.choice(test_records) picks one intact record — same decisions as pseudocode.
Notes for submission: Lesson: CSP 3.15 Random Values | MCQ 3.15: 4/4 | Popcorn Hacks 1-3 completed and copied to notebook: yes | Homework tested: deterministic 3-record validator (ACCEPT/REJECT) + 5 randomized draws via random.choice + 200-draw randint range check.