@admin add these problems to practice please…, i can’t see a submit(practice) button there yet
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
Can anyone find the mistake in this code?
It is giving SIGSEGV upon executing.
#include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define MOD 1000000007;
using namespace std;
struct student{
string name;
string phone;
int marks;
int order;
};
bool mysort(student &a,student &b){
if(a.marks == b.marks)
return a.order < b.order;
return a.marks < b.marks;
}
int main(){
ios_base :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll tt;
cin >> tt;
while(tt--){
int n;
cin >> n;
vector<student> arr(n);
string sname,number;
int m;
ll total = 0;
for(int i = 0;i < n;i++){
cin >> n >> number >> m;
arr[i].name = sname;
arr[i].phone = number;
arr[i].marks = m;
arr[i].order = i;
total += m;
}
sort(arr.begin(),arr.end(),mysort);
for(int i = 0;i < n;i++){
if(arr[i].marks * n < total)
cout << arr[i].name << " " << arr[i].phone << " " << arr[i].marks << endl;
}
}
return 0;
}
You have taken n as input instead of sname
A silly mistake can cost you 100 points!
Thanks btw.
Here key is string, It should be converted to int
Not sorted as per increasing order of marks
how do we sort it? is there an editorial?
Also,when will the problems be added to the practice section?
Editorial should be avialble by today.
You can have a look here
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])
When will they open CodeMantra for Practice.
but why i am getting sorted output then?
had i known this statement, I could have solved the problem
You can store them in a tuple and use bubble sort here which is O(n^2). It should easily pass.
if anyone wants a c++ code…
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t,n;
cin>>t;
while(t–)
{
cin>>n;
string s;
ll ph[n],marks[n];
float sum=0;
vector<pair<string,ll>>v;
for(int i=0;i<n;i++)
{
cin>>s>>ph[i]>>marks[i];
v.push_back(make_pair(s,marks[i]));
sum+=marks[i];
}
sum=sum/(double)n;
sort(marks,marks+n);
for(int i=0;i<n;i++)
{
if(marks[i]<sum)
{
for(int j=0;j<n;j++)
{
if(v[j].second==marks[i])
{
cout<<v[j].first<<" “<<ph[j]<<” "<<v[j].second<<endl;
v[j].second=-1;
}
}
}
}
}
return 0;
}
You are getting sorted output by string.
Because you are not converting them to int before sorting.
For example,
final = [ ['baban','1234567890','44'] ,['abhichal','9876543210','5'] ]
ans=sorted(final,key=itemgetter(2))
Then ans = [ [‘abhichal’,‘9876543210’,‘44’], [‘baban’,‘1234567890’,‘5’]]. Because 44 is alphabetically smaller then 5.
However, if you write
ans=sorted(final,key=int(itemgetter(2)))
You will get it correctly.
ohk got it
would be nice if you format your codes… (use ``` for basics or visit the discuss page)
thanks : )