Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

mapAB4 edge cases

while trying below challenge
http://codingbat.com/prob/p136950

i wrote my code as below

public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
    
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
    
  }
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


i am failing below tests
Expected      Run            
mapAB4({"a": "aaa", "b": "bb", "c": "cake"}) → {"a": "aaa", "b": "bb", "c": "aaa"}      {"a": "aaa", "b": "bb", "c": "aaa"}      OK      
mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aaa"}) → {"a": "aaa"}      {"a": "aaa"}      OK      
mapAB4({"b": "bbb"}) → {"b": "bbb"}      {"b": "bbb"}      OK      
mapAB4({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "aaa", "b": "bbb", "c": "cake"}      X      
mapAB4({"a": "a", "b": "b", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "a", "b": "b", "c": "cake"}      X      
mapAB4({"a": "", "b": "b", "c": "cake"}) → {"a": "", "b": "b", "c": "b"}      {"a": "", "b": "b", "c": "b"}      OK      
mapAB4({"a": "a", "b": "", "c": "cake"}) → {"a": "a", "b": "", "c": "a"}      {"a": "a", "b": "", "c": "a"}      OK      
mapAB4({"c": "cat", "d": "dog"}) → {"c": "cat", "d": "dog"}      {"c": "cat", "d": "dog"}      OK      
mapAB4({"ccc": "ccc"}) → {"ccc": "ccc"}      {"ccc": "ccc"}      OK      
mapAB4({"c": "a", "d": "b"}) → {"c": "a", "d": "b"}      {"c": "a", "d": "b"}      OK      
mapAB4({}) → {}      {}      OK      
mapAB4({"a": "", "z": "z"}) → {"a": "", "z": "z"}      {"a": "", "z": "z"}      OK      
mapAB4({"b": "", "z": "z"}) → {"b": "", "z": "z"}      {"b": "", "z": "z"}      OK      
other tests
OK      
Correct for more than half the tests

Your progress graph for this problem
how to improve my code and fix the tests.

please advise
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
    
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
    
  }
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()==map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


above passed all.
how to write junit test cases for this?
Avatar of gudii9

ASKER

  if ( map.get("a")!=null && map.get("b")!=null && map.get("a").length() == map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  
  if ( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }

Open in new window


does the order matters liek above or like below


  if ( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  if ( map.get("a")!=null && map.get("b")!=null && map.get("a").length() == map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  

Open in new window

provided answer passes all conditions...