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

asked on

mapAB2 errors

http://codingbat.com/prob/p115011

For above challenge i wrote as below
public Map<String, String> mapAB2(Map<String, String> map) {
  if( map.get("a").equals(map.get("b")) ){
    map.remove("a");
    map.remove("b");
  }
  return map;
}

Open in new window


Expected      Run            
mapAB2({"a": "aaa", "b": "aaa", "c": "cake"}) → {"c": "cake"}      {"c": "cake"}      OK      
mapAB2({"a": "aaa", "b": "bbb"}) → {"a": "aaa", "b": "bbb"}      {"a": "aaa", "b": "bbb"}      OK      
mapAB2({"a": "aaa", "b": "bbb", "c": "aaa"}) → {"a": "aaa", "b": "bbb", "c": "aaa"}      {"a": "aaa", "b": "bbb", "c": "aaa"}      OK      
mapAB2({"a": "aaa"}) → {"a": "aaa"}      {"a": "aaa"}      OK      
mapAB2({"b": "bbb"}) → {"b": "bbb"}      NullPointerException (line:2)      X      
mapAB2({"a": "", "b": "", "c": "ccc"}) → {"c": "ccc"}      {"c": "ccc"}      OK      
mapAB2({}) → {}      NullPointerException (line:2)      X      
mapAB2({"a": "a", "b": "b", "z": "zebra"}) → {"a": "a", "b": "b", "z": "zebra"}      {"a": "a", "b": "b", "z": "zebra"}      OK      
other tests
X      
i am failing one edge case. how to fix it?
please advise on any modifications, improvement to the code

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

Open in new window


above passes all tests but challenge description did not mention a and b values cannot be null or can be null etc?
Modify and return the given map as follows: if the keys "a" and "b" are both in the map and have equal values, remove them both.

mapAB2({"a": "aaa", "b": "aaa", "c": "cake"}) → {"c": "cake"}
mapAB2({"a": "aaa", "b": "bbb"}) → {"a": "aaa", "b": "bbb"}
mapAB2({"a": "aaa", "b": "bbb", "c": "aaa"}) → {"a": "aaa", "b": "bbb", "c": "aaa"}
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America 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

let me close
Avatar of gudii9

ASKER

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

Open in new window


Expected      Run            
mapAB2({"a": "aaa", "b": "aaa", "c": "cake"}) → {"c": "cake"}      {"c": "cake"}      OK      
mapAB2({"a": "aaa", "b": "bbb"}) → {"a": "aaa", "b": "bbb"}      {"a": "aaa", "b": "bbb"}      OK      
mapAB2({"a": "aaa", "b": "bbb", "c": "aaa"}) → {"a": "aaa", "b": "bbb", "c": "aaa"}      {"a": "aaa", "b": "bbb", "c": "aaa"}      OK      
mapAB2({"a": "aaa"}) → {"a": "aaa"}      {"a": "aaa"}      OK      
mapAB2({"b": "bbb"}) → {"b": "bbb"}      {"b": "bbb"}      OK      
mapAB2({"a": "", "b": "", "c": "ccc"}) → {"c": "ccc"}      {"c": "ccc"}      OK      
mapAB2({}) → {}      {}      OK      
mapAB2({"a": "a", "b": "b", "z": "zebra"}) → {"a": "a", "b": "b", "z": "zebra"}      {"a": "a", "b": "b", "z": "zebra"}      OK      
other tests
OK      

above passes all
Thanks for closing this question. Currently, you have over 30 open questions.
Avatar of gudii9

ASKER

getting to all of them