Skip to content

Commit b620b38

Browse files
committed
feat: locker problem
1 parent 11f6c4b commit b620b38

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

algorithms/sequences/lockers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
PROBLEM:
3+
4+
Imagine 100 lockers numbered 1 to 100 with 100 students lined up in front of those 100 lockers:
5+
6+
The first student opens every locker.
7+
8+
The second student closes every 2nd locker.
9+
10+
The 3rd student changes every 3rd locker; if it's closed, she opens it; if it's open, she closes it.
11+
12+
The 4th student changes every fourth locker.
13+
14+
The 5th student changes every 5th locker.
15+
16+
That same pattern continues for all 100 students.
17+
18+
Which lockers are left open after all 100 students have walked the row of lockers?
19+
"""
20+
21+
STUDENTS = 100
22+
23+
lockers = [0] * 100
24+
25+
for student in range(1, STUDENTS + 1):
26+
for i, locker in enumerate(lockers):
27+
if (i + 1) % student == 0:
28+
if lockers[i] == 0:
29+
lockers[i] = 1
30+
elif lockers[i] == 1:
31+
lockers[i] = 0
32+
33+
open_lockers = [i + 1 for i, locker in enumerate(lockers) if locker == 1]
34+
print(open_lockers)

0 commit comments

Comments
 (0)