Last active
October 4, 2019 09:26
-
-
Save funkyHat/c100ef3fa99ac11933217ab9fa10fe70 to your computer and use it in GitHub Desktop.
This file contains 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 characters
import time | |
import random | |
def main(): | |
lift = Lift(10) | |
while True: | |
lift.move() | |
time.sleep(0.3) | |
if random.randrange(3) == 1: | |
floor = random.randrange(10) | |
lift.go_to_floor(floor) | |
print(" called to", floor, end="") | |
if random.randrange(3) == 1: | |
floor, direction = random.randrange(10), random.choice((1, -1)) | |
lift.call_to_floor(floor, direction) | |
print(" called from", floor, "going", direction, end="") | |
print() | |
class Lift: | |
def __init__(self, num_floors, initial_floor=0): | |
self.floors = [0] * num_floors | |
self.current_floor = initial_floor | |
self.direction = 1 | |
def call_to_floor(self, floor, direction): | |
if direction not in {1, -1}: | |
raise ValueError | |
if self.floors[floor] == 4: | |
return | |
if self.floors[floor] == -direction: | |
self.floors[floor] = 4 | |
else: | |
self.floors[floor] = direction | |
def go_to_floor(self, floor): | |
self.floors[floor] = 4 | |
def print_info(self): | |
floor_vis = ( | |
str(self.current_floor).rjust(self.current_floor).ljust(len(self.floors)) | |
) | |
print(floor_vis, self.floors, self.direction, end="") | |
def move(self): | |
self.print_info() | |
if any(self.floors): | |
if self.floors[self.current_floor] in {4, self.direction}: | |
print(" Opened doors", end="") | |
self.floors[self.current_floor] = 0 | |
return | |
if self.current_floor == 0: | |
self.direction = 1 | |
for floor in self.floors[ | |
self.current_floor + self.direction :: self.direction | |
]: | |
if floor: | |
self.current_floor += self.direction | |
break | |
else: | |
self.direction = -self.direction | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment