A Boolean Matrix Question | Python3 Code For A Boolean Matrix Question ; variables to check if there are any 1 in first row and column ; updating the first row and col if 1 is encountered ; Modify the input matrix mat [ ] using the first row and first column of Matrix mat ; modify first row if there was any 1 ; modify first col if there was any 1 ; A utility function to print a 2D matrix ; Driver Code
def modifyMatrix ( mat ) : NEW_LINE INDENT row_flag = False NEW_LINE col_flag = False NEW_LINE for i in range ( 0 , len ( mat ) ) : NEW_LINE INDENT for j in range ( 0 , len ( mat ) ) : NEW_LINE INDENT if ( i == 0 and mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT row_flag = True NEW_LINE DEDENT if ( j == 0 and mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT col_flag = True NEW_LINE DEDENT if ( mat [ i ] [ j ] == 1 ) : NEW_LINE INDENT mat [ 0 ] [ j ] = 1 NEW_LINE mat [ i ] [ 0 ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , len ( mat ) ) : NEW_LINE INDENT for j in range ( 1 , len ( mat ) + 1 ) : NEW_LINE INDENT if ( mat [ 0 ] [ j ] == 1 or mat [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT mat [ i ] [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT if ( row_flag == True ) : NEW_LINE INDENT for i in range ( 0 , len ( mat ) ) : NEW_LINE INDENT mat [ 0 ] [ i ] = 1 NEW_LINE DEDENT DEDENT if ( col_flag == True ) : NEW_LINE INDENT for i in range ( 0 , len ( mat ) ) : NEW_LINE INDENT mat [ i ] [ 0 ] = 1 NEW_LINE DEDENT DEDENT DEDENT def printMatrix ( mat ) : NEW_LINE INDENT for i in range ( 0 , len ( mat ) ) : NEW_LINE INDENT for j in range ( 0 , len ( mat ) + 1 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT mat = [ [ 1 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE print ( " Input ▁ Matrix ▁ : " ) NEW_LINE printMatrix ( mat ) NEW_LINE modifyMatrix ( mat ) NEW_LINE print ( " Matrix ▁ After ▁ Modification ▁ : " ) NEW_LINE printMatrix ( mat ) NEW_LINE