davegimo

Untitled

Jun 4th, 2022
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #Scrivere una funzione adiacenti che prende in Input una matrice di interi
  2. #e restituisce True se tutti gli elementi adiacenti agli elementi primi sono pari,
  3. #False altrimenti.
  4. #Dato un elemento, i suoi adiacenti sono tutti gli elementi che si trovano nel
  5. #suo intorno
  6.  
  7.  
  8.  
  9. def adiacenti(M):
  10.  
  11.     for i in range(len(M)):
  12.         for j in range(len(M[i])):
  13.             if isPrimo(M[i][j]):
  14.                 if verificaPari(M,i,j) is False:
  15.                     return False
  16.  
  17.     return True
  18.  
  19.  
  20. def verificaPari(M,i,j):
  21.  
  22.     if i - 1 >= 0:
  23.         if M[i-1][j] % 2 != 0:
  24.             return False
  25.  
  26.     if j - 1 >= 0:
  27.         if M[i][j-1] % 2 != 0:
  28.             return False
  29.  
  30.     if j + 1 < len(M[i]):
  31.         if M[i][j+1] % 2 != 0:
  32.             return False
  33.  
  34.     if i + 1 < len(M):
  35.         if M[i+1][j] % 2 != 0:
  36.             return False
  37.  
  38.     return True
  39.  
  40.  
  41.  
  42. def isPrimo(num):
  43.     for i in range(2,num):
  44.         if num % i == 0:
  45.             return False
  46.     return True
  47.  
  48.  
  49.  
  50. M = [[20,20,20],
  51.      [4,5,6],
  52.      [7,8,9]]
  53.  
  54.  
  55. print(adiacenti(M))
  56.  
  57.  
  58.  
Advertisement
Add Comment
Please, Sign In to add comment