Solved

mapShare challenge

Posted on 2016-09-27
13
66 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
Comment Utility
>> 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 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
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
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
Typo - map.remove("c"); ==> needs parentheses
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
>> 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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
http://www.tutorialspoint.com/online_java_formatter.htm
above is good formatter tool i found
0
 
LVL 37

Expert Comment

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

Featured Post

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

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

762 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

8 Experts available now in Live!

Get 1:1 Help Now