Array examples

These examples use four fixed on-call slots. The update helper checks the index before replacing a value, so the array’s capacity remains explicit.

Python

import json

def print_label_value(label, value):
    print(f"\033[1;36m{label}:\033[0m {value}")

def json_value(value):
    return json.dumps(value, separators=(",", ":"))

def update_slot(slots, index, value):
    if index < 0 or index >= len(slots):
        raise IndexError("on-call slot is outside the fixed capacity")
    slots[index] = value

on_call_slots = ["Mina", "Owen", "Priya", "Sam"]
print_label_value("1. Initial slots", json_value(on_call_slots))
print_label_value("2. Read slot 3", on_call_slots[2])

update_slot(on_call_slots, 2, "Dana")
print_label_value("3. Replaced slot 3", on_call_slots[2])
print_label_value("4. Capacity", str(len(on_call_slots)))
print_label_value("5. Iterate slots", " | ".join(on_call_slots))
print_label_value(
    "6. Capacity status",
    "full" if all(slot is not None for slot in on_call_slots) else "has open slots",
)

JavaScript

function printLabelValue(label, value) {
  console.log(`\x1b[1;36m${label}:\x1b[0m`, value);
}

function updateSlot(slots, index, value) {
  if (index < 0 || index >= slots.length) {
    throw new RangeError("on-call slot is outside the fixed capacity");
  }
  slots[index] = value;
}

const onCallSlots = new Array(4);
["Mina", "Owen", "Priya", "Sam"].forEach((name, index) => {
  onCallSlots[index] = name;
});

printLabelValue("1. Initial slots", JSON.stringify(onCallSlots));
printLabelValue("2. Read slot 3", onCallSlots[2]);

updateSlot(onCallSlots, 2, "Dana");
printLabelValue("3. Replaced slot 3", onCallSlots[2]);
printLabelValue("4. Capacity", String(onCallSlots.length));
printLabelValue("5. Iterate slots", onCallSlots.join(" | "));
printLabelValue(
  "6. Capacity status",
  onCallSlots.every((slot) => slot !== undefined) ? "full" : "has open slots",
);

Expected output

1. Initial slots: ["Mina","Owen","Priya","Sam"]
2. Read slot 3: Priya
3. Replaced slot 3: Dana
4. Capacity: 4
5. Iterate slots: Mina | Owen | Dana | Sam
6. Capacity status: full