gudii9
asked on
mapAB2 errors
http://codingbat.com/prob/p115011
For above challenge i wrote as below
please advise on any modifications, improvement to the code
above passes all tests but challenge description did not mention a and b values cannot be null or can be null etc?
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;
}
Expected Runi am failing one edge case. how to fix it?
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
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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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.
ASKER
getting to all of them
ASKER