Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created October 17, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save les-peters/e546e1be2774bea3597c668259dca770 to your computer and use it in GitHub Desktop.
Save les-peters/e546e1be2774bea3597c668259dca770 to your computer and use it in GitHub Desktop.
Open Doors
question = """
Let's say you have n doors that start out as closed. With the
first pass across the doors, you toggle every door open. With
the second pass, you toggle every second door. With the third,
every third door, and so on. Write a function that takes in an
integer numberOfPasses, and returns how many doors are open after
the number of passes. Thanks Max for inspiring this question!
Example:
let n = 7
let numberOfPasses = 3
> passDoors(n, numberOfPasses)
> 4
// Explanation:
// 0 means open, 1 means closed
// Initial: 1 1 1 1 1 1 1
// Pass 1: 0 0 0 0 0 0 0
// Pass 2: 0 1 0 1 0 1 0
// Pass 3: 0 1 1 1 0 0 0
"""
def passDoors(doors, passes):
doors_open = 0
for door in range(1, doors+1):
divisors = 0
for i in range(1, passes+1):
if (door / i) == int(door / i) and i <= passes:
divisors += 1
if divisors % 2 == 1:
doors_open += 1
print(doors_open)
doors = 7
numberOfPasses = 3
passDoors(doors, numberOfPasses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment