-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoorOpenTest.py
36 lines (26 loc) · 1.2 KB
/
DoorOpenTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""There are 500 rooms in a hotel with 500 employees having keys to all the rooms. The first employee opened main
entrance door of all the rooms. The second employee then closed the doors of room numbers 2,4,6,8,10 and so on to 500.
The third employee then closed the door if it was opened or opened the door if it was closed of rooms 3,6,9,12,15
and so on to 500. Similarly the fourth employee did the same as the third but only room numbers 4,8,12,16 and so on
to 500. This goes on until all employees has had a turn.
Write a script to find out all the rooms still open at the end.
"""
# 500 rooms with 500 doors
# 500 employees, each turns the key for their number
# (i.e. Emp 1 opens all doors, Emp 2 closes (if open) doors two apart-2,4,6,etc.)
# loop through 500 doors performing action based on their position
# Main
# Setup doors and employees
def setup_hotel():
hotel = []
for i in range(1, 501):
# Door = 1 is closed, Door = 0 is open.
hotel.append([i, 1])
# Cycle through the rooms
for room in hotel:
for x in range(1,500):
if x % room[0] == 0 and hotel[x][1] == 1:
hotel[x][1] = 0
return hotel
my_hotel = setup_hotel()
print(my_hotel)