I need help converting this problem into a python program. I've been at it for days and its due tomorrow. Someone please help!

In a locker room there are 1,000 lockers in a row numbered from 1 to 1,000. They are all closed. A monkey comes in and starting at locker 1opens every locker. Then another monkey comes in and starting with door two reverses every second door. Then another monkey comes in and reverses the door on every third locker. Then monkeys repeat the procedure with every fourth locker and every fifth and so on. What is the status of the lockers after 1,000 of these maneuvers? Create a Python program that allows a user to inquire the status of a given locker after the monkeys are done with the lockers.

Recommended Answers

All 5 Replies

Please start a new thread when you have a new question. What you have done here (posting a new question at the bottom of an existing thread) is called "thread hijacking" and is very frowned upon :(

Thread split and moved from C++ -> Python forum. Now read this and you're ready to start.

The status of a locker L is calculated by the number of divisors of L.
The most naive approach is to count all divisors. The advanced would be prime factorization.
If the first locker is indexed by 1 and the last 1000, then

locker=4
print "open? ",bool(sum(1 for i in xrange(1,locker+1) if locker%i==0)%2)

The answer is simple. Put Some Damn Locks On The Lockers! You have to have a way to keep track of the lockers that you understand. It can be as simple as a list of integers that you set to zero/false for closed, and 1/true for open, that is sent to a function that also receives the "monkey number" and reverses every "monkey number" locker, then returns the list. Print the list and see if it is the same as slate's solution.

##   A helpful trick
x = False
for j in range(5):
    print x
    x = not(x)
## or
x = 0
reverse = [1, 0]
for j in range(5):
    print x
    x = reverse[x]
print x

These monkey's second name must be Erastoshtenes :) And the key opening/closing is called slice with step parameter. Then we have status of every door ready. Only these monkeys continue after square root of 1000 with only job of one door.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.