Answer the question
In order to leave comments, you need to log in
How to get a matrix from a string by transforming it?
a1 a2 a3 a4 a5 a6
a2 a3 a4 a5 a6 a1
a3 a4 a5 a6 a1 a2
a4 a5 a6 a1 a2 a3
a5 a6 a1 a2 a3 a4
a6 a1 a2 a3 a4 a5
from random import randint
def create_list(row):
a = []
for i in range(row):
a.append(randint(0,20))
return a
def print_list(listname):
for elem in listname:
print('{:3d}'.format(elem), end = ' ')
print()
def list3matrix(listname):
matname = []
matname.append(listname)
for i in range(1,len(listname)):
temp = []
for j in range(0,len(listname)-1):
temp.append(matname[i-1][j+1])
temp.append(matname[i-1][0])
matname.append(temp)
return matname
def list2matrix(listname):
matname = []
matname.append(listname)
for i in range(1,len(listname)):
temp = []
for j in range(0,len(listname)-1):
matname.append(matname[i-1][j+1])
matname.append(matname[i-1][0])
return matname
def print_matrix(matname):
for row in matname:
print()
for elem in row:
print('{:3d}'.format(elem), end = ' ')
print()
n = int(input('Введите количество значений в строке : '))
list1 = create_list(n)
print_list(list1)
matrix1 = list2matrix(list1)
print_matrix(matrix1)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question