-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingStudent.py
39 lines (31 loc) · 1.25 KB
/
SortingStudent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''
Implement a function called sort_students that takes a list of student objects as input and sorts the
list based on their CGPA (Cumulative Grade Point Average) in descending order. Each student object
has the following attributes: name (string), roll_number (string), and cgpa (float). Test the function
with different input lists of students.
'''
class Student:
def __init__(self, name, roll_number, cgpa):
self.name = name
self.roll_number = roll_number
self.cgpa = cgpa
def sort_students(student_list):
# Sort the list of students in descending order of CGPA
sorted_students = sorted(student_list,
key=lambda student: student.cgpa,
reverse=True)
# Syntax - lambda arg:exp
return sorted_students
# Example usage:
students = [
Student("Hari", "A123", 7.8),
Student("Srikanth", "A124", 8.9),
Student("Saumya", "A125", 9.1),
Student("Mahidhar", "A126", 9.9),
]
sorted_students = sort_students(students)
# Print the sorted list of students
for student in sorted_students:
print("Name: {}, Roll Number: {}, CGPA: {}".format(student.name,
student.roll_number,
student.cgpa))