HackerRank Eye and Identity problem solution in Python YASH PAL, 31 July 202417 January 2026 HackerRank Eye and Identity problem solution in Python – In this Eye and Identity problem, your task is to print an array of size N X M with its main diagonal elements as 1’s and 0’s everywhere else.identityThe identity tool returns an identity array. An identity array is a square matrix with all the main diagonal elements as 1 and the rest as 0. The default type of elements is float.import numpy print numpy.identity(3) #3 is for dimension 3 X 3 #Output [[ 1. 0. 0.] [ 0. 1. 0.] [ 0. 0. 1.]] eyeThe eye tool returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. The diagonal can be main, upper or lower depending on the optional parameter k. A positive k is for the upper diagonal, a negative k is for the lower, and a 0 k (default) is for the main diagonal.import numpy print numpy.eye(8, 7, k = 1) # 8 X 7 Dimensional array with first upper diagonal 1. #Output [[ 0. 1. 0. 0. 0. 0. 0.] [ 0. 0. 1. 0. 0. 0. 0.] [ 0. 0. 0. 1. 0. 0. 0.] [ 0. 0. 0. 0. 1. 0. 0.] [ 0. 0. 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 0. 0. 1.] [ 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0.]] print numpy.eye(8, 7, k = -2) # 8 X 7 Dimensional array with second lower diagonal 1. HackerRank Eye and Identity solution in Python 2.import numpy N, M = map(int, raw_input().split()) print numpy.eye(N,M)Eye and Identity solution in Python 3.import numpyprint(str(numpy.eye(*map(int,input().split()))).replace('1',' 1').replace('0',' 0'))Problem solution in pypy programming.# Enter your code here. Read input from STDIN. Print output to STDOUT import numpy n, m = map(int, raw_input().strip().split()) print numpy.eye(n, m) Problem solution in pypy3 programming.# Enter your code here. Read input from STDIN. Print output to STDOUT import numpy as np N,M=list(map(int,input().split())) print(np.eye(N,M)) coding problems solutions Hackerrank Problems Solutions Python Solutions HackerRankPython