gudii9
asked on
mapShare challenge
Hi,
I am working on below challenge
http://codingbat.com/prob/p148813
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
Expected Runabove fails one test.
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
please advise
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;
}
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","");
}
map.remove"c"; ==> in all cases
if (map.containsKey("a") {
map.put("b",map.get("a"));
map.put("a","");
}
Typo - map.remove("c"); ==> needs parentheses
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;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
}
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
}
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;
}
i see it is in if loop
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;
}
above passes all tests
ASKER
http://www.tutorialspoint.com/online_java_formatter.htm
above is good formatter tool i found
above is good formatter tool i found
>> above is good formatter tool i found
All right. Try to use it :)
All right. Try to use it :)
Yes.
This is really a so basic challenge that you should be able to solve this on your own.