Can anyone help me to transfrom the code to PYTHON code? Thanks a lot
Can anyone help me to transfrom the code to PYTHON code? Thanks a lot
#include<bits/stdc++.h>
using namespace std;
void add(string name[],int scores[],string n,int sc)
{
for(int i=0;i<10;i++)
{
if(scores[i]<=sc)
{
for(int j=9;j>i;j--)
{
name[j]=name[j-1];
scores[j]=scores[j-1];
}
name[i]=n;
scores[i]=sc;
return;
}
else if(scores[i]==-1)
{
name[i]=n;
scores[i]=sc;
return;
}
}
}
void print(string name[],int scores[])
{
for(int i=0;i<10;i++)
{
if(scores[i]!=-1)
{
cout<<name[i]<<" "<<scores[i]<<"\n";
}
}
}
int getHscore(string name[],int scores[],string n)
{
int ans=-1;
for(int i=0;i<10;i++)
{
if(scores[i]!=-1)
{
if(name[i]==n)
{
ans=max(ans,scores[i]);
}
}
}
return ans;
}
void removeHscore(string name[],int scores[],string n)
{
for(int i=0;i<10;i++)
{
if(scores[i]!=-1)
{
if(name[i]==n)
{
for(int j=i;j<9;j++)
{
scores[j]=scores[j+1];
name[j]=name[j+1];
}
return;
}
}
}
}
int main()
{
string name[10];
int scores[10];
memset(scores,-1,sizeof(scores));
while(1)
{
cout<<"\nTo add a new player and score enter 1\n";
cout<<"To print the top 10 names and scores to the screen sorted by score with the highest score first enter 2\n";
cout<<"To get a player's highest score enter 3\n";
cout<<"To remove a player's highest score enter 4\n";
cout<<"To quit the program enter any other number\n";
int val;
cin>>val;
if(val==1)
{
string pname;
cout<<"Enter player's name: ";
cin.ignore();
getline(cin,pname);
// cin>>pname;
cout<<"Enter player's score: ";
int pscore;
cin>>pscore;
add(name,scores,pname,pscore);
}
else if(val==2)
{
cout<<"\nList of Players and their scores:\n";
print(name,scores);
cout<<"\n\n";
}
else if(val==3)
{
cout<<"Enter player's name: ";
string pname;
cin.ignore();
getline(cin,pname);
int k=getHscore(name,scores,pname);
if(k==-1)
{
cout<<"\n\nNo such player found\n\n";
}
else
{
cout<<"\n\nhighest score of entered player is "<<k<<"\n\n";
}
}
else if(val==4)
{
cout<<"\nEnter player's name: ";
string pname;
cin.ignore();
getline(cin,pname);
removeHscore(name,scores,pname);
cout<<"\n";
}
else
{
break;
}
}
return 0;
}

Step by step
Solved in 3 steps with 3 images









