Solved

mapBully challenge

Posted on 2016-09-27
6
128 Views
Last Modified: 2016-09-30
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
Comment
Question by:gudii9
6 Comments
 
LVL 37

Accepted Solution

by:
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 35

Assisted Solution

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

Because your code will add the "b" to the map
0
 
LVL 7

Author Comment

by:gudii9
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;
}

Open in new window


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      
please advise
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
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;
}

Open in new window

above passes all tests. any improvements or alternate approaches?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823011
>> any improvements
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;
}

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A short article about problems I had with the new location API and permissions in Marshmallow
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question