Skip to main content
4 of 8
added 38 characters in body
Ravinder Baid
  • 207
  • 1
  • 2
  • 7

Solution to king kohima problem in python

King Kohima problem:

King Kohima has reserved a new exclusive street for his executive class employees where they can build their homes. He has assigned you to plan that street. You have to decide on which plots along the street are new building allowed to be built. In order to this, you first want to calculate the number of possible ways of assigning free plots to buildings while keeping in mind this restriction – No two consecutive plots can have buildings on them. This is done to ensure a sense of free space in the arena. The street is divided in M sections. Each section corresponds to 2 plots, one on each side of the street. Find the number of possible assignments.

Input

In the first line you’re given M ( M ≤ 1000 ). Output In the first and only line output the result/ Example Input: 3 Output: 25 Example explanation: If we just look at the one street side and mark X as a plot where building is allowed and Y as a free plot, we have: XYX, YXY, YYX, XYY, YYY. Since the same number exists on the other side, we have 5*5 = 25 combinations.

So if input is 3, the total combination possible is 8 like(YYY,YYX,YXX,YXY,XYY,XYX,XXY,XXX) But valid are only-YYY,YYX,YXY,XYY,XYX

I have tried this code in Python can one tell me is this the right solution for King Kohima's problem?

def binary(i):
    r=str(0)
    while(i):
        x=str(i%2)
        if(x=='1' and r[-1]=='1'): #two consecutive X are not possible 
            return None
        else:
            r=r+str(x)
        i=i/2
    return r    
m=3 #input
l=[]
for i in range(0,2**m):
    l.append(binary(i)) #converted the number to binary
print(l)
c=0 #count possible plot available
for i in l:
    if i:
        c=c+1
print(c*c) #both side of the lane
Ravinder Baid
  • 207
  • 1
  • 2
  • 7