### Locks in a grid problem

` ````
"""The purpose of this is to simulate the problem of locks
where when you turn on the key in the grid all the other
keys in the same row and column also turn. I represent this
by 0 and 1
One can experiment and learn that if the grid is even by even
then we can change any particular bit by"""
n = 4
a=[]
sum = 0
# It prints the whole Grid of Locks
def printMatrix(val):
for i in range(n):
print(val[i])
# It flips the entries from 0 to 1 and 1 to 0
def flip(val):
if val == 0:
return 1
return 0
# It changes the entries in a given row and column
def ChangeRowChangeColumn(a,i,j):
a[i][j]=flip(a[i][j])
for k in range(n):
a[i][k]=flip(a[i][k])
for k in range(n):
a[k][j]=flip(a[k][j])
for i in range(1,n+1):
b=[]
for j in range(1,n+1):
b.append(1)
c = b[:]
sum= sum+n
a.append(c)
del b[:]
printMatrix(a)
flag=1
while(flag == 1):
row=int(input("Enter what row you want to change ?"))
col=int(input("Enter what column you want to change?"))
ChangeRowChangeColumn(a,row,col)
printMatrix(a)
flag = int(input("Enter 0 or 1 to continue"))
```

Labels: lock problem, Python