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"
class Kata{
public static String verifySum(String nameOne, String nameTwo) {
int[] sumOfNames = new int[]{0, 0};
if (nameOne == null || nameTwo == null) {
return "NULL";
}
for (int i = 0; i < nameOne.length(); i++){
sumOfNames[0] += nameOne.charAt(i);
}
for (int i = 0; i < nameTwo.length(); i++){
sumOfNames[1] += nameTwo.charAt(i);
}
return sumOfNames[0] == sumOfNames[1] ? "TRUE" : "FALSE";
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
@Test
public void testName() {
assertEquals("FALSE", Kata.verifySum("Sebastian", "Patricia"));
assertEquals("TRUE", Kata.verifySum("Anna", "Nana"));
assertEquals("NULL", Kata.verifySum("John", null));
}
}
Sebastian and Patricia want to know if the sum digits of their years is greater than the sum digits of their mother's years?
Example: (27,3,1)
- Mother = 27
- Sebastian = 3
- Patricia = 1
Calculate: (2+7) < (3+1) => false
public class AgeSumDigits
{
public static boolean SumOfDigits(int[] ages) {
int m = ages[0];
int s = ages[1];
int p = ages[2];
// Coding with passion ;)
return (calcSum(m) < calcSum(s)+calcSum(p)) ? true : false;
}
static int calcSum(int age){
int sum = 0;
while(age>0){
sum += age % 10;
age /= 10;
}
return sum;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
AgeSumDigits asd = new AgeSumDigits();
@Test
public void testSumDigits() {
assertEquals(false, asd.SumOfDigits(new int[]{27,3,1}));
assertEquals(true, asd.SumOfDigits(new int[]{33,9,7}));
assertEquals(true, asd.SumOfDigits(new int[]{50,26,24}));
}
}