Thursday, December 7, 2023
HomeSoftware EngineeringExamine Strings by Sum of Chars in Java

Examine Strings by Sum of Chars in Java


The problem

Examine two strings by evaluating the sum of their values (ASCII character code).

  • For evaluating deal with all letters as UpperCase
  • null needs to be handled as empty strings
  • If the string comprises different characters than letters, deal with the entire string as it might be empty

Your technique ought to return true, if the strings are equal and false if they don’t seem to be equal.

Examples:

"AD", "BC"  -> equal
"AD", "DD"  -> not equal
"gf", "FG"  -> equal
"zz1", ""   -> equal (each are thought of empty)
"ZzZz", "ffPFF" -> equal
"kl", "lz"  -> not equal
null, ""    -> equal

The answer in Java code

Choice 1:

public class Answer {
  public static boolean examine(String s1, String s2) {
    if (s1==null || s2==null) return true;
    
    s1 = s1.replaceAll(" ", "").toUpperCase().trim();
    s2 = s2.replaceAll(" ", "").toUpperCase().trim();
    
    if (s1.size()==0 || s2.size()==0) return true;
    
    int s1v = 0;
    int s2v = 0;
    
    for(int i=0;i<s1.size(); i++) {
      if (!Character.isAlphabetic(s1.charAt(i))) return true;
      s1v += (int) s1.charAt(i);
    }
    for(int j=0;j<s2.size(); j++) {
      if (!Character.isAlphabetic(s2.charAt(j))) return true;
      s2v += (int) s2.charAt(j);
    }
    
    return s1v==s2v;
  }
}

Choice 2:

public class Answer {

  public static boolean examine(String s1, String s2)  !s1.matches("[a-zA-Z]+")) s1 = "";
    if (s2 == null 
}

Choice 3:

class Answer {
  static boolean examine(String s1, String s2) {
    return (s1 != null && s1.matches("[a-zA-Z]+") ? s1.toUpperCase().chars().sum() : 0)
        == (s2 != null && s2.matches("[a-zA-Z]+") ? s2.toUpperCase().chars().sum() : 0);
  }
}

Check circumstances to validate our answer

import org.junit.Check;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTests {
    @Check
    public void BasicTests() {
        assertEquals("'AD' vs 'BC'", true, Answer.examine("AD", "BC"));
        assertEquals("'AD' vs 'DD'", false, Answer.examine("AD", "DD"));
        assertEquals("'gf' vs 'FG'", true, Answer.examine("gf", "FG"));
        assertEquals("'Advert' vs 'DD'", false, Answer.examine("Advert", "DD"));
        assertEquals("'zz1' vs ''", true, Answer.examine("zz1", ""));
        assertEquals("'ZzZz' vs 'ffPFF'", true, Answer.examine("ZzZz", "ffPFF"));
        assertEquals("'kl' vs 'lz'", false, Answer.examine("kl", "lz"));
        assertEquals("'[null]' vs ''", true, Answer.examine(null, ""));
        assertEquals("'!!' vs '7476'", true, Answer.examine("!!", "7476"));
        assertEquals("'##' vs '1176'", true, Answer.examine("##", "1176"));
    }
    
    @Check
    public void RandomTests() {
      for(int i=0; i < 40; i++) {
        char letter1 = (char)(Math.random() * 26 + 65);
        char letter2 = '1';
  
        do {
          letter2 = (char)(Math.random() * 26 + 65);
        }
        whereas(letter1 == letter2);
    
        if(Math.random() < 0.5) {
          assertEquals(true, Answer.examine(new String(new char[5]).exchange('',letter1).toUpperCase() + letter2, letter2 + new String(new char[5]).exchange('',letter1).toLowerCase()));
        }
        if(Math.random() < 0.5) {
          assertEquals(false, Answer.examine(new String(new char[4]).exchange('', letter1).toUpperCase() + letter2 + letter2, letter2 + new String(new char[4]).exchange('', letter1).toLowerCase()));
        }
      }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments