Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

mapShare challenge

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
Avatar of zzynx
zzynx
Flag of Belgium image

>> 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.
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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?
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","");
}
Typo - map.remove("c"); ==> needs parentheses
Avatar of gudii9

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
}
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

http://www.tutorialspoint.com/online_java_formatter.htm
above is good formatter tool i found
>> above is good formatter tool i found
All right. Try to use it :)