# Hàm để đọc dữ liệu từ tệp 'data.inp'
def doc_file(ten_file):
hocsinh = []
with open(ten_file, 'r') as file:
n = int(file.readline()) # Đọc số lượng học sinh
for _ in range(n):
line = file.readline().strip() # Đọc từng dòng dữ liệu
ten_diem = line.rsplit(' ', 1) # Tách họ tên và điểm trung bình
ten = ten_diem[0]
diem = float(ten_diem[1])
hocsinh.append((ten, diem))
return hocsinh
# Hàm để tìm học sinh có điểm cao nhất
def timdiemtrungbinhcaonhat(hocsinh):
diemtrungbinhcaonhat = max(hocsinh, key=lambda x: x[1])
return diemtrungbinhcaonhat
# Hàm để sắp xếp danh sách học sinh tăng dần theo điểm trung bình
def sapxepdiemtrungbinhtang(hocsinh):
return sorted(hocsinh, key=lambda x: x[1])
# Hàm để ghi kết quả vào tệp 'data.out'
def ghi_file(ten_file, diemtrungbinhcaonhat, sapxephocsinh):
with open(ten_file, 'w') as file:
# Ghi tên học sinh có điểm cao nhất
file.write(f"{diemtrungbinhcaonhat[0]} {diemtrungbinhcaonhat[1]}\n")
# Ghi danh sách học sinh đã sắp xếp theo tên
for hs in sapxephocsinh:
file.write(f"{hs[0]} {hs[1]:.2f}\n")
def main():
# Đọc dữ liệu từ tệp 'data.inp'
hocsinh = doc_file('data.inp')
# Tìm học sinh có điểm trung bình cao nhất
diemtrungbinhcaonhat = timdiemtrungbinhcaonhat(hocsinh)
# Sắp xếp học sinh theo diem
sapxephocsinh = sapxepdiemtrungbinhtang(hoc xinh)
# Ghi kết quả vào tệp 'data.out'
ghi_file('data.out', diemtrungbinhcaonhat, sapxephocsinh)
# Chạy chương trình
if __name__ == "__main__":
main()
Tìm lỗi sai trong chương trình
By @NguyenHoangHuy
Hãy luôn nhớ cảm ơn và vote 5*
nếu câu trả lời hữu ích nhé!
Bảng tin