Corrected logic error in the solution.
#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);
- }
// TODO: Replace examples and use TDD development by writing your own tests Describe(Tests) { It(Test_1) { Assert::That(verify_sum("Sebastian","Patricia"), Equals(false)); } It(Test_2) { Assert::That(verify_sum("Anna","Nana"), Equals(true)); } It(Test_3) { Assert::That(verify_sum("Rohan","Johnny"), Equals(false)); } It(Test_4) { Assert::That(verify_sum("Mark",""), Equals(false)); } It(Test_5) { Assert::That(verify_sum("",""), Equals(true)); } };
- // TODO: Replace examples and use TDD development by writing your own tests
- Describe(Tests)
- {
- It(Test_1)
- {
- Assert::That(verify_sum("Sebastian","Patricia"), Equals(false));
- }
- It(Test_2)
- {
- Assert::That(verify_sum("Anna","Nana"), Equals(true));
- }
- It(Test_3)
- {
- Assert::That(verify_sum("Rohan","Johnny"), Equals(false));
- }
- It(Test_4)
- {
Assert::That(verify_sum("Mark",""), Equals(true));- Assert::That(verify_sum("Mark",""), Equals(false));
- }
- It(Test_5)
- {
- Assert::That(verify_sum("",""), Equals(true));
- }
- };
int returnhundred() { return 'd'; }
function returnhundred(){return 'd'.charCodeAt();}- int returnhundred()
- {
- return 'd';
- }
// TODO: Replace examples and use TDD development by writing your own tests Describe(Tests) { It(Test_1) { Assert::That(returnhundred(), Equals(100)); } };
Test.assertEquals(returnhundred(),100)- // TODO: Replace examples and use TDD development by writing your own tests
- Describe(Tests)
- {
- It(Test_1)
- {
- Assert::That(returnhundred(), Equals(100));
- }
- };
Cleaned up C++ solution with test cases.
#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");elseprintf("False\n");- return ((sum[0]-sum[1]) == 0);
- }
return 0;}
// TODO: Replace examples and use TDD development by writing your own tests Describe(Tests) { It(Test_1) { Assert::That(verify_sum("Sebastian","Patricia"), Equals(false)); } It(Test_2) { Assert::That(verify_sum("Anna","Nana"), Equals(true)); } It(Test_3) { Assert::That(verify_sum("Rohan","Johnny"), Equals(false)); } It(Test_4) { Assert::That(verify_sum("Mark",""), Equals(true)); } It(Test_5) { Assert::That(verify_sum("",""), Equals(true)); } };
- // TODO: Replace examples and use TDD development by writing your own tests
- Describe(Tests)
- {
- It(Test_1)
- {
- Assert::That(verify_sum("Sebastian","Patricia"), Equals(false));
- }
- It(Test_2)
- {
- Assert::That(verify_sum("Anna","Nana"), Equals(true));
- }
- It(Test_3)
- {
- Assert::That(verify_sum("Rohan","Johnny"), Equals(false));
- }
- It(Test_4)
- {
- Assert::That(verify_sum("Mark",""), Equals(true));
- }
- It(Test_5)
- {
- Assert::That(verify_sum("",""), Equals(true));
- }
- };
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.
#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;
- }