gudii9
asked on
mapBully challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p197888
how below got below result
mapBully({"a": "candy"}) → {"b": "candy", "a": ""}
there is no b in the input map right then how it came in the output map?
I am working on below challenge
http://codingbat.com/prob/p197888
[/Map-1 > mapBully
prev | next | chance
Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that value, and set the key "a" to have the value "". Basically "b" is a bully, taking the value and replacing it with the empty string.
mapBully({"b": "dirt", "a": "candy"}) → {"b": "candy", "a": ""}
mapBully({"a": "candy"}) → {"b": "candy", "a": ""}
mapBully({"b": "carrot", "c": "meh", "a": "candy"}) → {"b": "candy", "c": "meh", "a": ""}
how below got below result
mapBully({"a": "candy"}) → {"b": "candy", "a": ""}
there is no b in the input map right then how it came in the output map?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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> mapBully(Map<String, String> map) {
if(map.containsKey("a")){
String val=map.get("a");
String backup=val;
map.put("b",backup);
map.put("a","");
}
return map;
}
above passes all tests. any improvements or alternate approaches?
>> any improvements
Yes.
1. Indent your code decently
2. There's no need to work with those temporary variables
Yes.
1. Indent your code decently
2. There's no need to work with those temporary variables
public Map<String, String> mapBully(Map<String, String> map) {
if (map.containsKey("a")) {
map.put("b", map.get("a"));
map.put("a","");
}
return map;
}
And you could have seen the code in the above post if you'd simply clicked the "Show Solution" button next to the "Go" button on the codingbat webpage you linked to.
ASKER
Open in new window
failing two tests.
please advise