Question

Write a function occupy(n), which shows how birds are going to occupy n nests, assuming that...

Write a function occupy(n), which shows how birds are going to occupy n nests, assuming that each new bird will choose the nest in the middle of the largest unoccupied run of nests.For example, if there were 10 nests, occupy(10) would print out the following sequence, where underscore indicates an unoccupied nest, and X indicates an occupied nest. The first line of the printout is just 10 underscores showing that all the nests are unoccupied. The second line shows that a bird came to nest in position 5, since that is one the first middle positions of the unoccupied run from 0 to 9. In the third line a bird came to occupy the middle position for the longest open run of nests, from 0 to index 4.

_ _ _ _ _ _ _ _ _ _

_ _ _ _ _ X_ _ _ _

_ _ X _ _ X _ _ _ _

_ _X _ _ X _ _ X _

and so on until

XXXXXXXXXX

programming language: python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

Note: There are different approaches to solve this problem, and you have to understand that after a few steps, the process of finding middle free position become irrelevant as there will be a lot of spaces (obviously shorter run of nests) with same possibility of placing the next bird. So whatever algorithm we use, only the first few placements will be relevant. If you have any unit testing code or if you expect a specific set of sequences from start to end, just let me know.

#code

''' required method '''
def occupy(n):
    #creating a list of n number of underscores
    nests=['_']*n
    #creating a list of n number of 'X' chars
    target=['X']*n
    #printing initial status of nests (underscores separated by space)
    print(' '.join(nests))
    #recording first and last indices
    front=0
    rear=n-1
    #creating another list, to act like a queue, adding a tuple containing front and
    #rear indices
    queue=[(front,rear)]
    #loops until nests become all 'X's
    while nests!=target:
        #removing first element from queue, into front and rear variables
        front,rear=queue.pop(0)
        #finding number of elements between front and rear
        count=rear-front+1
        #if count is event, finding index of middle right element as the mid index
        if count % 2 == 0:
            mid = (front+rear)//2 +1
        #otherwise, middle of front and rear as mid index
        else:
            mid = (front+rear)//2
        #if the position is already filled by an 'X', moving to next iteration
        #skipping further steps
        if nests[mid]=='X':
            continue
        #otherwise assigning 'X' to mid pos
        nests[mid]='X'
        #printing status of nests
        print(' '.join(nests))
        #appending a tuple containing front and mid-1 indices to queue
        queue.append((front,mid-1))
        # appending a tuple containing mid+1 and rear indices to queue
        queue.append((mid + 1, rear))


#testing
occupy(10)

#OUTPUT

Add a comment
Know the answer?
Add Answer to:
Write a function occupy(n), which shows how birds are going to occupy n nests, assuming that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING It is a well-researched fact that men in a restroom generally prefer to maximize...

    JAVA PROGRAMMING It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For example, consider the situation where ten stalls are empty. _ _ _ _ _ _ _ _ _ _    The first visitor will occupy a middle position: _ _ _ _ _ X _ _ _ _   The next visitor will be in the middle...

  • I need help with java It is a well-researched fact that men in a restroom generally...

    I need help with java It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For example, consider the situation where ten stalls are empty. __________ The first visitor will occupy a middle position: _____X____ The next visitor will be in the middle of the empty area at the left. __X__X____ Write a program that reads the number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT