3.15 Random Values HW
Popcorn Hack 1: Random SFI Record Number
Code Runner Challenge
Popcorn Hack 1 - Random SFI Record Number
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 1 - Random SFI Record Number
import random
record_count = 4
# Select a random record ID between 1 and record_count (inclusive)
selected_record_id = random.randint(1, record_count)
print("Selected SFI Record ID:", selected_record_id)
print(f"Possible range: 1 through {record_count} inclusive.")
print("Explanation: `random.randint(a, b)` includes BOTH endpoints. Repeated values on consecutive runs are normal because each call performs an independent random selection rather than a sequential rotation.")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 2: Random Structured SFI Car Part
Code Runner Challenge
Popcorn Hack 2 - Random SFI Part Record
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 2 - Random SFI Part Record
import random
parts = [
{
"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"
}
]
# Randomly select one complete dictionary record from the list
selected_part = random.choice(parts)
print("--- Selected SFI Part Record ---")
print("Product Name:", selected_part["product_name"])
print("Category: ", selected_part["category"])
print("Spec Number: ", selected_part["spec_number"])
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Popcorn Hack 3: Random SFI API Test
Code Runner Challenge
Popcorn Hack 3 - Random SFI API Test
View IPYNB Source
# CODE_RUNNER: Popcorn Hack 3 - Random SFI API Test
import random
parts = [
{"product_name": "Replacement Flywheels", "spec_number": "1.1"},
{"product_name": "Multiple Disc Clutch Assemblies", "spec_number": "1.2"},
{"product_name": "Racing Flywheel Record", "spec_number": "2.1"}
]
actions = [
"GET/search",
"POST/create",
"PUT/update"
]
# Randomly select one part and one action
chosen_part = random.choice(parts)
chosen_action = random.choice(actions)
print(f"Action: {chosen_action} | Target Part: '{chosen_part['product_name']}' (Spec {chosen_part['spec_number']})")
# Route behavior based on selected action
if chosen_action == "GET/search":
print(f"--> RESULT: Executing search query for Spec {chosen_part['spec_number']}. Record found.")
elif chosen_action == "POST/create":
print(f"--> RESULT: Validating payload for '{chosen_part['product_name']}' before inserting new record.")
elif chosen_action == "PUT/update":
print(f"--> RESULT: Updating metadata for Spec {chosen_part['spec_number']}.")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
MCQ Answers
- Question 1. C
- Question 2. B
- Question 3. B
- Question 4. B
| MCQ 3.15: 4/4 | answers: C,B,B,B |
Homework Hack: SFI Backend QA Simulator
Code Runner Challenge
Homework Hack - SFI Backend QA Simulator
View IPYNB Source
# CODE_RUNNER: Homework Hack - SFI Backend QA Simulator
import random
parts = [
{
"product_name": "Replacement Flywheels",
"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"
}
]
actions = [
"GET/search",
"POST/create",
"PUT/update",
"DELETE/remove"
]
existing_spec_numbers = ["1.1", "2.1"]
test_count = 5
def run_qa_simulation(parts_list, actions_list, existing_specs, runs):
"""
Simulates automated QA backend testing with random part and action combinations.
Handles duplicate spec logic during POST/create operations.
"""
print(f"=== STARTING SFI BACKEND QA TEST RUN ({runs} Iterations) ===")
for run in range(1, runs + 1):
target_part = random.choice(parts_list)
selected_action = random.choice(actions_list)
spec = target_part["spec_number"]
name = target_part["product_name"]
print(f"\n[Test Run #{run}] Action: {selected_action} | Spec: {spec} | Part: '{name}'")
if selected_action == "GET/search":
print(f" Status 200 OK: Found record '{name}' in database.")
elif selected_action == "POST/create":
# Check duplicate spec logic
if spec in existing_specs:
print(f" Status 409 Conflict: REJECTED creation. Spec number '{spec}' already exists.")
else:
print(f" Status 201 Created: ACCEPTED creation of new record '{name}'.")
elif selected_action == "PUT/update":
print(f" Status 200 OK: Updated record details for spec '{spec}'.")
elif selected_action == "DELETE/remove":
print(f" Status 200 OK: Soft-deleted record '{spec}' from active catalog.")
# Execute the test suite
run_qa_simulation(parts, actions, existing_spec_numbers, test_count)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Lesson: CSP 3.15 Random Values Notebook: https://Rashig-1804.github.io/portfolio/python/random-hw MCQ 3.15: MCQ 3.15: 4/4 (100%) Popcorn Hacks 1–3 completed during class and copied to notebook: yes Homework tested: Ran QA simulator for 5 iterations. Verified random selection across all 4 HTTP methods (GET, POST, PUT, DELETE) and verified 409 Conflict rejection when attempting to POST duplicate spec numbers ‘1.1’ or ‘2.1’.