Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- def print_matrix(c, r, mat):
- for i in range(c):
- for j in range(r):
- print(mat[i][j], end=' ')
- print('')
- def check_situation(element, current, middle, minimum, maximum):
- if current <= middle:
- if element > maximum:
- maximum = element
- else:
- if element < minimum:
- minimum = element
- return current + 1, minimum, maximum
- def get_min_max(n, m, matrix):
- print_matrix(n, m, matrix)
- minimum = sys.maxsize
- maximum = sys.maxsize * (-1)
- middle = (n * m) // 2
- current = 0 # current element is incremented by one on every turn
- top = 0 # top line
- bottom = n - 1 # bottom line
- left = 0 # left column
- right = m - 1 # right column
- print('\nSpiral matrix: ', end=' ')
- while top <= bottom and left <= right:
- # from left to right on the top line
- for i in range(left, right + 1):
- current, minimum, maximum = check_situation(matrix[top][i], current, middle, minimum, maximum)
- print(matrix[top][i], end=' ')
- top += 1
- # from top to bottom on the right column
- for i in range(top, bottom + 1):
- current, minimum, maximum = check_situation(matrix[i][right], current, middle, minimum, maximum)
- print(matrix[i][right], end=' ')
- right -= 1
- # from right to left on the bottom line
- for i in range(right, left - 1, -1):
- current, minimum, maximum = check_situation(matrix[bottom][i], current, middle, minimum, maximum)
- print(matrix[bottom][i], end=' ')
- bottom -= 1
- # from bottom to top on the left column
- for i in range(bottom, top - 1, -1):
- current, minimum, maximum = check_situation(matrix[i][left], current, middle, minimum, maximum)
- print(matrix[i][left], end=' ')
- left += 1
- print('\n')
- print('Min:', minimum, 'Max:', maximum, 'Mid:', middle)
- def start():
- # matrix[n][m]
- matrix = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
- n = len(matrix)
- m = len(matrix[0])
- get_min_max(n, m, matrix)
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement