• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest May 16th, 2018 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sys
2.
3.
4. def print_matrix(c, r, mat):
5.     for i in range(c):
6.         for j in range(r):
7.             print(mat[i][j], end=' ')
8.         print('')
9.
10.
11. def check_situation(element, current, middle, minimum, maximum):
12.     if current <= middle:
13.         if element > maximum:
14.             maximum = element
15.     else:
16.         if element < minimum:
17.             minimum = element
18.     return current + 1, minimum, maximum
19.
20.
21. def get_min_max(n, m, matrix):
22.     print_matrix(n, m, matrix)
23.     minimum = sys.maxsize
24.     maximum = sys.maxsize * (-1)
25.     middle = (n * m) // 2
26.     current = 0  # current element is incremented by one on every turn
27.
28.     top = 0  # top line
29.     bottom = n - 1  # bottom line
30.     left = 0  # left column
31.     right = m - 1  # right column
32.     print('\nSpiral matrix: ', end=' ')
33.
34.     while top <= bottom and left <= right:
35.
36.         # from left to right on the top line
37.         for i in range(left, right + 1):
38.             current, minimum, maximum = check_situation(matrix[top][i], current, middle, minimum, maximum)
39.             print(matrix[top][i], end=' ')
40.         top += 1
41.
42.         # from top to bottom on the right column
43.         for i in range(top, bottom + 1):
44.             current, minimum, maximum = check_situation(matrix[i][right], current, middle, minimum, maximum)
45.             print(matrix[i][right], end=' ')
46.         right -= 1
47.
48.         # from right to left on the bottom line
49.         for i in range(right, left - 1, -1):
50.             current, minimum, maximum = check_situation(matrix[bottom][i], current, middle, minimum, maximum)
51.             print(matrix[bottom][i], end=' ')
52.         bottom -= 1
53.
54.         # from bottom to top on the left column
55.         for i in range(bottom, top - 1, -1):
56.             current, minimum, maximum = check_situation(matrix[i][left], current, middle, minimum, maximum)
57.             print(matrix[i][left], end=' ')
58.         left += 1
59.
60.     print('\n')
61.     print('Min:', minimum, 'Max:', maximum, 'Mid:', middle)
62.
63.
64. def start():
65.     # matrix[n][m]
66.     matrix = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
67.     n = len(matrix)
68.     m = len(matrix)
69.     get_min_max(n, m, matrix)
70.
71.
72. start()
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.

Top