gudii9
asked on
mapAB4 edge cases
while trying below challenge
http://codingbat.com/prob/p136950
i wrote my code as below
i am failing below tests
please advise
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;
}
i am failing below tests
Expected Runhow to improve my code and fix the tests.
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
please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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","");
}
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","");
}
provided answer passes all conditions...
ASKER
Open in new window
above passed all.
how to write junit test cases for this?