MyTetra Share
Делитесь знаниями!
Моя задачка
Время создания: 31.08.2017 21:14
Раздел: Python - Задачник - Матрицы
Запись: xintrea/mytetra_db_mcold/master/base/14982480983uvi4rz8rz/text.html на raw.githubusercontent.com
# coding:utf-8

n = 5 # Число строк
m = 6 # Число столбцов

import random
import os

class matrix:
def __init__(self):
self.n = int(raw_input("Number of lines: "))
self.m = int(raw_input("Number of columns: "))
self.m_min = int(raw_input("Minimum number: "))
self.m_max = int(raw_input("Maximum number: "))
# TODO: reprove max > min
self.a = [[random.randint(self.m_min, self.m_max) for j in range(self.m)] for i in range(self.n)]

def print_matrix(self):
print(self.a)

# function add elements from 1 matrix to another
def __add__(self, other):
# other.print_matrix()
m_res = []
for i in range(len(self.a)):
for j in range(len(other.a[i])):
self.a[i].append(other.a[i][j])

# TODO: function form list of all elements of matrix
def summon(self):
pass

# TODO: function exchange columns
def exchange_columns(self):
"""
Function exchange columns
:return:
"""
#os.system('cls')
m = [] # result matrix
#x = int(raw_input("Number of column: "))
n1 = int(raw_input("Number of column: "))
n1 = n1 +
1 # just for user understand right
if n1 > len(self.a[0]) or n1 < 0:
print("Number of lines not in range of lines matrix")
input()
return
n2 = int(raw_input("Number of column: "))
n2 = n2 +
1 # just for user understand right
if n2 > len(self.a[0]) or n2 < 0:
print("Number of lines not in range of lines matrix")
input()
return
for
i in range(len(self.a)):
m.append(
list())
for j in range(len(self.a[0])):
if j == n1:
# TODO: need to create list - mistake yet
m[i].append(self.a[i][n2])
continue
if
j == n2:
m[i].append(
self.a[i][n1])
continue
m[i].append(self.a[i][j])
self.a = m


def exchange_rows(self):
"""
Function exchange rows
:return:
"""
#os.system('cls')
m = [] # result matrix
n1 = int(raw_input("Number of line: "))
n1 = n1 +
1 # just for user understand right
if n1 > len(self.a) or n1 < 0:
print("Number of lines not in range of lines matrix")
input()
return
n2 = int(raw_input("Number of line: "))
n2 = n2 +
1 # just for user understand right
if n2 > len(self.a) or n2 < 0:
print("Number of lines not in range of lines matrix")
input()
return
for
i in range(len(self.a)):
m.append(
list())
if i == n1:
m[i].append(
self.a[n2])
continue
if
i == n2:
m[i].append(
self.a[n1])
continue
m[i].append(self.a[i])
self.a = m


if __name__ == '__main__':
m = matrix()
m.print_matrix()
#m.exchange_columns()
m.exchange_rows()
m.print_matrix()
 
MyTetra Share v.0.59
Яндекс индекс цитирования