I have an input file in txt with data type the following:
7 6 8
4 8 5
0 1 2
1 0 3
2 3 0
This file is about 3 students from a school. The first line is the age of these 3 students (the first column could be student1, the second student2 and the third student3). The second line is the grade of the tests of these three students and line 3 to 5 corresponds to a matrix with the distance of these students in portfolios. (For example, in row three: first column - the distance from student1 to student1 is 0. Second column - the distance from student1 to student2 is 1 portfolio. Third column - the distance from student1 to student 2. The same idea for the lines 4 and 5.
I need a code that uses bubble sort to sort and read this txt file and sort the information according to the child's highest age and the other lines follow this order.
So the program should return:
6 7 8
8 4 5
0 1 3
1 0 2
3 2 0
So far I've been able to make a code that orders only the first line, with no connection to the second line and array. The code is as follows:
#lendo o arquivo com os dados no modo read.
arquivo = open('alunos.txt','r');
#lê uma linha do arquivo de texto
linha = arquivo.readline()
#Fecha o arquivo de texto
arquivo.close()
#Cria uma lista substituindo os espaços por virgulas
lista = linha.rsplit(" ")
#Determina o tamanho da lista para as condições do bubble sort
tam_entrada = len(lista)
#Bubble Sort
for i in range (tam_entrada):
for j in range (1, tam_entrada-i):
if lista[j] < lista[j-1]:
lista[j], lista[j-1] = lista[j-1], lista[j]
#Imprime a lista depois da utilização do bubble sort
print ("A ordem dos alunos classificados de acordo com a idade é: \n", lista)
Can anyone help me complete the code or maybe help me with new ideas?
Thank you very much!