Problem:- Given an array of Student objects, sort array using a comparator that sorts them in order of decreasing marks; if or more students have the same marks, sort those students alphabetically by name.
Input :- Array of Student objects(Student object { Name; Marks;})
amy 90
ckm 90
nikhil 50
shweta 75
ranjan 150
Output:- Sort based on Marks (Name alphabetically if marks is same)
ranjan 150
amy 90
ckm 90
shweta 75
nikhil 50
Sample output:-
>>>
Enter number of students
5
Enter Name and marks of student 1
amy 90
Enter Name and marks of student 2
ckm 90
Enter Name and marks of student 3
nikhil 50
Enter Name and marks of student 4
shweta 75
Enter Name and marks of student 5
ranjan 150
Students Name and marks in sorted form
Name Marks
----------------
ranjan 150
amy 90
ckm 90
shweta 75
nikhil 50
Input :- Array of Student objects(Student object { Name; Marks;})
amy 90
ckm 90
nikhil 50
shweta 75
ranjan 150
Output:- Sort based on Marks (Name alphabetically if marks is same)
ranjan 150
amy 90
ckm 90
shweta 75
nikhil 50
Sample program to sort list of student objects by passing comparator
class Students: def __init__(self, name, marks): self.name = name self.marks = marks def comparator(a, b): if a.marks != b.marks : if a.marks < b.marks : return 1 elif a.marks > b.marks: return -1 else : return 0 else: if a.name > b.name : return 1 elif a.name < b.name: return -1 else : return 0 print "Enter number of students " n = int(raw_input()) data = [] for i in range(n): print "Enter Name and marks of student %d" %(i+1) name, marks = raw_input().split() marks = int(marks) player = Students(name, marks) data.append(player) data = sorted(data, cmp=Students.comparator) print "\nStudents Name and marks in sorted form " print "Name \t Marks" print "----------------" for i in data: print i.name+ " \t "+ str(i.marks)
Sample output:-
>>>
Enter number of students
5
Enter Name and marks of student 1
amy 90
Enter Name and marks of student 2
ckm 90
Enter Name and marks of student 3
nikhil 50
Enter Name and marks of student 4
shweta 75
Enter Name and marks of student 5
ranjan 150
Students Name and marks in sorted form
Name Marks
----------------
ranjan 150
amy 90
ckm 90
shweta 75
nikhil 50
Tags:
PythonSample
This was a nice post. Thanks for sharing, And do share more post of this sort.
ReplyDeleteEnglish Speaking Classes in Mulund
IELTS Classes in Mulund
German Classes in Mulund
French Classes in Mulund
Spoken English Classes in Chennai
IELTS Coaching in Chennai
English Speaking Classes in Mumbai
IELTS Classes in Mumbai
Spoken English Class in Porur
IELTS Coaching in Anna Nagar
I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai
DeleteI would definitely thank the admin of this blog for sharing this information with us. Waiting for more updates from this blog admin.
ReplyDeleteData Science Course in Chennai
Data Science Training in Chennai
AWS Training in Chennai
Azure Training in Chennai
Cloud Computing Training in Chennai
VMware Training in Chennai
Data Science Training in Velachery
Data Science Training in Tambaram
Data Science Training in Adyar
Đặt vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ giá rẻ 2021
chuyến bay đưa công dân về nước
vé máy bay hà nội -- tokyo vietnam airline
vé máy bay từ đức về sài gòn
thông tin chuyến bay từ canada về việt nam
Giá vé máy bay Hàn Việt Vietjet
khách sạn cách ly tphcm