Skip to content

Instantly share code, notes, and snippets.

@jakemmarsh
Last active January 2, 2016 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakemmarsh/8304502 to your computer and use it in GitHub Desktop.
Save jakemmarsh/8304502 to your computer and use it in GitHub Desktop.
a basic implementation of the common "elevator" interview question.
import time
class Elevator:
def __init__(self):
self.state = "stand"
self.doors = "open"
self.currentFloor = 1
self.requests = []
def requestFloor(self, floorNum):
# only begin moving to floor if currently in 'stand' mode
if(self.state == "stand"):
if(floorNum < self.currentFloor):
self.goDown(floorNum)
elif(floorNum > self.currentFloor):
self.goUp(floorNum)
# if elevator is moving, add to requests to check
elif(self.state == "up" or self.state == "down"):
self.requests.append(floorNum)
def goUp(self, floorNum):
self.closeDoors()
self.state = "up"
# continue going up until floor is reached
while(self.currentFloor < floorNum):
self.currentFloor += 1
# pause for five seconds if a floor we're passing has a request
if(self.currentFloor in self.requests):
self.openDoors()
time.sleep(5)
self.requests.remove(self.currentFloor)
self.closeDoors()
self.state = "stand"
def goDown(self, floorNum):
self.closeDoors()
self.state = "down"
# continue going down until floor is reached
while(self.currentFloor > floorNum):
self.currentFloor -= 1
# pause for five seconds if a floor we're passing has a request
if(self.currentFloor in self.requests):
self.openDoors()
time.sleep(5)
self.closeDoors()
self.requests.remove(self.currentFloor)
self.state = "stand"
def openDoors(self):
self.doors = "open"
def closeDoors(self):
self.doors = "closed"
class Scheduler:
def __init__(self):
self.requestBank = []
# populate elevators
self.elevators = []
for i in range(0, 3):
self.elevators.append(Elevator())
def getStandingOnCurrentFloor(self, floorNum):
for elevator in self.elevators:
if(elevator.state == 'stand' and elevator.currentFloor == floorNum):
return elevator
return None
def getElevatorMovingToCurrentFloor(self, floorNum):
for elevator in self.elevators:
if(elevator.currentFloor > floorNum and elevator.state == 'down'):
return elevator
elif(elevator.currentFloor < floorNum and elevator.state == 'up'):
return elevator
return None
def getAnyStandingElevator(self):
for elevator in self.elevators:
if(elevator.state == 'stand'):
return elevator
return None
def requestElevator(self, floorNum):
# find appropriate elevator
if(self.getStandingOnCurrentFloor()):
elevator = self.getStandingOnCurrentFloor()
elif(self.getElevatorMovingToCurrentFloor()):
elevator = self.getElevatorMovingToCurrentFloor()
elif(self.getAnyStandingElevator()):
elevator = self.getAnyStandingElevator()
# make request
elevator.requestFloor(floorNum)
if(__name__ == "__main__"):
scheduler = Scheduler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment