Last active
January 31, 2016 10:52
-
-
Save romanitalian/736a7a1f0f85422759e4 to your computer and use it in GitHub Desktop.
Не самый лучший вариант преобразования двумерной матрицы, заполняемой построчно - в матрицу заполняемую по спирали (@todo доработать)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'romanitalian' | |
class Matrix(self): | |
def __init__(self, m, n): | |
self.m = m | |
self.n = n | |
def print_matrix(self, a): | |
for c in range(len(a)): | |
for d in range(len(a)): | |
print(a[c][d], end=', ') | |
print() | |
print('-------------\n') | |
def convert(self): | |
m = self.m | |
n = self.n | |
b = [[0 for i in range(m)] for j in range(n)] | |
t = 0 | |
x = 0 | |
for i in range(m): | |
for j in range(n): | |
if (i == x and ((i == 0 and j == 0) or (x <= j < n - x - 1))) or (n % 2 != 0 and i == j and j == (n // 2)): | |
t += 1 | |
b[i][j] = t | |
if j == n - x - 1: | |
for k in range(x, n - x): | |
if k != n - x - 1: | |
t += 1 | |
b[k][j] = t | |
if k == n - x - 1: | |
for l in reversed(range(x + 1, n - x)): | |
t += 1 | |
b[k][l] = t | |
for p in reversed(range(x + 1, n - x)): | |
t += 1 | |
b[p][n - k - 1] = t | |
x += 1 | |
self.print_matrix(b) | |
if __name__ == '__main__': | |
print('Enter the number of columns or rows: ', end='') | |
n = int(input()) | |
m = int(input()) | |
# m = n | |
# n = 4 | |
# m = 6 | |
matrix = Matrix(m, n) | |
matrix.convert() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment