CSP 3.15 Random Values

Completed all three Popcorn Hacks, the MCQ, and the SFI Backend QA Simulator homework. Random output changes each run.

Popcorn Hack 1: Random SFI Record Number

Prediction: each run prints 1, 2, 3 or 4, since randint includes both ends. Repeats are normal because every draw is independent.

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

for run in range(5):
    record_number = random.randint(1, record_count)
    print("Record", record_number)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Random Structured SFI Car Part

Prediction: each run shows one whole record. random.choice returns the full dictionary, so its fields always match.

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

part = random.choice(parts)
print("Product:", part["product_name"])
print("Category:", part["category"])
print("Spec number:", part["spec_number"])
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Random SFI API Test

Prediction: two possible outcomes are GET/search on spec 1.2 and PUT/update on spec 2.1. There are 3 × 3 = 9 possible part/action pairs.

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

part = random.choice(parts)
action = random.choice(actions)

if action == "GET/search":
    print(action, "-> found", part["product_name"])
elif action == "POST/create":
    print(action, "-> check spec", part["spec_number"], "before creating")
else:
    print(action, "-> updated spec", part["spec_number"])
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ 3.15

Answers: C, B, B, B
Score: 4/4

  1. C: randint(2, 5) can return 2, 3, 4, 5.
  2. B: independent random selections are allowed to repeat.
  3. B: random.randint(1, 4) matches inclusive RANDOM(1, 4).
  4. B: one run may show only one of many possible outcomes.
**MCQ 3.15: 4/4 answers: C,B,B,B**

Homework Hack: SFI Backend QA Simulator

Each run picks a random part and action, and handle() returns a backend-style result. Creating a spec adds it to the stored list, so creating it again is rejected as a duplicate.

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 handle(part, action, specs):
    spec = part["spec_number"]
    if action == "GET/search":
        if spec in specs:
            return "found " + part["product_name"]
        return "spec " + spec + " not found"
    elif action == "POST/create":
        if spec in specs:
            return "duplicate spec " + spec + " rejected"
        specs.append(spec)
        return "created spec " + spec
    elif action == "PUT/update":
        if spec in specs:
            return "updated spec " + spec
        return "cannot update missing spec " + spec
    else:
        if spec in specs:
            specs.remove(spec)
            return "removed spec " + spec
        return "cannot remove missing spec " + spec

specs = list(existing_spec_numbers)
for run in range(1, test_count + 1):
    part = random.choice(parts)
    action = random.choice(actions)
    print("Run", run, "| spec", part["spec_number"], "|", action, "->", handle(part, action, specs))

# Duplicate test: the same new spec created twice.
specs = list(existing_spec_numbers)
print(handle(parts[1], "POST/create", specs))
print(handle(parts[1], "POST/create", specs))
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Possible Results

There are 3 parts × 4 actions = 12 possible pairs, and a pair can repeat between runs. Creating spec 1.1 or 2.1 is always rejected as a duplicate. Spec 1.2 is created the first time and rejected the second time, as the duplicate test shows.