Skip to content

Instantly share code, notes, and snippets.

@neo7BF
Created January 17, 2021 13:43
Show Gist options
  • Save neo7BF/aa9bbc76fc02a84ac2a2b4f1fe701b12 to your computer and use it in GitHub Desktop.
Save neo7BF/aa9bbc76fc02a84ac2a2b4f1fe701b12 to your computer and use it in GitHub Desktop.
Search in matrix with Phyton
w, h = 8, 5;
# le prime due quadre interne creano un array di W posti riempito di zeri (asse x), questo stesso array è poi usato come valore per riempire H posti (asse y) formando una matrice bidimensionale.
MatrixTest1 = [[0 for x in range(w)] for y in range(h)]
MatrixTest2 = [[0 for x in range(w)] for y in range(h)]
MatrixTest3 = [[0 for x in range(w)] for y in range(h)]
MatrixTest1[0][0] = 2
MatrixTest1[0][1] = 2
MatrixTest1[4][6] = 3
MatrixTest1[2][3] = 4
MatrixTest2[0][0] = 3
MatrixTest2[0][1] = 2
MatrixTest2[4][6] = 3
MatrixTest2[2][3] = 3
MatrixTest3[0][0] = 4
MatrixTest3[0][1] = 4
MatrixTest3[4][6] = 4
MatrixTest3[2][3] = 4
def cercaInMatrice(n,m):
count = 0;
for row in m:
for cell in row:
if cell == n:
count = count + 1
return bool(count == n)
#Usa per test 1
print(cercaInMatrice(2,MatrixTest1))
#Usa per test 2
print(cercaInMatrice(3,MatrixTest2))
#Usa per test 3
print(cercaInMatrice(4,MatrixTest3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment