Always Prefer Python...

Python Version of String Game Problem:-

T=int(input("Enter the number of Test Cases: "))
from collections import Counter as Cn
while(T):
    n=int(input("Enter the length of the string: "))
    s=input("Enter the string which needs of be divided: ")
    d1=Cn(s)
    chk=0
    if(n%2!=0):
        print("NO")
        T-=1
        continue
    for freq in d1.values():
        if(freq%2!=0):
            print("NO")
            chk=1
            break
    if(chk==1):
        T-=1
        continue
    print("YES")
    
    T-=1

String Game Problem Of CodeChef, Cpp version, still needs work on debugging the logical error.

#include <iostream>
#include<vector>
using namespace std;
/* String Game */
int main() {
    int T,n;
    // cout<<"Enter the number of Test Cases.\n";
    cin>>T;
    while(T>0){
        // cout<<"Enter the length of the string.\n";
        cin>>n;
        
        // If the string is of odd length then it can't be distributed equally in two parts, so cout no
        if(n%2!=0){
            cout<<"NO"<<endl;
            T--;
            continue;
        }
        // cout<<"Enter the string which needs to be divided\n";
        
        string s;
        cin>>s;
        vector<int> freq(n);
        int fin_chk=0;
        for(int i=0;i<n;i++){
            
            int chk=0;
            if(i!=0){
                // Will check if we have already visited this char and so will not count again
                for(int k=i-1;k>=0;k--){
                    if(s[k]==s[i]){
                        // The char had been visited
                      chk=1;
                      break;
                    }
                }
                if(chk==1){
                    // Don't do anything for this char
                    continue;
                }
            }
            if(i==n-1){
                // cout<<"NO"<<endl;
                fin_chk=1;
                break;
            }
            freq[i]=1;
            for(int j=i+1;j<n;j++){
                if(s[j]==s[i]){
                    freq[i]+=1;
                }
            }
            if(freq[i]%2!=0){
                // cout<<"NO"<<endl;
                fin_chk=1;
                T--;
                break;
            }
        }
        if(fin_chk==0){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
        T--;
    }
    return 999;

} 

When I made the code for the above problem("String Game") in Python, it worked fine, and this is one of the cases when we can use Python, as this was the best possible case to make use of the Counter module of Python to get the frequencies of the characters in the passed string, which was a bit complex in cpp when compared to Python, because here we just get the frequencies in 1 line by the use of the Counter Constructor, we don't have to iterate through the whole string and manage all the possible corner cases of the characters repeating in the string unlike in CPP. This is what makes Python beautiful, and is perfect for such type of Conditions, although I am going to make a new code for this problem in cpp again, and hopefully it works this time.

#include <iostream>
using namespace std;
/** Attempt No.-2 String Game- CPP Version **/
/** Time Limit Exceeded **/
int main() {
    int T,n;
    // cout<<"What's T? ";
    cin>>T;
    // cout<<endl;
    while(T>0){
        // cout<<"What's n? ";
        cin>>n;
        // cout<<endl;
        int chk=0;
        string s;
        // cout<<"What's s? ";
        cin>>s;
        // cout<<endl;
        if(n%2!=0){
            cout<<"NO"<<endl;
            T--;
            continue;
        }
        for(int i=0;i<n;i++){
            int freq=1;
            if(i>0){
                for(int j=i-1;j>=0;j--){
                    if(s[j]==s[i]){
                        freq+=1;
                    }
                }    
            }
            if(i!=n-1){
                for(int k=i+1;k<n;k++){
                    if(s[k]==s[i]){
                        freq++;   
                    }
                }    
            }
            if(freq%2!=0){
                cout<<"NO"<<endl;
                chk=1;
                break;
            }
        }
        if(chk==1){
            T--;
            continue;
        }
        cout<<"YES"<<endl;
        T--;
    }
    return 0;
}

Football Problem of HackerRank:-

Explanation...

#include <iostream>
#include<vector>
using namespace std;
/** Football Problem of CodeChef **/
int main() {
    
    int T,n;
    cout<<"Enter the number of Test Cases please!\n";
    cin>>T;
    while(T>0){
        cout<<"Now enter the number of players(n)?\n";
        cin>>n;
        vector<int> points(n);
        vector<int> A(n);
        vector<int> B(n);
        cout<<"Enter the goals scored by the players respectively.\n";
        for(int i=0;i<n;i++){
            // Taking input for goals vector
            // int temp;
            // cin>>temp;
            // A.push_back(temp);
            cin>>A[i];
            points[i]+=A[i]*20;
        }
        int ind=-1;
        int max=-1;
        cout<<"Enter the number of fouls by each player in order.\n";
        for(int i=0;i<n;i++){
            // int temp;
            // cin>>temp;
            // B.push_back(temp);
            cin>>B[i];
            points[i]-=B[i]*10;
            if(points[i]<0){
                // The points of a player will never be less than 0
                points[i]=0;
            }
            if(points[i]>max){
                ind=i;
                max=points[i];
            }
        }
        /* Displaying the points of the player who has the 
           maximum number of points */
        cout<<"The maximum number of points secured are "<<max<<" by "
        "the "<<ind+1<<((ind+1==1)?"st":(ind+1==2)?"nd":(ind+1==3)?""
        "rd":(ind+1>3)?"th":"Nothing")<<" player."<<endl;
        // for(int i=0;i<n;i++){
        //     if(points[])
        // }
        T--;
    }
    return 0;
}

Comments

Popular Posts