Solved

mapShare challenge

Posted on 2016-09-27
13
189 Views
Last Modified: 2016-09-30
Hi,

 I am working on below challenge

http://codingbat.com/prob/p148813


Map-1 > mapShare
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 same value. In all cases remove the key "c", leaving the rest of the map unchanged.

mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}


does the  'a' key corresponding value remains same in output map as in below case "aaa"?

mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}

please advise
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
  • 4
  • 3
13 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 41819216
>> does the  'a' key corresponding value remains same in output map as in below case "aaa"?
Yes.

This is really a so basic challenge that you should be able to solve this on your own.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 250 total points
ID: 41820010
Think of the characteristics and methods for different types of maps -
Modify and return the given map as follows: if the key "a" has a value [contains], set the key [put] "b" to have that same value [get]. In all cases remove the key "c" [remove], leaving the rest of the map unchanged [need a map that preserves order].
0
 
LVL 7

Author Comment

by:gudii9
ID: 41822187
public Map<String, String> mapShare(Map<String, String> map) {
  
  String val=map.get("a");
  String backup=val;
  map.put("b",backup);
 // map.put("a","");
 map.remove("c");
  
  return map;

}

Open in new window

Expected      Run            
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}      {"b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}      {"b": null}      X      
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}      {"d": "hi", "b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"}      {"b": "xyz", "a": "xyz", "z": "zzz"}      OK      
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      OK      
other tests
X      
Correct for more than half the tests

Your progress graph for this problem

above fails one test.
please advise
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 41822203
public Map<String, String> mapShare(Map<String, String> map) {
  if(map.containsKey("a")){
  String val=map.get("a");
  String backup=val;
  map.put("b",backup);
 // map.put("a","");
 map.remove("c");
  }
  else{
     map.remove("c");
  }
  return map;

}

Open in new window


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

Expert Comment

by:awking00
ID: 41822227
You might consolidate some of the steps -
map.remove"c";  ==> in all cases
if (map.containsKey("a") {
  map.put("b",map.get("a")); ==>no need for intermediate variable
  map.put("a","");
}
0
 
LVL 32

Expert Comment

by:awking00
ID: 41822322
Typo - map.remove("c"); ==> needs parentheses
0
 
LVL 7

Author Comment

by:gudii9
ID: 41822413
public Map<String, String> mapShare(Map<String, String> map) {
  if(map.containsKey("a")){
 // String val=map.get("a");
 // String backup=val;
  map.put("b",map.get("a"));
 // map.put("a","");
 map.remove("c");
  }
 // else{
 //    map.remove("c");
 // }
  return map;

}

Open in new window


above failse one test if i try to remove c in all cases

Expected      Run            
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}      {"b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}      {"b": "xyz", "c": "ccc"}      X      
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}      {"d": "hi", "b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"}      {"b": "xyz", "a": "xyz", "z": "zzz"}      OK      
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      OK      
other tests
OK      


please advise
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 250 total points
ID: 41822996
>> above failse one test if i try to remove c in all cases
You DON'T remove "c" in all cases.
If you would remove instead of comment out old code ànd indent your code well, you would have seen that your
map.remove("c");

Open in new window

statement is INSIDE the IF statement.
A perfect illustration of WHY I always keep telling you that you should indent your code well, to make it good readable.

This works:
public Map<String, String> mapShare(Map<String, String> map) {
    map.remove("c");
    if (map.containsKey("a")) {
        map.put("b", map.get("a"));
    }
    return map;
}

Open in new window


You had this:
public Map<String, String> mapShare(Map<String, String> map) {
   if (map.containsKey("a")) {
       map.put("b",map.get("a"));
       map.remove("c");
   }
   return map;
}

Open in new window


You see the difference?
0
 
LVL 32

Expert Comment

by:awking00
ID: 41823469
Without comments, your code is -
if (map.containsKey("a")) {
    map.put("b",map.get("a"));
    map.remove("c");
}
return map;

You need to remove "c" whether "a" exists or not -
    map.remove("c"); ==> note - if "c" doesn't exist, nothing will happen, if it does, the record is removed from the map
if (map.containsKey("a")) {
    map.put("b",map.get("a"));
    map.put("a",""); ==> this is still needed
}
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823528
public Map < String, String > mapShare(Map < String, String > map) {
    if (map.containsKey("a")) {
        String val = map.get("a");
        String backup = val;
        map.put("b", backup);
        // map.put("a","");
        map.remove("c");
    } else {
        map.remove("c");
    }
    return map;

}

Open in new window


i see it is in if loop
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823534
public Map < String, String > mapShare(Map < String, String > map) {
    map.remove("c");
    if (map.containsKey("a")) {
        // String val=map.get("a");
        // String backup=val;
        map.put("b", map.get("a"));
        // map.put("a","");
        //map.remove("c");
    }
    // else{
    //    map.remove("c");
    // }
    return map;

}

Open in new window


above passes all tests
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823536
http://www.tutorialspoint.com/online_java_formatter.htm
above is good formatter tool i found
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823567
>> above is good formatter tool i found
All right. Try to use it :)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

691 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