100 Doors Kata

The 100 doors problem statement is:

There are 100 doors in a row that are all initially closed.

You make 100 passes by the doors.

The first time through, visit every door and  toggle  the door  (if the door is closed,  open it;   if it is open,  close it).

The second time, only visit every 2nd door   (door #2, #4, #6, ...),   and toggle it.

The third time, visit every 3rd door   (door #3, #6, #9, ...), etc,   until you only visit the 100th door.

What state are the doors in after the last pass?   Which are open, which are closed?

Not much to say about this one except during the dojo we had some discussion about whether we would need a class to represent a door and an array of doors vs using an array of flags to model the open/closed-ness.


# initially closed
doors = [False] * 100

# loop over all doors. 
for i in range(100):
    for j in range(i, 100, i + 1):
        # toggle the door
        doors[j] = not doors[j]

# print all states 
for door in doors:
    print("open" if door else "closed")

count_open = sum(door for door in doors)
print(str(count_open) + " open")

count_closed = sum(not door for door in doors)
print(str(count_closed) + " closed")
    

There was also a debate about the likely need for a different number of doors and a discussion of the costs, benefits and YAGNI.