An elevator controller combines dispatch decisions with safety constraints. The nearest car may be unavailable, and a valid movement command still cannot run while the doors are open. Separate the choice of a car from the allowed transitions of one car.
You implement a small dispatch policy, one directional sweep, boarding capacity and a door-state guard. These functions model bounded parts of a controller; they don't simulate physical equipment or replace a safety system. Stable tie rules make the output repeatable. An exact capacity fit's permitted, repeated stops appear once and an invalid transition leaves the state unchanged. Use those invariants to choose fields and methods before writing the loop that applies commands.
import json
def process_payload(payload: str) -> str:
data = json.loads(payload)
result = []
for floor in data['requests']:
available = [car for car in data['cars'] if car['available']]
chosen = min(available,key=lambda car:(abs(car['floor'] - floor),car['id'])) if available else None
result.append(chosen['id'] if chosen else None)
return json.dumps(result,separators=(',',':'))
Dispatch excludes unavailable cars and resolves equal distances by stable ID. Filter unavailable cars before comparing distances. Equal distances need a stable tie rule. The direct design scans cars per request and deliberately leaves their floors unchanged.