Skip to content

Instantly share code, notes, and snippets.

@ldelelis
Created Aug 3, 2020
Embed
What would you like to do?
# coding: utf-8
import math
def transpose(matrix):
row_length = len(matrix[0])
transpose_matrix = [[] for _ in range(row_length)]
for i, m1 in enumerate(matrix):
for j, m2 in enumerate(m1):
transpose_matrix[j].append(matrix[i][j])
return transpose_matrix
foo = "devoff se puso atr mono"
length = 4
submatrices_length = (len(foo) // length) + 1
matrix = [[] for _ in range(submatrices_length)]
for i, char in enumerate(foo):
matrix_idx = math.ceil(i // length)
matrix[matrix_idx].append(char)
padding = length - len(matrix[-1])
if padding > 0:
for _ in range(padding):
matrix[-1].append('')
transpose_matrix = transpose(matrix)
real_matrix = transpose(transpose_matrix)
matrix_to_vector = []
for row in real_matrix:
for c in row:
matrix_to_vector.append(c)
print(''.join(matrix_to_vector))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment