from operator import itemgetter
for _ in range(int(input())):
n=int(input())
info=[]
val=0
for i in range(n):
lst=list(input().strip().split())
val+=int(lst[2]) # finding the total sum of marks
info.append(lst)
final=[] # for storing the students with marks lower than avg
val=val/n
for i in range(n):
if int(info[i][2])<val:
final.append(info[i])
ans=sorted(final,key=itemgetter(2)) # sort aray with marks as key
for i in ans:
print(i[0],i[1],i[2])
I got stuck here too, I think when we are calculating average we are doing Total marks/n which will give us a float value, instead of that we can compare Total marks and n*current marks.
That is
we can remove this val=val/n and do this
int(info[i][2])*n<val
i got partially correct [20 pts] and was stuck there for about the next hour or so
Here’s my code:
for _ in range(int(input())):
n = int(input())
marks,phone,name = [],[],[]
for _ in range(n):
x,p,m = input().split()
p,m = int(p),int(m)
marks.append(m)
phone.append(p)
name.append(x)
avg = sum(marks)/len(marks)
for i in range(len(marks)):
if(marks[i] < avg):
print(name[i],phone[i],marks[i],sep = " ")
same. I don’t believe that my logic was perfectly sound. Instead, i think that its a weak testcase since my code didn’t give output in the expected order for sample testcase
T = int(input())
for z in range(T):
N = int(input())
details = []
for i in range(N):
name, phone, marks = input().split()
details.append((name, phone, int(marks)))
# Find sum of marks
total = sum(elem[2] for elem in details)
avg = total / N
details.sort(key=lambda x: x[2]) # Sort by score
for student in details:
if student[2] < avg:
print(student[0], student[1], student[2])