Today we are going to share a Python Code For A Boolean Matrix Question. If you are a python beginner and want to start learning the python programming.
To increase your Python knowledge, practice all Python programs, here is a collection of 100+ Python problems with solutions.
Copy the below python program and execute it with the help of python compiler.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | def modifyMatrix(mat) : # variables to check if there are any 1 # in first row and column row_flag = False col_flag = False # updating the first row and col # if 1 is encountered for i in range(0, len(mat)) : for j in range(0, len(mat)) : if (i == 0 and mat[i][j] == 1) : row_flag = True if (j == 0 and mat[i][j] == 1) : col_flag = True if (mat[i][j] == 1) : mat[0][j] = 1 mat[i][0] = 1 # Modify the input matrix mat[] using the # first row and first column of Matrix mat for i in range(1, len(mat)) : for j in range(1, len(mat) + 1) : if (mat[0][j] == 1 or mat[i][0] == 1) : mat[i][j] = 1 # modify first row if there was any 1 if (row_flag == True) : for i in range(0, len(mat)) : mat[0][i] = 1 # modify first col if there was any 1 if (col_flag == True) : for i in range(0, len(mat)) : mat[i][0] = 1 # A utility function to print a 2D matrix def printMatrix(mat) : for i in range(0, len(mat)) : for j in range(0, len(mat) + 1) : print( mat[i][j], end = "" ) print() # Driver Code mat = [ [1, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0] ] print("Input Matrix :") printMatrix(mat) modifyMatrix(mat) print("Matrix After Modification :") printMatrix(mat) |
Input Matrix :
1001
0010
0000
Matrix After Modification :
1111
1111
1011
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.