Solved

mapBully challenge

Posted on 2016-09-27
6
201 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
[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
  • Learn & ask questions
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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

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.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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