Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Scrivere una funzione adiacenti che prende in Input una matrice di interi
- #e restituisce True se tutti gli elementi adiacenti agli elementi primi sono pari,
- #False altrimenti.
- #Dato un elemento, i suoi adiacenti sono tutti gli elementi che si trovano nel
- #suo intorno
- def adiacenti(M):
- for i in range(len(M)):
- for j in range(len(M[i])):
- if isPrimo(M[i][j]):
- if verificaPari(M,i,j) is False:
- return False
- return True
- def verificaPari(M,i,j):
- if i - 1 >= 0:
- if M[i-1][j] % 2 != 0:
- return False
- if j - 1 >= 0:
- if M[i][j-1] % 2 != 0:
- return False
- if j + 1 < len(M[i]):
- if M[i][j+1] % 2 != 0:
- return False
- if i + 1 < len(M):
- if M[i+1][j] % 2 != 0:
- return False
- return True
- def isPrimo(num):
- for i in range(2,num):
- if num % i == 0:
- return False
- return True
- M = [[20,20,20],
- [4,5,6],
- [7,8,9]]
- print(adiacenti(M))
Advertisement
Add Comment
Please, Sign In to add comment