Advertisement
sphinx2001

sm_work_2

Feb 6th, 2021
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import copy
  2. d = """1 2 3 4 5 6 7 8
  3. 8 7 6 5 4 3 2 1
  4. 2 3 4 5 6 7 8 9
  5. 9 8 7 6 5 4 3 2
  6. 1 3 5 7 9 7 5 3
  7. 3 1 5 3 2 6 5 7
  8. 1 7 5 9 7 3 1 5
  9. 2 6 3 5 1 7 3 2"""
  10.  
  11. matrix = [[int(x) for x in line.split()]
  12.           for line in d.split("\n")]
  13.  
  14.  
  15. def print_matrinx(matrix):
  16.     for x in range(len(matrix)):
  17.         for y in range(len(matrix[x])):
  18.             print(f"{matrix[x][y]:3}", end=" ")
  19.         print()
  20. print_matrinx(matrix)
  21.  
  22. def square(m):
  23.     for x in range(len(m)):
  24.         for y in range(len(m[x])):
  25.             m[x][y] = m[x][y]**2
  26.  
  27. def square_even(m):
  28.     count = 0
  29.     for x in range(len(m)):
  30.         for y in range(len(m[x])):
  31.             count += 1
  32.             if count % 2 == 0:
  33.                 m[x][y] = m[x][y]**2
  34.  
  35.  
  36. def square_less_five(m):
  37.     count = 0
  38.     for x in range(len(m)):
  39.         for y in range(len(m[x])):            
  40.             if count < 5:
  41.                 m[x][y] = m[x][y]**2
  42.             else:
  43.                 return
  44.             count += 1
  45.  
  46.            
  47. def square_first_4_line(m):
  48.     for x in range(4):
  49.         for y in range(len(m[x])):            
  50.             m[x][y] = m[x][y]**2
  51.  
  52. print("Squares")
  53. mat = copy.deepcopy(matrix)
  54. square(mat)
  55. print_matrinx(mat)
  56. print("Squares even")
  57. mat = copy.deepcopy(matrix)
  58. square_even(mat)
  59. print_matrinx(mat)
  60. print("Squares less 5")
  61. mat = copy.deepcopy(matrix)
  62. square_less_five(mat)
  63. print_matrinx(mat)
  64. print("Squares first 4 line")
  65. mat = copy.deepcopy(matrix)
  66. square_first_4_line(mat)
  67. print_matrinx(mat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement