Modify a matrix by converting each element to XOR of its digits | Python3 program for the above approach ; Function to calculate Bitwise XOR of digits present in X ; Stores the Bitwise XOR ; While X is true ; Update Bitwise XOR of its digits ; Return the result ; Function to print matrix after converting each matrix element to XOR of its digits ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Function to convert the given matrix to required XOR matrix ; Traverse each row of arr [ ] [ ] ; Traverse each column of arr [ ] [ ] ; Store the current matrix element ; Find the xor of digits present in X ; Stores the XOR value ; Print resultant matrix ; Driver Code
M = 3 NEW_LINE N = 3 NEW_LINE def findXOR ( X ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( X ) : NEW_LINE INDENT ans ^= ( X % 10 ) NEW_LINE X //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def printXORmatrix ( arr ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def convertXOR ( arr ) : NEW_LINE INDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT X = arr [ i ] [ j ] NEW_LINE temp = findXOR ( X ) NEW_LINE arr [ i ] [ j ] = temp NEW_LINE DEDENT DEDENT printXORmatrix ( arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 27 , 173 , 5 ] , [ 21 , 6 , 624 ] , [ 5 , 321 , 49 ] ] NEW_LINE convertXOR ( arr ) NEW_LINE DEDENT