Answer to Question #301227 in Python for Soumya

Question #301227

in a building, there are N rooms numbered from 1 to N. In each room there is a bulb that is initially ON for the odd-numbered rooms and OFF for the even-numbered rooms.before visiting a room, you will note the state of the bulb. when you leave a room, the bulb is turned OFF if it is ON and turned ON if it is OFF. you visit all rooms in the same order as they are numbered. you then return to the starting room and repeat the process for N times. you are given N, write a program that should output the state of the bulb in each room for every visit

Note: Print 1 if the bulb is ON, 0 otherwise

I/p: The input is a single line containing a positive integer N representing the number of room in the building

O/p: The output contains N lines each line containing the state of the bulb in the room numbered from 1 to N

Note: These is a space after each 1 or 0

I/p: N=5

1 0 1 0 1

0 1 0 1 0

1 0 1 0 1

0 1 0 1 0

1 0 1 0 1

I/p: N=6

O/p:

1 0 1 0 1 0

0 1 0 1 0 1

1 0 1 0 1 0

0 1 0 1 0 1

1 0 1 0 1 0

0 1 0 1 0 1



1
Expert's answer
2022-02-22T12:13:18-0500
print("Enter N: ", end='')
n = int(input())
array = [[0] * n for i in range(n)]
for i in range(n):
    if i % 2 == 0:
        for j in range(0, n, 2):
            array[i][j] = 1
    else:
        for j in range(1, n, 2):
            array[i][j] = 1
print()
for i in range(n):
    for j in range(n):
        print(array[i][j], end=' ')
    print()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog