-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindX.py
More file actions
33 lines (26 loc) · 927 Bytes
/
findX.py
File metadata and controls
33 lines (26 loc) · 927 Bytes
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
# Given a matrix (list of lists) of size N*M and a number X, find if X is present in matrix.
# Invokation: python3 findX.py
from typing import List
def findX(mat: List[List[int]], n: int, m: int, target: int) -> bool:
'''
Function returns True if element present, otherwise False.
'''
for i in range(n):
for j in range(m):
if(mat[i][j] == target):
return True
return False
try:
N, M = map(int, input("Please enter N and M (dimensions): ").split())
print("Enter the matrix row-wise, space seperated integers: ")
mat = []
for i in range(N):
mat.append(list(map(int, input().split())))
X = int(input("Enter the number to search: "))
output = findX(mat, N, M, X)
if(output):
print(f'{X} is present in the matrix.')
else:
print(f'{X} is not present in the matrix.')
except:
print("Please enter valid input!")