Solved

mapBully challenge

Posted on 2016-09-27
6
155 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
How our DevOps Teams Maximize Uptime

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

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net and sql server 4 45
java imports not found 4 30
storing csv file in table variable in Python 2 28
batch file or script 4 27
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

820 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