2
I have a txt input file with data type the below:
7 6 8
4 8 5
0 1 2
1 0 3
2 3 0
This file is about information for 3 students of a school. The first line is the age of these 3 students (the first column could be aluno1, the second aluno2 and the third aluno3). The second line is the grade of the tests of these three students and line 3 until 5 corresponds to a matrix with the distance of these students in portfolios. (For example, in row three: first column - the distance from aluno1 to aluno1 is 0. Second column - the distance from aluno1 to aluno2 is 1 wallet. Third column - distance from student to student 2. Same idea for 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 minor to the adult of the students and that the other lines follow this ordering.
Therefore, the program should return:
6 7 8
8 4 5
0 1 3
1 0 2
3 2 0
So far I have managed to make a code that ordered only the first line, without connection to the second line and the matrix. The code follows below:
#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 someone help me complete the code or maybe help me with new ideas?
Thank you very much!
NOTE: txt file does not have blank lines between rows and columns are separated by 2 spaces.
– fnands
But ordination has to be specifically with Bubble Sort ? This is part of some exercise ?
– Isac