from dataclasses import dataclass @dataclass class Student: name: str surname: str mathematics: int physics: int informatics: int def rate(self): return (self.mathematics + self.physics + self.informatics) / 3.0 def read_student(line: str) -> Student: fields = line.split(" ") return Student( name=fields[1], surname=fields[0], mathematics=int(fields[2]), physics=int(fields[3]), informatics=int(fields[4]) ) def main(): students = [] with open("students.txt", 'r', encoding='UTF-8') as students_file: lines_count = int(students_file.readline()) for student in students_file: students.append(read_student(student)) students.sort(key=lambda student: student.rate(), reverse=True) for student in students: print("{surname} {name}".format(name=student.name, surname=student.surname)) if __name__ == "__main__": main()