Solved

mapShare challenge

Posted on 2016-09-27
13
105 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 42
by zero exception 10 40
ejb on wildfly 5 20
Adding multiple JVM environments to RedHat 6 7 20
This is about my first experience with programming Arduino.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

770 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