Given a file named student.txt which has the information of the students in a class such as Name, Roll number, Age, CGPA.
Write a pseudocode and the subsequent Python program to do the following operations.
a) Read the file and print the contents of the file
b) Read the file and sort the contents (records) of the file in an increasing order of CGPA and print the content of the sorted file.
c) Read the file and print the record of the student with maximum age and minimum age.
d) Read the file and print the average CGPA of the class.
e) Read the file and print the name of the students whose CGPA is greater than the average CGPA of the class and whose age lies between average age of the class and the minimum age of the students in the class
Input Format
Read the name of the input file
Details of the students with the fields : Name, Rollnumber, Age, CGPA separated by a comma.
Input:
Read the name of the input file
Details of the students with the fields : Name, Rollnumber, Age, CGPA separated by a comma.
Processing:
name = input().rstrip()
def read_print():
grade =
open(name)
for i in
grade.readlines():
print(i,end='')
print()
grade.close()
def sort_file():
stud_list = []
grade = open(name)
for i in
grade.readlines():
stud_list.append(i.rstrip().split(','))
cgpa = {}
for i in
stud_list:
cgpa.setdefault(eval(i[-1]),[])
cgpa[eval(i[-1])].append(i)
for i in
cgpa.keys():
cgpa[i].sort(key = lambda name:name[0])
for i in
reversed(sorted(cgpa.keys())):
for j in
cgpa[i]:
print(','.join(j))
stud_list.sort(key
= lambda x:eval(x[-2]))
max_age = []
for i in
range(len(stud_list)-1,-1,-1):
if
stud_list[i][-2] == stud_list[-1][-2]:
max_age.append(stud_list[i])
else:
break
max_age.sort(key =
lambda x:x[0])
min_age = []
for i in
range(len(stud_list)):
if
stud_list[i][-2] == stud_list[0][-2]:
min_age.append(stud_list[i])
else:
break
min_age.sort(key =
lambda x:x[0])
for i in max_age:
print(','.join(i))
for i in min_age:
print(','.join(i))
stud_list.sort(key
= lambda x:eval(x[-1]),reverse = True)
sum = 0
age = 0
for i in
stud_list:
age +=
eval(i[-2])
sum +=
eval(i[-1])
avg_grade =
sum/len(stud_list)
avg_age =
age/len(stud_list)
print(round(avg_grade,2))
above_avg = []
for i in
stud_list:
if
float(i[-1]) > avg_grade and float(i[-2]) < avg_age:
above_avg.append(i)
above_avg.sort()
for i in
above_avg:
print(i[0])
grade.close()
read_print()
sort_file()
Output:
a) Print the contents of the file with each field of a records separated by a comma.
(b) Print the contents of file after sorting the records based on the CGPA, in the increasing order of the CGPA. If there are students with the same CGPA, Print in the order of their name.
(c) Print the details of the student with maximum age and Print the details of the student with minimum age. If there are more than one student with the maximum age (or minimum age), your program should print all the student’s details in the increasing order of their name.
(d) Print the average CGPA of the class.
(e) Print the name of the student whose CGPA is greater than the average CGPA of the class and whose age lies between average age of the class and the minimum age of the students in the class
Program:
name = input().rstrip()
def read_print():
grade =
open(name)
for i in
grade.readlines():
print(i,end='')
print()
grade.close()
def sort_file():
stud_list = []
grade = open(name)
for i in
grade.readlines():
stud_list.append(i.rstrip().split(','))
cgpa = {}
for i in
stud_list:
cgpa.setdefault(eval(i[-1]),[])
cgpa[eval(i[-1])].append(i)
for i in
cgpa.keys():
cgpa[i].sort(key = lambda name:name[0])
for i in
reversed(sorted(cgpa.keys())):
for j in
cgpa[i]:
print(','.join(j))
stud_list.sort(key
= lambda x:eval(x[-2]))
max_age = []
for i in
range(len(stud_list)-1,-1,-1):
if
stud_list[i][-2] == stud_list[-1][-2]:
max_age.append(stud_list[i])
else:
break
max_age.sort(key =
lambda x:x[0])
min_age = []
for i in
range(len(stud_list)):
if
stud_list[i][-2] == stud_list[0][-2]:
min_age.append(stud_list[i])
else:
break
min_age.sort(key =
lambda x:x[0])
for i in max_age:
print(','.join(i))
for i in min_age:
print(','.join(i))
stud_list.sort(key
= lambda x:eval(x[-1]),reverse = True)
sum = 0
age = 0
for i in
stud_list:
age +=
eval(i[-2])
sum +=
eval(i[-1])
avg_grade =
sum/len(stud_list)
avg_age =
age/len(stud_list)
print(round(avg_grade,2))
above_avg = []
for i in
stud_list:
if
float(i[-1]) > avg_grade and float(i[-2]) < avg_age:
above_avg.append(i)
above_avg.sort()
for i in
above_avg:
print(i[0])
grade.close()
read_print()
sort_file()
Pseudocode:
Step1. Get the name of the file.
Step2. Display the contents of the file with each field of a records separated by a comma using read_print function
Step3. Display the contents of file after sorting the records based on the CGPA, in the increasing order of the CGPA. If there are students with the same CGPA, Print in the order of their name. This is done using sort_file function
Step4. Display the details of the student with maximum age and Print the details of the student with minimum age. If there are more than one student with the maximum age (or minimum age), your program should print all the student’s details in the increasing order of their name. This is done using sort_file function
Step5.Display the average CGPA of the class
Step6. Display the name of the student whose CGPA is greater than the average CGPA of the class and whose age lies between average age of the class and the minimum age of the students in the class.This is done using sort_file function
Step7.End