We help IT Professionals succeed at work.

mapAB3 challenge edge case

Medium Priority
580 Views
Last Modified: 2017-08-27
when i am trying below challenge
http://codingbat.com/prob/p115012

as below
public Map<String, String> mapAB3(Map<String, String> map) {
  if(map.get("a")!=null ){
    
    map.put("b",map.get("a"));
  }
  
   if(map.get("b")!=null){
     map.put("a",map.get("b"));
   }
   return map;
}

Open in new window


public Map<String, String> mapAB3(Map<String, String> map) {
  if(map.get("a")!=null ){
   
    map.put("b",map.get("a"));
  }
 
   if(map.get("b")!=null){
     map.put("a",map.get("b"));
   }
   return map;
}
I am failing one edge case. how to improve my code and fix it. please advise
Comment
Watch Question

Sr. System Analyst
CERTIFIED EXPERT
Commented:
here

public Map<String, String> mapAB3(Map<String, String> map) {

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

  return map;
}

Open in new window

Author

Commented:
public Map<String, String> mapAB3(Map<String, String> map) {
  if(map.get("a")!=null && map.get("b")==null){
    
    map.put("b",map.get("a"));
  }
  
   if(map.get("b")!=null && map.get("a")==null){
     map.put("a",map.get("b"));
   }
   return map;
}

Open in new window

this pass all tests
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
@gudii9

this pass all tests

which is EXACTLY same as above ;)
or do I miss anything here?
HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
provided answer fully solves the problem for all conditions...