Last active
January 2, 2016 12:39
-
-
Save jakemmarsh/8304502 to your computer and use it in GitHub Desktop.
Revisions
-
jakemmarsh revised this gist
Jan 8, 2014 . 1 changed file with 0 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,10 +46,6 @@ def goDown(self, floorNum): self.requests.remove(self.currentFloor) self.state = "stand" def openDoors(self): self.doors = "open" @@ -96,14 +92,6 @@ def requestElevator(self, floorNum): # make request elevator.requestFloor(floorNum) if(__name__ == "__main__"): scheduler = Scheduler() -
jakemmarsh revised this gist
Jan 7, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,7 +48,7 @@ def goDown(self, floorNum): def alarm(self): self.state = "stand" self.openDoors() def openDoors(self): self.doors = "open" -
jakemmarsh created this gist
Jan 7, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ 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 alarm(self): self.state = "stand" self.doors = "open" 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) def alarmElevator(self, elevator): # alarm elevator elevator.alarm() # move any requests the elevator had back to global request bank self.requestBank = self.requestBank + elevator.requests elevator.requests = [] if(__name__ == "__main__"): scheduler = Scheduler()