SHARE
TWEET

Laba 3 PP

a guest May 25th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from threading import Thread, Lock
  2.  
  3. mutex = Lock()
  4.  
  5.  
  6. def find_squares(matrix, k):
  7.     mutex.acquire()
  8.     print(matrix)
  9.     sum_of_el = 0
  10.     for i in range(len(matrix) - 1):
  11.         for j in range(len(matrix) - 1):
  12.             for value in [matrix[i][j], matrix[i][j + 1], matrix[i + 1][j], matrix[i + 1][j + 1]]:
  13.                 sum_of_el += value
  14.             if sum_of_el == k:
  15.                 print(matrix[i][j], ' ', matrix[i][j + 1], '\n', matrix[i + 1][j], ' ', matrix[i + 1][j + 1], '\n',
  16.                       sep='')
  17.             elif sum_of_el < k and i + 2 < len(matrix) and j + 2 < len(matrix[i]):
  18.                 for value in [matrix[i][j + 2], matrix[i + 1][j + 2], matrix[i + 2][j], matrix[i + 2][j + 1],
  19.                               matrix[i + 2][j + 2]]:
  20.                     sum_of_el += value
  21.                 if sum_of_el == k:
  22.                     print(matrix[i][j], matrix[i][j + 1], matrix[i][j + 2])
  23.                     print(matrix[i + 1][j], matrix[i + 1][j + 1], matrix[i + 1][j + 2])
  24.                     print(matrix[i + 2][j], matrix[i + 2][j + 1], matrix[i + 2][j + 2])
  25.                     print()
  26.             sum_of_el = 0
  27.     mutex.release()
  28.  
  29.  
  30. matrix1 = [
  31.     [1, 2, 0, 1],
  32.     [2, 1, 0, 1],
  33.     [0, 0, 2, 2],
  34.     [0, 0, 1, 1]
  35. ]
  36. matrix2 = [
  37.     [1, 0, 1],
  38.     [5, 1, 3],
  39.     [7, 0, 1]
  40. ]
  41. matrix3 = [
  42.     [4, 3, 0, 0],
  43.     [3, 0, 4, 0],
  44.     [6, 2, 1, 0],
  45.     [0, 2, 5, 2]
  46. ]
  47.  
  48. thread1 = Thread(target=find_squares, args=(matrix1, 6))
  49. thread2 = Thread(target=find_squares, args=(matrix2, 5))
  50. thread3 = Thread(target=find_squares, args=(matrix3, 10))
  51. thread1.start()
  52. thread2.start()
  53. thread3.start()
  54. thread1.join()
  55. thread2.join()
  56. thread3.join()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top