-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathDoors.py
More file actions
44 lines (42 loc) · 1.49 KB
/
Doors.py
File metadata and controls
44 lines (42 loc) · 1.49 KB
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
34
35
36
37
38
39
40
41
42
43
44
# In the morning all the doors in the school are closed. The school is quite big:
# there are N doors. Then pupils start coming. It might be hard to believe, but
# all of them want to study! Also, there are exactly N children studying in this
# school, and they come one by one.
#
# When these strange children pass by some doors they change their status
# (i.e. Open -> Closed, Closed -> Open). Each student has their number,
# and each i-th student alters the status of every i-th door. For example:
# when the first child comes to the schools, he changes every first door
# (he opens all of them). The second one changes the status of every second door
# (he closes some doors: the 2nd, the 4th and so on). Finally, when the last one –
# the n-th – comes to the school, he changes the status of each n-th door
# (there's only one such door, though).
#
# You need to count how many doors are left opened after all the students have come.
#
# Example:
#
# Here you can see red squares – closed doors, green – opened ones.
#
# Input:
#
# n – the number of doors and students, n ∈ N, n ∈ [1, 100000]
# Output:
#
# o – the number of opened doors, o ∈ N
# doors(5)
# Should return
#
# 2
# My Solution
def doors(n):
door_status = []
for a in range(0,n):
door_status.append(False)
for a in range(0,n):
for b in range(a,n,a+1):
door_status[b] = not door_status[b]
result = 0
for a in range(0,n):
if door_status[a] == True: result += 1
return result