Ad

Corrected logic error in the solution.

Code
Diff
  • #include <string>
    
    using std::string;
    
    
    bool verify_sum(string name1,string name2)
    {
      
      int sum[2] = {0,0};
      
      
      if(name1.empty() && name2.empty())
          return true;                  // Expected behaviour for both strings empty is "true"
          
      else if(!name1.empty() && !name2.empty())
      {
        for(int i=0;i<name1.size();i++)
          {
              sum[0] += name1[i] ;   
          }
        for(int i=0;i<name2.size();i++)
          {
              sum[1] += name2[i] ;   
          }
      }
      else
        return false;
        
        return ((sum[0]-sum[1]) == 0);
    }
    
    
    • #include <string>
    • using std::string;
    • bool verify_sum(string name1,string name2)
    • {
    • int sum[2] = {0,0};
    • if(name1.empty() || name2.empty())
    • return true; // Expected behaviour for empty strings is "true"
    • for(int i=0;i<name1.size();i++)
    • {
    • sum[0] += name1[i] ;
    • }
    • for(int i=0;i<name2.size();i++)
    • {
    • sum[1] += name2[i] ;
    • }
    • if(name1.empty() && name2.empty())
    • return true; // Expected behaviour for both strings empty is "true"
    • else if(!name1.empty() && !name2.empty())
    • {
    • for(int i=0;i<name1.size();i++)
    • {
    • sum[0] += name1[i] ;
    • }
    • for(int i=0;i<name2.size();i++)
    • {
    • sum[1] += name2[i] ;
    • }
    • }
    • else
    • return false;
    • return ((sum[0]-sum[1]) == 0);
    • }
Code
Diff
  • int returnhundred()
    {
      return 'd';
    }
    
    
    • function returnhundred(){return 'd'.charCodeAt();}
    • int returnhundred()
    • {
    • return 'd';
    • }

Cleaned up C++ solution with test cases.

Code
Diff
  • #include <string>
    
    using std::string;
    
    
    bool verify_sum(string name1,string name2)
    {
      
      int sum[2] = {0,0};
      
      
      if(name1.empty() || name2.empty())
          return true;                   // Expected behaviour for empty strings is "true"
    
      for(int i=0;i<name1.size();i++)
        {
            sum[0] += name1[i] ;   
        }
      for(int i=0;i<name2.size();i++)
        {
            sum[1] += name2[i] ;   
        }
       
        return ((sum[0]-sum[1]) == 0);
    }
    
    
    • #include <string>
    • using std::string;
    • bool verify_sum(std::string name1,std::string name2)
    • bool verify_sum(string name1,string name2)
    • {
    • int i,sum1=0,sum2=0;
    • if(name1.size() == 0 || name2.size() == 0)
    • return true;
    • int sum[2] = {0,0};
    • if(name1.empty() || name2.empty())
    • return true; // Expected behaviour for empty strings is "true"
    • for(i=0;i<name1.size();i++)
    • for(int i=0;i<name1.size();i++)
    • {
    • sum1 += name1[i] ;
    • sum[0] += name1[i] ;
    • }
    • for(i=0;i<name2.size();i++)
    • for(int i=0;i<name2.size();i++)
    • {
    • sum2 += name2[i] ;
    • sum[1] += name2[i] ;
    • }
    • return ((sum1-sum2) == 0);
    • }
    • int main()
    • {
    • int i;
    • std::string store[10] = {"Sebastian","Patricia","Anna","Nana","John"};
    • // I don't really know how to do unit testing on C++ :(
    • for(i=0;i<4;i++)
    • {
    • if(verify_sum(store[i],store[i+1]) == true)
    • printf("True\n");
    • else
    • printf("False\n");
    • return ((sum[0]-sum[1]) == 0);
    • }
    • return 0;
    • }

This is a simple solution in C++ as I noticed there were no C++ solutions. I do not know unit testing in C++ so, I manullay call these functions from main function.

Code
Diff
  • #include <string>
    
    
    
    bool verify_sum(std::string name1,std::string name2)
    {
      int i,sum1=0,sum2=0;
      
      if(name1.size() == 0 || name2.size() == 0)
          return true;
        
    
    
     
      for(i=0;i<name1.size();i++)
        {
            sum1 += name1[i] ;   
        }
      for(i=0;i<name2.size();i++)
        {
            sum2 += name2[i] ;   
        }
       
        return ((sum1-sum2) == 0);
    }
    
    int main()
    {
    int i;
    std::string store[10] = {"Sebastian","Patricia","Anna","Nana","John"};
    
    
    // I don't really know how to do unit testing on C++ :(
    
    
    for(i=0;i<4;i++)
    {
        if(verify_sum(store[i],store[i+1]) == true)
          printf("True\n");
      else 
          printf("False\n");
    }
    
      return 0;
    }
    • verify_sum = lambda a, b: sum(map(ord, a)) == sum(map(ord, b)) if a and b else None
    • #include <string>
    • bool verify_sum(std::string name1,std::string name2)
    • {
    • int i,sum1=0,sum2=0;
    • if(name1.size() == 0 || name2.size() == 0)
    • return true;
    • for(i=0;i<name1.size();i++)
    • {
    • sum1 += name1[i] ;
    • }
    • for(i=0;i<name2.size();i++)
    • {
    • sum2 += name2[i] ;
    • }
    • return ((sum1-sum2) == 0);
    • }
    • int main()
    • {
    • int i;
    • std::string store[10] = {"Sebastian","Patricia","Anna","Nana","John"};
    • // I don't really know how to do unit testing on C++ :(
    • for(i=0;i<4;i++)
    • {
    • if(verify_sum(store[i],store[i+1]) == true)
    • printf("True\n");
    • else
    • printf("False\n");
    • }
    • return 0;
    • }