Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The ` ` N Doors Puzzle is a great challenge to explore various aspects of algorithmic thinkingand Python programming. Imagine you have N doors in
The N Doors Puzzle is a great challenge to explore various aspects of algorithmic thinkingand Python programming.
Imagine you have N doors in a row that are all initially closed.
You make N pass by each of these 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 nd door ie door # # # and toggle it
The third time, visit every rd door ie door # # # etc., until you only visit the
N th door on the N th pass.
Implement your algorithmic thinking using Python programming to determine the state of the
doors after the last pass. That is which doors are open and which are closed after the last pass?
Looking at the N doors puzzle we can make the following observations:
There are doors numbered through N that start closed. You can assume a list of
Boolean variables. Initially, they are all going to be set to False, with false representing a
closed door and true representing an open door.
We will iterate over the range N times, toggling the doors openclosed represented by
truefalse
Each iteration over the range the gap between toggles increases by a multiple,
incremented by one.
On the last iteration, only the N th door will be toggled
Sample IO:
Run :
Enter the number of Doors N:
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
i: ; j:; step size: Toggling door number
Algorithm has finished.
Door number remains open.
Door number remains open.
Door number remains open.
Door number remains closed.
Door number remains closed.
Door number remains closed.
Door number remains closed.
Door number remains closed.
Door number remains closed.
Door number remains closed.
Bonus Part points:
For all the open doors, i after the last pass ie N th pass, return a list where:
doorsi "FizzBuzz" if i is divisible by and
doorsi "Fizz" if i is divisible by
doorsi "Buzz" if i is divisible by
doorsi i if none of the above conditions are true.
Run : When N
N Doors Puzzle's Fizz Buzz Implementation: 'fizz',
Run : When N
N Doors Puzzle's Fizz Buzz Implementation: 'fizz', 'buzz', 'fizz', 'fizz', 'buzz'
Run : When N
N Doors Puzzle's Fizz Buzz Implementation: 'fizz', 'buzz', 'fizz', 'fizz', 'buzz',
'fizz', 'fizzbuzz',
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started