Skip to content

Commit dda12eb

Browse files
Initial elevator
0 parents  commit dda12eb

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

elevator.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import time
2+
3+
def wait(seconds, reason):
4+
print("%s (%d seconds)" % (reason, seconds))
5+
time.sleep(seconds)
6+
7+
class Elevator(object):
8+
def __init__(self):
9+
self.current_floor = 1
10+
self.is_open = False
11+
12+
def openDoor(self):
13+
wait(2, "Opening door")
14+
self.is_open = True
15+
16+
def closeDoor(self):
17+
wait(2, "Closing door")
18+
self.is_open = False
19+
20+
def goToFloor(self, target_floor):
21+
# assume 2 seconds to move from one floor to the next, but 2 extra seconds to start and 3 extra seconds to stop
22+
original_floor = self.current_floor
23+
while (self.current_floor != target_floor):
24+
# Going up!
25+
if (self.current_floor < target_floor):
26+
if (self.current_floor == original_floor):
27+
wait(2, "Starting elevator")
28+
wait(2, "Moving from floor %d to %d" % (self.current_floor, self.current_floor + 1))
29+
if (self.current_floor == target_floor - 1):
30+
wait(3, "Stopping elevator")
31+
self.current_floor += 1
32+
# Going down!
33+
elif (self.current_floor > target_floor):
34+
if (self.current_floor == original_floor):
35+
wait(2, "Starting elevator")
36+
wait(2, "Moving from floor %d to %d" % (self.current_floor, self.current_floor - 1))
37+
if (self.current_floor == target_floor + 1):
38+
wait(3, "Stopping elevator")
39+
self.current_floor -= 1
40+

0 commit comments

Comments
 (0)