Wrong Connections Problem in Python

In this class, we write a program for wrong connections problem in python.

For Complete YouTube Video: Click Here

Wrong Connections Problem

Q1) Check the conditions given below and write a program in python.

Eight lights are given wrong connections. Because of wrong connections, the lights have the following conditions.

C1) If both left and right lights are on or off, the light will be off the next day.

C2) If one light is on and the other light is off. The light will be on the next day.

C3) For the first light and last light. Left and right lights are not present. Default take them as off.

Identify the position of lights after N days.

Let’s take an example and understand the question better.

Example:

Input: Number of days.

The position of eight lights is also taken as input. One is considered on, and 0 is considered off.

N=2

11101111 are the eight light positions. On and off.

Output: 00000110

Logic

Loop on each light.

Each time should consider the left light on/off position and the right light on/off position.

Suppose both left and right light positions are the same. The light will off the next day.

With this, if-else condition updates all the light positions.

Repeat this for N number of times.

We need the nested loop to write the logic.

All these examples will help in improving coding skills. Please analyze the program given below for better practice.

For improving coding skills. take practice from the beginning. Click here.

Program

days=int(input("enter number of days"))
print("enter the position of 8 street lights")
list1=[]
list2=[0]*8
for i in range(8):
    list1.append(int(input()))
for j in range(days):
    for k in range(8):
        if k==0:
            lpl=0
            rpl=list1[k+1]
        elif k==7:
            lpl=list1[k-1]
            rpl=0
        else:
            lpl=list1[k-1]
            rpl=list1[k+1]
        if lpl==rpl:
            list2[k]=0
        else:
            list2[k]=1
    list1=list(list2)
print(list1)