Solved

# mapBully challenge

Posted on 2016-09-27
234 Views
Hi,

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?
0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 37

Accepted Solution

zzynx earned 250 total points
ID: 41819221
>> there is no b in the input map right then how it came in the output map?

It exactly does what is required:
Modify and return the given map as follows:
1) if the key "a" has a value, set the key "b" to have that value,
{"a": "candy"}  == becomes ==> {"a": "candy", "b": "candy"}
2) and set the key "a" to have the value ""
{"a": "candy", "b": "candy"}  == becomes ==> {"a": "", "b": "candy"}
0

LVL 36

Assisted Solution

mccarl earned 250 total points
ID: 41821042
there is no b in the input map right then how it came in the output map?

0

LVL 7

Author Comment

ID: 41822175
``````public Map<String, String> mapBully(Map<String, String> map) {
String val=map.get("a");
String backup=val;
map.put("b",backup);
map.put("a","");

return map;
}
``````

failing two tests.
Expected      Run
mapBully({"b": "dirt", "a": "candy"}) → {"b": "candy", "a": ""}      {"b": "candy", "a": ""}      OK
mapBully({"a": "candy"}) → {"b": "candy", "a": ""}      {"b": "candy", "a": ""}      OK
mapBully({"b": "carrot", "c": "meh", "a": "candy"}) → {"b": "candy", "c": "meh", "a": ""}      {"b": "candy", "c": "meh", "a": ""}      OK
mapBully({"b": "carrot"}) → {"b": "carrot"}      {"b": null, "a": ""}      X
mapBully({"c": "meh"}) → {"c": "meh"}      {"b": null, "c": "meh", "a": ""}      X
mapBully({"c": "meh", "a": "sparkle"}) → {"b": "sparkle", "c": "meh", "a": ""}      {"b": "sparkle", "c": "meh", "a": ""}      OK
0

LVL 7

Author Comment

ID: 41822192
``````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?
0

LVL 37

Expert Comment

ID: 41823011
>> any improvements
Yes.
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;
}
``````
0

LVL 12

Expert Comment

ID: 41823016
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.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A solution for Fortify Path Manipulation.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
###### Suggested Courses
Course of the Month3 days, 19 hours left to enroll