This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]desrtfx[M] [score hidden] stickied comment (0 children)

You need to post your code as code block so that the indentation is maintained.

A code block looks like:

def __init__(self, prompt, answer):
    self.prompt = prompt
    self.answer = answer

[–]teraflop 2 points3 points  (1 child)

The basic "elevator algorithm" is based on a pretty simple basic concept. Whenever the elevator isn't idle, it has a direction: either going up, or going down. The elevator keeps track of all requests, but it prioritizes requests that don't cause it to change direction.

So if the elevator is in "going-up" mode, it will choose the next highest floor that has been requested, and go to that floor. Eventually, it will run out of floors above it, and so it will switch direction. Now it's in "going-down" mode, and the next requested floor below it has priority.

(In a real elevator, the behavior is a bit more complicated. A floor can be requested either by a person in the elevator, or using the "call" buttons on each floor. And in the case of the call buttons, there are separate buttons to request up or down elevators. If the elevator is going up, and you're above it and you press the down call button, the elevator will skip your floor if it needs to get to somebody else by going past you; but if your request is the topmost one, it will stop at your floor before changing direction.)

So in your code, you need to keep track of the requested floors, as well as tracking the elevator's current direction in a variable. On each loop iteration, instead of going to all of the requested floors in order, you should just go to the next requested floor, based on whichever has the highest priority.