Solved

mapBully challenge

Posted on 2016-09-27
6
89 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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 11

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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now