-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionaryMaster_4.py
More file actions
87 lines (64 loc) · 2.24 KB
/
Copy pathdictionaryMaster_4.py
File metadata and controls
87 lines (64 loc) · 2.24 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
students = [
{"name": "Bhoomi", "subject": "Math", "marks": 85},
{"name": "Priya", "subject": "Physics", "marks": 92},
{"name": "Aman", "subject": "Math", "marks": 78},
{"name": "Riya", "subject": "Chemistry", "marks": 88},
{"name": "Karan", "subject": "Physics", "marks": 65},
{"name": "Neha", "subject": "Math", "marks": 91}
]
#grouping marks per subject
def marks_per_sub(students):
groups = {}
for s in students:
subject = s["subject"]
if subject not in groups:
groups[subject] = []
groups[subject].append(s["marks"])
return groups
#calculating avg marks per subject
groups = marks_per_sub(students)
def avg_marks_per_sub(groups):
avg_result = {}
for subjects,marks_list in groups.items():
total = sum(marks_list)
count = len(marks_list)
avg_result[subjects] = round(total/count,2)
return avg_result
#calculating students per subject
def students_per_sub(students):
groups = {}
for s in students:
subject = s["subject"]
if subject not in groups:
groups[subject] = []
groups[subject].append((s["marks"],(s["name"])))
return groups
stu_grp = students_per_sub(students)
#calculating topper per subject
def topper_per_sub(stu_grp):
topper_dict = {}
for subjects,records in stu_grp.items():
top_marks, top_name = max(records)
topper_dict[subjects] = (top_name,top_marks)
return topper_dict
topper_dict = topper_per_sub(stu_grp)
#calculating overall topper
def overall_topper(topper_dict):
return max(topper_dict.values(),key = lambda x: x[1])
#calculating subjet wise students
def subject_wise_students(students):
groups = {}
for s in students:
subject = s["subject"]
groups.setdefault(subject, []).append(s["name"])
return groups
groups = marks_per_sub(students)
avg_per_subject = avg_marks_per_sub(groups)
stu_grp = students_per_sub(students)
toppers = topper_per_sub(stu_grp)
overall = overall_topper(toppers)
students_grouped = subject_wise_students(students)
print("Average Marks per Subject:", avg_per_subject)
print("Topper per Subject:", toppers)
print("Overall Topper:", overall)
print("Students Grouped by Subject:", students_grouped)