Você recebeu 2 nomes.
Verifique se a soma das letras do 1º nome é igual à soma das letras do 2º nome. Se o nome ou sobrenome for nulo, produza NULL.
Por exemplo:
"Ana" e "Nana" "Ana" = 65+110+110+97 = 382 "Nana" = 78+97+110+97 = 382 Resultado: "Anna" e "Nana" => true
"Sebastião" e "Patrícia" => false
"João" e nulo => "NULO"
#include <string> #include <numeric> #include <algorithm> class StringComparer { public: static bool verifySum(const std::string& w1, const std::string& w2) { int sumW1 = sumOfCharacters(w1); int sumW2 = sumOfCharacters(w2); return sumW1 == sumW2; } static int sumOfCharacters(const std::string& word) { return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) { return sum + static_cast<int>(ch); }); } };
- #include <string>
- #include <numeric>
- #include <algorithm>
- class StringComparer {
- public:
- static bool verifySum(const std::string& w1, const std::string& w2) {
- int sumW1 = sumOfCharacters(w1);
- int sumW2 = sumOfCharacters(w2);
std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;- return sumW1 == sumW2;
- }
- static int sumOfCharacters(const std::string& word) {
- return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
- return sum + static_cast<int>(ch);
- });
- }
- };
You have received 2 names.
Verify if the sum of letters of the 1 name is the same as sum of the letters of the 2 name. If the name or surname is null output NULL.
For example:
"Anna" and "Nana"
"Anna" = 65+110+110+97 = 382
"Nana" = 78+97+110+97 = 382
Result: "Anna" and "Nana" => "TRUE"
"Sebastian" and "Patricia" => "FALSE"
"John" and null => "NULL"
#include <string> #include <numeric> #include <algorithm> class StringComparer { public: static bool verifySum(const std::string& w1, const std::string& w2) { int sumW1 = sumOfCharacters(w1); int sumW2 = sumOfCharacters(w2); return sumW1 == sumW2; } static int sumOfCharacters(const std::string& word) { return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) { return sum + static_cast<int>(ch); }); } };
- #include <string>
- #include <numeric>
- #include <algorithm>
- class StringComparer {
- public:
- static bool verifySum(const std::string& w1, const std::string& w2) {
- int sumW1 = sumOfCharacters(w1);
- int sumW2 = sumOfCharacters(w2);
std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;- return sumW1 == sumW2;
- }
- static int sumOfCharacters(const std::string& word) {
- return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
- return sum + static_cast<int>(ch);
- });
- }
- };
Você recebeu 2 nomes.
Verifique se a soma das letras do 1º nome é igual à soma das letras do 2º nome. Se o nome ou sobrenome for nulo, produza NULL.
Por exemplo:
"Ana" e "Nana"
"Ana" = 65+110+110+97 = 382
"Nana" = 78+97+110+97 = 382
Resultado: "Anna" e "Nana" => true
"Sebastião" e "Patrícia" => false
"João" e nulo => "NULO"
#include <string> #include <numeric> #include <algorithm> class StringComparer { public: static bool verifySum(const std::string& w1, const std::string& w2) { int sumW1 = sumOfCharacters(w1); int sumW2 = sumOfCharacters(w2); std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl; std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl; return sumW1 == sumW2; } static int sumOfCharacters(const std::string& word) { return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) { return sum + static_cast<int>(ch); }); } };
def verifySum(w1, w2)return false if w1.nil? || w2.nil?w1.downcase().split('').map(&:ord).inject(:+) == w2.downcase().split('').map(&:ord).inject(:+)end- #include <string>
- #include <numeric>
- #include <algorithm>
- class StringComparer {
- public:
- static bool verifySum(const std::string& w1, const std::string& w2) {
- int sumW1 = sumOfCharacters(w1);
- int sumW2 = sumOfCharacters(w2);
- std::cout << "String 1: " << w1 << " - Sum of Characters: " << sumW1 << std::endl;
- std::cout << "String 2: " << w2 << " - Sum of Characters: " << sumW2 << std::endl;
- return sumW1 == sumW2;
- }
- static int sumOfCharacters(const std::string& word) {
- return std::accumulate(word.begin(), word.end(), 0, [](int sum, char ch) {
- return sum + static_cast<int>(ch);
- });
- }
- };
Describe(AStringComparer) { It(ShouldReturnTrueForAnaAndNana) { Assert::That(StringComparer::verifySum("Anna", "Nana"), Equals(true)); } It(ShouldReturnFalseForSebastiaoAndPatricia) { Assert::That(StringComparer::verifySum("Sebastião", "Patrícia"), Equals(false)); } It(ShouldReturnFalseIfOneStringIsEmpty) { Assert::That(StringComparer::verifySum("João", ""), Equals(false)); } It(ShouldReturnTrueForEqualSums) { Assert::That(StringComparer::verifySum("abc", "cba"), Equals(true)); } It(ShouldReturnFalseForNonEqualSums) { Assert::That(StringComparer::verifySum("abc", "def"), Equals(false)); } It(ShouldReturnTrueForResourcefulAndFourscorelu) { Assert::That(StringComparer::verifySum("Resourceful", "Fourscorelu"), Equals(false)); } It(ShouldReturnTrueForConversationAndVoicesrantno) { Assert::That(StringComparer::verifySum("Conversation", "Voicesrantno"), Equals(true)); } It(ShouldReturnTrueForListenAndSilent) { Assert::That(StringComparer::verifySum("Listen", "Silent"), Equals(true)); } It(ShouldReturnFalseForProgrammingAndIntegration) { Assert::That(StringComparer::verifySum("Programming", "Integration"), Equals(false)); } It(ShouldReturnFalseForInnovativeAndIneffective) { Assert::That(StringComparer::verifySum("Innovative", "Ineffective"), Equals(false)); } It(ShouldReturnFalseForComputerAndAlgorithm) { Assert::That(StringComparer::verifySum("Computer", "Algorithm"), Equals(false)); } It(ShouldReturnFalseIfEitherStringIsEmpty) { Assert::That(StringComparer::verifySum("", "abc"), Equals(false)); Assert::That(StringComparer::verifySum("abc", ""), Equals(false)); } }; Describe(SumOfCharactersFunction) { It(ShouldCorrectlySumCharactersForAnna) { StringComparer comparer; Assert::That(comparer.sumOfCharacters("Anna"), Equals(382)); } It(ShouldCorrectlySumCharactersForNana) { StringComparer comparer; Assert::That(comparer.sumOfCharacters("Nana"), Equals(382)); } };
Test.assert_equals(verifySum('Sebastian', 'Patricia'), false);Test.assert_equals(verifySum('Anna', 'Nana'), true);Test.assert_equals(verifySum('John', nil), false);- Describe(AStringComparer) {
- It(ShouldReturnTrueForAnaAndNana) {
- Assert::That(StringComparer::verifySum("Anna", "Nana"), Equals(true));
- }
- It(ShouldReturnFalseForSebastiaoAndPatricia) {
- Assert::That(StringComparer::verifySum("Sebastião", "Patrícia"), Equals(false));
- }
- It(ShouldReturnFalseIfOneStringIsEmpty) {
- Assert::That(StringComparer::verifySum("João", ""), Equals(false));
- }
- It(ShouldReturnTrueForEqualSums) {
- Assert::That(StringComparer::verifySum("abc", "cba"), Equals(true));
- }
- It(ShouldReturnFalseForNonEqualSums) {
- Assert::That(StringComparer::verifySum("abc", "def"), Equals(false));
- }
- It(ShouldReturnTrueForResourcefulAndFourscorelu) {
- Assert::That(StringComparer::verifySum("Resourceful", "Fourscorelu"), Equals(false));
- }
- It(ShouldReturnTrueForConversationAndVoicesrantno) {
- Assert::That(StringComparer::verifySum("Conversation", "Voicesrantno"), Equals(true));
- }
- It(ShouldReturnTrueForListenAndSilent) {
- Assert::That(StringComparer::verifySum("Listen", "Silent"), Equals(true));
- }
- It(ShouldReturnFalseForProgrammingAndIntegration) {
- Assert::That(StringComparer::verifySum("Programming", "Integration"), Equals(false));
- }
- It(ShouldReturnFalseForInnovativeAndIneffective) {
- Assert::That(StringComparer::verifySum("Innovative", "Ineffective"), Equals(false));
- }
- It(ShouldReturnFalseForComputerAndAlgorithm) {
- Assert::That(StringComparer::verifySum("Computer", "Algorithm"), Equals(false));
- }
- It(ShouldReturnFalseIfEitherStringIsEmpty) {
- Assert::That(StringComparer::verifySum("", "abc"), Equals(false));
- Assert::That(StringComparer::verifySum("abc", ""), Equals(false));
- }
- };
- Describe(SumOfCharactersFunction) {
- It(ShouldCorrectlySumCharactersForAnna) {
- StringComparer comparer;
- Assert::That(comparer.sumOfCharacters("Anna"), Equals(382));
- }
- It(ShouldCorrectlySumCharactersForNana) {
- StringComparer comparer;
- Assert::That(comparer.sumOfCharacters("Nana"), Equals(382));
- }
- };