moshkit icon

SortTask

moshkit | PRO | 12/07/19 05:09:17 PM UTC | 0 ⭐ | 465 👁️ | Never ⏰ | []
Python |

900 B

|

None

|

0 👍

/

0 👎

from dataclasses import dataclass
from pprint import pprint
 
 
@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())
    pprint(students)
 
 
if __name__ == "__main__":
    main()

Comments